![]() |
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
(PowerBuilder) 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.
integer li_rc integer li_Success oleobject loo_Rsa string ls_PubKeyXml string ls_PrivKeyXml oleobject loo_PubKey oleobject loo_PrivKey string ls_PlainText integer li_UsePrivateKey string ls_EncryptedStr1 string ls_EncryptedStr2 oleobject loo_Rsa2 string ls_DecryptedStr1 string ls_DecryptedStr2 li_Success = 0 // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Rsa = create oleobject li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa") if li_rc < 0 then destroy loo_Rsa MessageBox("Error","Connecting to COM object failed") return end if ls_PubKeyXml = "<RSAPublicKey><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent></RSAPublicKey>" ls_PrivKeyXml = "<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>" loo_PubKey = create oleobject li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey") li_Success = loo_PubKey.LoadFromString(ls_PubKeyXml) if li_Success = 0 then Write-Debug loo_PubKey.LastErrorText destroy loo_Rsa destroy loo_PubKey return end if loo_PrivKey = create oleobject li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey") li_Success = loo_PrivKey.LoadXml(ls_PrivKeyXml) if li_Success = 0 then Write-Debug loo_PrivKey.LastErrorText destroy loo_Rsa destroy loo_PubKey destroy loo_PrivKey return end if li_Success = loo_Rsa.UsePublicKey(loo_PubKey) if li_Success = 0 then Write-Debug loo_Rsa.LastErrorText destroy loo_Rsa destroy loo_PubKey destroy loo_PrivKey return end if // Encrypt a string and return the encrypted data base64-encoded: loo_Rsa.EncodingMode = "base64" ls_PlainText = "RSA gives different results with each call, weird but OK" li_UsePrivateKey = 0 ls_EncryptedStr1 = loo_Rsa.EncryptStringENC(ls_PlainText,li_UsePrivateKey) Write-Debug ls_EncryptedStr1 // Do it again. The results are different... ls_EncryptedStr2 = loo_Rsa.EncryptStringENC(ls_PlainText,li_UsePrivateKey) Write-Debug ls_EncryptedStr2 // Now decrypt both strings, and the results are correct // in both cases: loo_Rsa2 = create oleobject li_rc = loo_Rsa2.ConnectToNewObject("Chilkat.Rsa") li_Success = loo_Rsa2.UsePrivateKey(loo_PrivKey) if li_Success = 0 then Write-Debug loo_Rsa.LastErrorText destroy loo_Rsa destroy loo_PubKey destroy loo_PrivKey destroy loo_Rsa2 return end if loo_Rsa2.EncodingMode = "base64" li_UsePrivateKey = 1 ls_DecryptedStr1 = loo_Rsa2.DecryptStringENC(ls_EncryptedStr1,li_UsePrivateKey) Write-Debug ls_DecryptedStr1 ls_DecryptedStr2 = loo_Rsa2.DecryptStringENC(ls_EncryptedStr2,li_UsePrivateKey) Write-Debug ls_DecryptedStr2 destroy loo_Rsa destroy loo_PubKey destroy loo_PrivKey destroy loo_Rsa2 |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.