Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) RSA Encrypt Randomly Generated AES KeyDemonstrates how to RSA encrypt a randomly generated AES key.
IncludeFile "CkBinData.pb" IncludeFile "CkCert.pb" IncludeFile "CkRsa.pb" IncludeFile "CkPrng.pb" IncludeFile "CkPublicKey.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. ; First generate a 256-bit AES key (32 bytes). prng.i = CkPrng::ckCreate() If prng.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bdAesKey.i = CkBinData::ckCreate() If bdAesKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkPrng::ckGenRandomBd(prng,32,bdAesKey) ; Use a public key from a certificate for RSA encryption. cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadFromFile(cert,"qa_data/pem/mf_public_rsa.pem") If success = 0 Debug CkCert::ckLastErrorText(cert) CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) ProcedureReturn EndIf pubKey.i = CkCert::ckExportPublicKey(cert) If CkCert::ckLastMethodSuccess(cert) <> 1 Debug CkCert::ckLastErrorText(cert) CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) ProcedureReturn EndIf rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRsa::ckImportPublicKeyObj(rsa,pubKey) If success = 0 Debug CkRsa::ckLastErrorText(rsa) CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) CkRsa::ckDispose(rsa) ProcedureReturn EndIf CkPublicKey::ckDispose(pubKey) ; RSA encrypt our 32-byte AES key. ; The contents of bdAesKey are replaced with result of the RSA encryption. success = CkRsa::ckEncryptBd(rsa,bdAesKey,0) If success = 0 Debug CkRsa::ckLastErrorText(rsa) CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) CkRsa::ckDispose(rsa) ProcedureReturn EndIf ; Return the result as a base64 string encryptedAesKey.s = CkBinData::ckGetEncoded(bdAesKey,"base64") Debug "encrypted AES key = " + encryptedAesKey CkPrng::ckDispose(prng) CkBinData::ckDispose(bdAesKey) CkCert::ckDispose(cert) CkRsa::ckDispose(rsa) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.