Sample code for 30+ languages & platforms
PureBasic

Duplicate PHP RSA Encryption

See more RSA Examples

Demonstrates how to duplicate the following PHP function.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkRsa.pb"
IncludeFile "CkPublicKey.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Duplicate the following PHP code:
    ; 
    ;    public function encryptRSA($plainText,$rsaMOD,$pubKEY){
    ;         $rsa  = new RSA();
    ;         $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
    ;         $publicKey = [
    ;             'e' => new BigInteger($pubKEY,16),
    ;             'n' => new BigInteger($rsaMOD,16)
    ;         ];
    ; 		
    ;         $rsa->loadKey($publicKey);
    ;         $ciphertext = $rsa->encrypt($plainText);
    ;         return bin2hex($ciphertext);
    ;     }
    ; 
    ;     $plainText="key=abcdefghijkmnopq&iv=abcdefghijkmnopq&h=12345678&s=12345678"
    ;     $rsaMOD="F0946D8F05604809E24B8CFFD30349CEA9E5F4D320BFD9E9AA1B088863F02C43E7997D37A3E27B4F8F359F1744DB6B20A437067C0D325A80660D12FF56A57673"
    ;     $pubKEY="010001"

    ; We have the RSA modulus in hex
    rsaMOD.s = "F0946D8F05604809E24B8CFFD30349CEA9E5F4D320BFD9E9AA1B088863F02C43E7997D37A3E27B4F8F359F1744DB6B20A437067C0D325A80660D12FF56A57673"

    ; The RSA exponent in hex is "010001", which is 65537 in decimal.  It's typically the exponent that is always used.
    rsaEXP.s = "010001"

    ; Get the RSA modulus and exponent in base64.
    bdMod.i = CkBinData::ckCreate()
    If bdMod.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    success = CkBinData::ckAppendEncoded(bdMod,rsaMOD,"hex")
    success = CkBinData::ckAppendEncoded(bdExp,rsaEXP,"hex")

    ; Build the XML representation of the RSA public key
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "RSAPublicKey")
    CkXml::ckUpdateChildContent(xml,"Modulus",CkBinData::ckGetEncoded(bdMod,"base64"))
    CkXml::ckUpdateChildContent(xml,"Exponent",CkBinData::ckGetEncoded(bdExp,"base64"))

    ; Load the RSA public key into a Chilkat public key object.
    pubkey.i = CkPublicKey::ckCreate()
    If pubkey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPublicKey::ckLoadFromString(pubkey,CkXml::ckGetXml(xml))

    ; Setup the RSA object for encryption and do it..
    rsa.i = CkRsa::ckCreate()
    If rsa.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRsa::setCkVerboseLogging(rsa, 1)
    success = CkRsa::ckUsePublicKey(rsa,pubkey)

    ; Use PKCSv1.5 padding
    CkRsa::setCkPkcsPadding(rsa, 1)

    ; Encrypt and return the string as hex.
    CkRsa::setCkEncodingMode(rsa, "hex")
    plainText.s = "key=abcdefghijkmnopq&iv=abcdefghijkmnopq&h=12345678&s=12345678"
    cipherText.s = CkRsa::ckEncryptStringENC(rsa,plainText,0)
    If CkRsa::ckLastMethodSuccess(rsa) = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkBinData::ckDispose(bdMod)
        CkBinData::ckDispose(bdExp)
        CkXml::ckDispose(xml)
        CkPublicKey::ckDispose(pubkey)
        CkRsa::ckDispose(rsa)
        ProcedureReturn
    EndIf

    ; Note: The PKCSv1_5 padding incorporates random bytes.  Therefore, the RSA encryption will produce different results each time -- all of which are valid 
    ; and decrypt correctly to the same original text.
    Debug cipherText


    CkBinData::ckDispose(bdMod)
    CkBinData::ckDispose(bdExp)
    CkXml::ckDispose(xml)
    CkPublicKey::ckDispose(pubkey)
    CkRsa::ckDispose(rsa)


    ProcedureReturn
EndProcedure