Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(C) Get Public Key in Format for ~/.ssh/authorized_keys

If you have an RSA private key, then by definition you also have the public key because the RSA public key is a subset of the private key. (The public key is just the modulus + exponent, whereas the private key contains additional items.)

This example converts an RSA private key to the OpenSSH format public-key (a single line of text) that can be inserted into the ~/.ssh/authorized_keys file on the SSH server to enable public-key authentication for a particular private key.

Note: On the SSH server, such as on a Ubuntu Linux computer, the sshd config file is located at /etc/ssh/sshd_config. It contains a line that indicates the authorized keys file, and the typical default is:

AuthorizedKeysFile      .ssh/authorized_keys

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkStringBuilder.h>
#include <C_CkSshKey.h>

void ChilkatSample(void)
    {
    const char *privateKeyPath;
    HCkStringBuilder sbKey;
    BOOL success;
    HCkSshKey key;
    const char *pubKeyStr;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Load a private key from a file.  The format can be text format, such as .ppk (PuTTY private key) or .pem.
    privateKeyPath = "/myKeys/myRsaKey.ppk";

    // Here's an example of a non-encrypted private key in ppk format:

    // PuTTY-User-Key-File-3: ssh-rsa
    // Encryption: none
    // Comment: rsa-key-20220606
    // Public-Lines: 6
    // AAAAB3NzaC1yc2EAAAADAQABAAABAQDKQUdx8EU6qIpVcT1NbL/sSnELJdifQa+K
    // kOqvgKIU30wIwGMfigSeiTFXw2jWsPcMyOIRKUvR3nf8wtR6NEj3qDfGUo3b/8Fx
    // 5BHDKuR5uU0ZJmv3Am+/hSPWOJFltL8DkxDLEpGDujLwGiUbpD9SADYhhvk9OBA7
    // 0l70hYs9gWJBjm9CR8hPg3b3F7gOYljAY2dk6W3PGmzQi+D7BTE0yYfhEOoDX7ah
    // sgrUnWIiIWK9cqLT8XbAb8gitnCmP0LerxRAkX25O4Gx/uGSrMxO/cRwz5UpiNL2
    // cciM7P4vzlbcpQiKrKaBYg5GB84QqE3xCt9cEr/qD235LpSMnWRR
    // Private-Lines: 14
    // AAABAQClKCE7PUSK3c34b3vrmX4vaapdvA3kHjNGJ4g8wAGaoazpCJDo1D9pZgZQ
    // 8FTP27ohSniwItSzD8NTN5ViJQfglBDXddo5Z+ODKQYIJSJk85etjd5j2i1+ay4U
    // ZCT2tF22gYUZDpScyJOH1RGwPLMoNtv9DMbB4uH+t46qhdJp6aWvJB7L/HUhnszc
    // qbwTG//CDL2j8Y1Mre8zvWBA8cr0I0qCzWD/Xw5MgDgpA444CFGpYb1mt8ghLniT
    // hMGgt6scUO/lnBEYo17a9N7wBexvyk0ZgEVo6nweRnCFHijNbG+C82svrsLQuL7g
    // o65BcEqLo+wBO+FtutTf2gtevZcBAAAAgQDnt2Freyfn1V93qZar06w7dXn4yV4G
    // cvQ3FmJBhk7akEFuZPOceDqrAgzS+TmsH4C96ssETdMxMO4vwEjSnZAatw6TKgnK
    // 9aDJL6/uzhk/dmBmyp7iWttN9QW9cRQvLYtwJssNDPUmF1/1cGFMpLyLfTlRBCok
    // 8dksYys4FTUfeQAAAIEA33N+LIO8i2U1NNfy2yU5GGJoFaXwPIa/CZNzZ8hbvLwm
    // 5yay4MKxGgizX+FhQ/OiEiFDEs1GDssKTXaqhE9mI/vKKLGYj9wEiWhwnawx56GI
    // WhdTZPtWkU4tYj/OkABD8vp973W1k4NEjYxVg8AoZZL1Kr4zturV8KJl9v4b/ZkA
    // AACBANAijbBLPxSTdyG9duZT89+jIdgjdjVdV5UL/xZLEh6pWbHludZHItXl62G7
    // luuJbiqhx2FANxhSu7/tAEX3z2jx7ari51Ddf/+sqfrLwc3dPkU8YXpUNYJIaNFR
    // LyzMMZvL2sPxnE6dxt4nR761y+EnTZpZ2oeSC21Lzq2xKcBp
    // Private-MAC: e0c3b5eba925944355d8d63fefcec02a982c7281a87e9994c04ce956b5c3e69b

    sbKey = CkStringBuilder_Create();
    success = CkStringBuilder_LoadFile(sbKey,privateKeyPath,"utf-8");
    if (success == FALSE) {
        printf("Failed to load the private key from a file.\n");
        CkStringBuilder_Dispose(sbKey);
        return;
    }

    key = CkSshKey_Create();

    // If the private key file is encrypted, then you'll need to set a password.
    // If it's not encrypted, the password here is ignored.
    CkSshKey_putPassword(key,"private_key_password");

    // The FromPuttyPrivateKey method will actually auto-detect the format and correctly load any string-based format.
    // For example, you can pass in a PEM private key.
    success = CkSshKey_FromPuttyPrivateKey(key,CkStringBuilder_getAsString(sbKey));
    if (success == FALSE) {
        printf("%s\n",CkSshKey_lastErrorText(key));
        CkStringBuilder_Dispose(sbKey);
        CkSshKey_Dispose(key);
        return;
    }

    // Get the public key in OpenSSH format:
    pubKeyStr = CkSshKey_toOpenSshPublicKey(key);
    printf("%s\n",pubKeyStr);

    // Sample output:
    // ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKQUdx8EU6qIpVcT1NbL/sSnELJdifQa+KkOqvgKIU30wIwGMfigSeiTFXw2jWsPcMyOIRKUvR3nf8wtR6NEj3qDfGUo3b/8Fx5BHDKuR5uU0ZJmv3Am+/hSPWOJFltL8DkxDLEpGDujLwGiUbpD9SADYhhvk9OBA70l70hYs9gWJBjm9CR8hPg3b3F7gOYljAY2dk6W3PGmzQi+D7BTE0yYfhEOoDX7ahsgrUnWIiIWK9cqLT8XbAb8gitnCmP0LerxRAkX25O4Gx/uGSrMxO/cRwz5UpiNL2cciM7P4vzlbcpQiKrKaBYg5GB84QqE3xCt9cEr/qD235LpSMnWRR rsa-key-20220606

    // To authorize the private key for a particular SSH user account, copy the above public key into the ~/.ssh/authorized_keys file.
    // If authorized_keys already contains some keys, you can append it to a new line in the file.


    CkStringBuilder_Dispose(sbKey);
    CkSshKey_Dispose(key);

    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.