Sample code for 30+ languages & platforms
PureBasic

RSA Encryption -- Same Key Different Results

See more RSA Examples

The 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.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPublicKey.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkRsa.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    rsa.i = CkRsa::ckCreate()
    If rsa.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    pubKeyXml.s = "<RSAPublicKey><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent></RSAPublicKey>"

    privKeyXml.s = "<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>"

    pubKey.i = CkPublicKey::ckCreate()
    If pubKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPublicKey::ckLoadFromString(pubKey,pubKeyXml)
    If success = 0
        Debug CkPublicKey::ckLastErrorText(pubKey)
        CkRsa::ckDispose(rsa)
        CkPublicKey::ckDispose(pubKey)
        ProcedureReturn
    EndIf

    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckLoadXml(privKey,privKeyXml)
    If success = 0
        Debug CkPrivateKey::ckLastErrorText(privKey)
        CkRsa::ckDispose(rsa)
        CkPublicKey::ckDispose(pubKey)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    success = CkRsa::ckUsePublicKey(rsa,pubKey)
    If success = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkRsa::ckDispose(rsa)
        CkPublicKey::ckDispose(pubKey)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    ; Encrypt a string and return the encrypted data base64-encoded:
    CkRsa::setCkEncodingMode(rsa, "base64")

    plainText.s = "RSA gives different results with each call, weird but OK"

    usePrivateKey.i = 0
    encryptedStr1.s = CkRsa::ckEncryptStringENC(rsa,plainText,usePrivateKey)
    Debug encryptedStr1

    ; Do it again. The results are different...
    encryptedStr2.s = CkRsa::ckEncryptStringENC(rsa,plainText,usePrivateKey)
    Debug encryptedStr2

    ; Now decrypt both strings, and the results are correct
    ; in both cases:
    rsa2.i = CkRsa::ckCreate()
    If rsa2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRsa::ckUsePrivateKey(rsa2,privKey)
    If success = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkRsa::ckDispose(rsa)
        CkPublicKey::ckDispose(pubKey)
        CkPrivateKey::ckDispose(privKey)
        CkRsa::ckDispose(rsa2)
        ProcedureReturn
    EndIf

    CkRsa::setCkEncodingMode(rsa2, "base64")
    usePrivateKey = 1

    decryptedStr1.s = CkRsa::ckDecryptStringENC(rsa2,encryptedStr1,usePrivateKey)
    Debug decryptedStr1

    decryptedStr2.s = CkRsa::ckDecryptStringENC(rsa2,encryptedStr2,usePrivateKey)
    Debug decryptedStr2


    CkRsa::ckDispose(rsa)
    CkPublicKey::ckDispose(pubKey)
    CkPrivateKey::ckDispose(privKey)
    CkRsa::ckDispose(rsa2)


    ProcedureReturn
EndProcedure