Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Generate an RSA Key and Save to Encrypted PEMSee more RSA ExamplesDemonstrates how to generate an RSA key and save to an encrypted PEM file.
IncludeFile "CkPublicKey.pb" IncludeFile "CkPrivateKey.pb" IncludeFile "CkRsa.pb" Procedure ChilkatExample() rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Generate a 2048-bit key. success.i = CkRsa::ckGenerateKey(rsa,2048) If success = 0 Debug CkRsa::ckLastErrorText(rsa) CkRsa::ckDispose(rsa) ProcedureReturn EndIf privKey.i = CkRsa::ckExportPrivateKeyObj(rsa) password.s = "secret" ; Saving to a relative path (from the current working directory of the process). path.s = "rsaKeys/myTestRsaPrivate.pem" ; Encrypt the PEM using 256-bit AES encryption. CkPrivateKey::setCkPkcs8EncryptAlg(privKey, "aes256") success = CkPrivateKey::ckSavePkcs8EncryptedPemFile(privKey,password,path) If success = 0 Debug CkPrivateKey::ckLastErrorText(privKey) CkRsa::ckDispose(rsa) ProcedureReturn EndIf CkPrivateKey::ckDispose(privKey) ; ; We can also save the public key. ; There is no need to encrypt public keys. pubKey.i = CkRsa::ckExportPublicKeyObj(rsa) path = "rsaKeys/myTestRsaPublic.pem" ; Choose PKCS1 or PKCS8 ; We'll choose PKCS8. preferPkcs1.i = 0 success = CkPublicKey::ckSavePemFile(pubKey,preferPkcs1,path) If success = 0 Debug CkPublicKey::ckLastErrorText(pubKey) CkRsa::ckDispose(rsa) ProcedureReturn EndIf CkPublicKey::ckDispose(pubKey) ; Debug "Success." CkRsa::ckDispose(rsa) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.