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 Sign Binary Data and Verify (Recover the Original Data)Demonstrates how to RSA sign binary data and then verify/recover the original data. Note: This example uses methods introduced in Chilkat v9.5.0.77.
IncludeFile "CkBinData.pb" IncludeFile "CkPublicKey.pb" IncludeFile "CkPrivateKey.pb" IncludeFile "CkRsa.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. ; Load an RSA private key for signing. privKey.i = CkPrivateKey::ckCreate() If privKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkPrivateKey::ckLoadEncryptedPemFile(privKey,"qa_data/pem/rsa_passwd.pem","passwd") If success <> 1 Debug CkPrivateKey::ckLastErrorText(privKey) CkPrivateKey::ckDispose(privKey) ProcedureReturn EndIf rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkRsa::ckImportPrivateKeyObj(rsa,privKey) ; We have some binary data (in hex) to sign originalData.s = "0102030405060708090A" bd.i = CkBinData::ckCreate() If bd.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkBinData::ckAppendEncoded(bd,originalData,"hex") ; If successful, the contents of bd are replaced with the RSA signature. success = CkRsa::ckOpenSslSignBd(rsa,bd) If success <> 1 Debug CkRsa::ckLastErrorText(rsa) CkPrivateKey::ckDispose(privKey) CkRsa::ckDispose(rsa) CkBinData::ckDispose(bd) ProcedureReturn EndIf ; Show the RSA signature in base64 Debug CkBinData::ckGetEncoded(bd,"base64") ; ------------------------------------------ ; Get the public key from the private key pubKey.i = CkPrivateKey::ckGetPublicKey(privKey) ; Verify the signature and extract the original data. rsa2.i = CkRsa::ckCreate() If rsa2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkRsa::ckImportPublicKeyObj(rsa2,pubKey) bVerified.i = CkRsa::ckOpenSslVerifyBd(rsa2,bd) Debug "signature verified: " + Str(bVerified) ; Show the original data: Debug "original data: " + CkBinData::ckGetEncoded(bd,"hex") CkPublicKey::ckDispose(pubKey) CkPrivateKey::ckDispose(privKey) CkRsa::ckDispose(rsa) CkBinData::ckDispose(bd) CkRsa::ckDispose(rsa2) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.