![]() |
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
(Lianja) RSA Sign with PKCS8 Encrypted KeyDemonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it). Note: This example requires Chilkat v11.0.0 or greater.
llSuccess = .F. // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loPrivKey = createobject("CkPrivateKey") // Load the private key from an RSA PEM file: llSuccess = loPrivKey.LoadAnyFormatFile("raul_privateKey.key","a0123456789") if (llSuccess = .F.) then ? loPrivKey.LastErrorText release loPrivKey return endif loRsa = createobject("CkRsa") // Import the private key into the RSA component: llSuccess = loRsa.UsePrivateKey(loPrivKey) if (llSuccess = .F.) then ? loRsa.LastErrorText release loPrivKey release loRsa return endif // This example will sign a string, and receive the signature // in a hex-encoded string. Therefore, set the encoding mode // to "hex": loRsa.EncodingMode = "hex" lcStrData = "This is the string to be signed." // Sign the string using the sha256 hash algorithm. // Other valid choices are sha1, sha384, sha512 and others. lcHexSig = loRsa.SignStringENC(lcStrData,"sha256") if (loRsa.LastMethodSuccess = .F.) then ? loRsa.LastErrorText release loPrivKey release loRsa return endif ? lcHexSig // Now verify with the public key. // This example shows how to use the public key from // a digital certificate (.cer file) loCert = createobject("CkCert") llSuccess = loCert.LoadFromFile("raul_publicKey.cer") if (llSuccess = .F.) then ? loCert.LastErrorText release loPrivKey release loRsa release loCert return endif loPubKey = createobject("CkPublicKey") loCert.GetPublicKey(loPubKey) loRsa2 = createobject("CkRsa") llSuccess = loRsa2.UsePublicKey(loPubKey) if (llSuccess = .F.) then ? loRsa2.LastErrorText release loPrivKey release loRsa release loCert release loPubKey release loRsa2 return endif // Verify the signature against the original data: loRsa2.EncodingMode = "hex" llSuccess = loRsa2.VerifyStringENC(lcStrData,"sha256",lcHexSig) if (llSuccess = .F.) then ? loRsa2.LastErrorText release loPrivKey release loRsa release loCert release loPubKey release loRsa2 return endif ? "Signature verified!" // Verify with incorrect data: llSuccess = loRsa2.VerifyStringENC("something else","sha256",lcHexSig) if (llSuccess <> .T.) then ? "Signature not verified! (which was expected in this case)" else ? "Hmmm... that's not right..." endif release loPrivKey release loRsa release loCert release loPubKey release loRsa2 |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.