Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode 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
Apple Keychain
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
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

 

 

 

(Unicode C++) Generate an RSA Key and Save to Encrypted PEM

See more RSA Examples

Demonstrates how to generate an RSA key and save to an encrypted PEM file.

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 <CkRsaW.h>
#include <CkPrivateKeyW.h>
#include <CkPublicKeyW.h>

void ChilkatSample(void)
    {
    CkRsaW rsa;

    // Generate a 2048-bit key.
    bool success = rsa.GenerateKey(2048);
    if (success == false) {
        wprintf(L"%s\n",rsa.lastErrorText());
        return;
    }

    CkPrivateKeyW *privKey = rsa.ExportPrivateKeyObj();
    const wchar_t *password = L"secret";
    // Saving to a relative path (from the current working directory of the process).
    const wchar_t *path = L"rsaKeys/myTestRsaPrivate.pem";
    // Encrypt the PEM using 256-bit AES encryption.
    privKey->put_Pkcs8EncryptAlg(L"aes256");
    success = privKey->SavePkcs8EncryptedPemFile(password,path);
    if (success == false) {
        wprintf(L"%s\n",privKey->lastErrorText());
        return;
    }

    delete privKey;

    // 

    // We can also save the public key.
    // There is no need to encrypt public keys.
    CkPublicKeyW *pubKey = rsa.ExportPublicKeyObj();
    path = L"rsaKeys/myTestRsaPublic.pem";
    // Choose PKCS1 or PKCS8
    // We'll choose PKCS8.
    bool preferPkcs1 = false;
    success = pubKey->SavePemFile(preferPkcs1,path);
    if (success == false) {
        wprintf(L"%s\n",pubKey->lastErrorText());
        return;
    }

    delete pubKey;

    // 

    wprintf(L"Success.\n");
    }

 

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