![]() |
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
(DataFlex) RSA Encryption -- Same Key Different ResultsThe RSA encryption algorithm produces different results for each call, even when encrypting the same data with the same key. Decryption however, will produce the correct results. This example demonstrates. *** The reason this occurs is that RSA encryption uses PKCS1 v1.5 padding, and this padding scheme uses random bytes. It is random bytes in the padding that causes the result to be different each time. Note: This example requires Chilkat v11.0.0 or greater.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoRsa String sPubKeyXml String sPrivKeyXml Variant vPubKey Handle hoPubKey Variant vPrivKey Handle hoPrivKey String sPlainText Boolean iUsePrivateKey String sEncryptedStr1 String sEncryptedStr2 2 Handle hoRsa2 String sDecryptedStr1 String sDecryptedStr2 String sTemp1 Move False To iSuccess // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatRsa)) To hoRsa If (Not(IsComObjectCreated(hoRsa))) Begin Send CreateComObject of hoRsa End Move "<RSAPublicKey><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent></RSAPublicKey>" To sPubKeyXml Move "<RSAKeyValue><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent><P>4cpW9fvG99Jsz8/AO7PDHTl+pPRAglksrR2kClLV2g9DEeFe/bvmCxLUgMCJ+0eGQ1zA6aA7McKr13zTQ7jKpQ==</P><Q>4cCS/kFlq/P1ExF37Fkh4pCodOEGutepLEG7Q/KljT3ZGlAY+2l8fGu4f+hrkUuGoFl7NOMaJflULoPIgQaq/Q==</Q><DP>lkjcSsvzqh3YKRXJiLNkyf3rypV8noYGU4+oEOsDxilkZfFRDafUPUiiQrRk4ui/d/SzvozU+ZDuWfaOk8PatQ==</DP><DQ>SYCD25i7W8Mwdibn3uIecEAdOQDTSh5RjIFSUYs9b8FFYJXXrHPp/jCsf6jS7RmkGa1Iui1/JAIL8KEjtS7QmQ==</DQ><InverseQ>EDAJa3TpNdPQ3GIdBpnTgFTQY5A60DcszsUW/iCYoXQdPVJ9BLBxVTe9jiLzGuNuzLkVBwQlCy0Bf84hACRV9A==</InverseQ><D>cMFdDYKkddlRNczaugOmOH8b1egpx2liSPs6GYZ2gFObAXJiPK8m+r6c2ckls7hrlUP0DZhi4cG6Tn7xANb0Ek17P7QquVhQYOmFy/YHzm+IJbcwwq7pJHhZBhtcjyXqfUZ+BADGE//GQbrSVwVltpOj5KcxG88NAprLn2MMxfE=</D></RSAKeyValue>" To sPrivKeyXml Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey If (Not(IsComObjectCreated(hoPubKey))) Begin Send CreateComObject of hoPubKey End Get ComLoadFromString Of hoPubKey sPubKeyXml To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoPubKey To sTemp1 Showln sTemp1 Procedure_Return End Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey If (Not(IsComObjectCreated(hoPrivKey))) Begin Send CreateComObject of hoPrivKey End Get ComLoadXml Of hoPrivKey sPrivKeyXml To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoPrivKey To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoPubKey to vPubKey Get ComUsePublicKey Of hoRsa vPubKey To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoRsa To sTemp1 Showln sTemp1 Procedure_Return End // Encrypt a string and return the encrypted data base64-encoded: Set ComEncodingMode Of hoRsa To "base64" Move "RSA gives different results with each call, weird but OK" To sPlainText Move False To iUsePrivateKey Get ComEncryptStringENC Of hoRsa sPlainText iUsePrivateKey To sEncryptedStr1 Showln sEncryptedStr1 // Do it again. The results are different... Get ComEncryptStringENC Of hoRsa sPlainText iUsePrivateKey To sEncryptedStr2 Showln sEncryptedStr2 // Now decrypt both strings, and the results are correct // in both cases: Get Create (RefClass(cComChilkatRsa)) To hoRsa2 If (Not(IsComObjectCreated(hoRsa2))) Begin Send CreateComObject of hoRsa2 End Get pvComObject of hoPrivKey to vPrivKey Get ComUsePrivateKey Of hoRsa2 vPrivKey To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoRsa To sTemp1 Showln sTemp1 Procedure_Return End Set ComEncodingMode Of hoRsa2 To "base64" Move True To iUsePrivateKey Get ComDecryptStringENC Of hoRsa2 sEncryptedStr1 iUsePrivateKey To sDecryptedStr1 Showln sDecryptedStr1 Get ComDecryptStringENC Of hoRsa2 sEncryptedStr2 iUsePrivateKey To sDecryptedStr2 Showln sDecryptedStr2 End_Procedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.