Sample code for 30+ languages & platforms
PureBasic

RSA Encrypting Symmetric Secret Key

See more RSA Examples

The RSA encryption algorithm is computationally expensive. It is not the best choice for encrypting large amounts of data. Symmetric encryption algorithms such as AES (i.e. Rijndael) or Blowfish are much more efficient. A typical application scenario is that you want to send encrypted messages to a partner, but you don't want to send the symmetric key unprotected. A solution is to generate a public/private RSA key pair and provide your partner with the public key (in advance). You may then encrypt the symmetric algorithm's key using the RSA private key. Next, encrypt the message using the symmetric algorithm, and send your partner both the encrypted key and encrypted message. Your partner decrypts by first RSA decrypting the key, and then uses the decrypted symmetric key to decrypt the message content.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPublicKey.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkRsa.pb"
IncludeFile "CkCrypt2.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

    ; Load a public/private key pair from a .snk key file.
    ; Assume you have already provided your partner with 
    ; the public key part of the key pair.
    xmlKey.s = CkRsa::ckSnkToXml(rsa,"qa_data/rsaKeys/test.snk")
    If CkRsa::ckLastMethodSuccess(rsa) = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkRsa::ckDispose(rsa)
        ProcedureReturn
    EndIf

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

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

    CkRsa::ckUsePublicKey(rsa,pubKey)

    ; Our message data will be encrypted using 128-bit AES
    ; encryption, using CBC (cipher-block chaining).
    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkCryptAlgorithm(crypt, "aes")
    CkCrypt2::setCkCipherMode(crypt, "cbc")
    CkCrypt2::setCkKeyLength(crypt, 128)

    ; Generate 128-bit (16 bytes) secret key and return as a hex string.
    CkCrypt2::setCkEncodingMode(crypt, "hex")
    secretKey.s = CkCrypt2::ckGenRandomBytesENC(crypt,16)
    Debug "Unencrypted Key: " + secretKey

    ; Use the key we generated:
    CkCrypt2::ckSetEncodedKey(crypt,secretKey,"hex")

    ; RSA encrypt the secret key and return as a hex string:
    CkRsa::setCkEncodingMode(rsa, "hex")
    bUsePrivateKey.i = 0
    encryptedKey.s = CkRsa::ckEncryptStringENC(rsa,secretKey,bUsePrivateKey)

    ; Symmetric encrypt a message.  For this example the message
    ; is very short, but typically this is where a large amount
    ; of data may be encrypted.
    CkCrypt2::setCkEncodingMode(crypt, "base64")
    encryptedText.s = CkCrypt2::ckEncryptStringENC(crypt,"Hello World!")

    ; Show our encrypted key and encrypted text:
    Debug "Encrypted Key: " + encryptedKey
    Debug "Encrypted Text: " + encryptedText

    ; Assume we sent these strings to our partner...

    ; Here's what we do at the partner end:
    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    CkRsa::ckUsePrivateKey(rsa,privKey)

    ; First, decrypt the encryptedKey:
    bUsePrivateKey = 1
    decryptedKey.s = CkRsa::ckDecryptStringENC(rsa,encryptedKey,bUsePrivateKey)
    Debug "Decrypted Key: " + decryptedKey

    ; Set our crypt object's properties and secret key:
    crypt2.i = CkCrypt2::ckCreate()
    If crypt2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkCryptAlgorithm(crypt2, "aes")
    CkCrypt2::setCkCipherMode(crypt2, "cbc")
    CkCrypt2::setCkKeyLength(crypt2, 128)
    CkCrypt2::setCkEncodingMode(crypt2, "base64")
    CkCrypt2::ckSetEncodedKey(crypt2,decryptedKey,"hex")

    ; Decrypt the message:
    decryptedText.s = CkCrypt2::ckDecryptStringENC(crypt2,encryptedText)
    Debug "Decrypted Text: " + decryptedText


    CkRsa::ckDispose(rsa)
    CkPublicKey::ckDispose(pubKey)
    CkCrypt2::ckDispose(crypt)
    CkPrivateKey::ckDispose(privKey)
    CkCrypt2::ckDispose(crypt2)


    ProcedureReturn
EndProcedure