Sample code for 30+ languages & platforms
AutoIt

RSA Encrypt Randomly Generated AES Key

See more RSA Examples

Demonstrates how to RSA encrypt a randomly generated AES key.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

; First generate a 256-bit AES key (32 bytes).
$oPrng = ObjCreate("Chilkat.Prng")
$oBdAesKey = ObjCreate("Chilkat.BinData")
$bSuccess = $oPrng.GenRandomBd(32,$oBdAesKey)

; Use a public key from a certificate for RSA encryption.
$oCert = ObjCreate("Chilkat.Cert")

$bSuccess = $oCert.LoadFromFile("qa_data/pem/mf_public_rsa.pem")
If ($bSuccess = False) Then
    ConsoleWrite($oCert.LastErrorText & @CRLF)
    Exit
EndIf

$oPubKey = ObjCreate("Chilkat.PublicKey")
$oCert.GetPublicKey($oPubKey)

$oRsa = ObjCreate("Chilkat.Rsa")
$bSuccess = $oRsa.UsePublicKey($oPubKey)
If ($bSuccess = False) Then
    ConsoleWrite($oRsa.LastErrorText & @CRLF)
    Exit
EndIf

; RSA encrypt our 32-byte AES key.
; The contents of bdAesKey are replaced with result of the RSA encryption.
$bSuccess = $oRsa.EncryptBd($oBdAesKey,False)
If ($bSuccess = False) Then
    ConsoleWrite($oRsa.LastErrorText & @CRLF)
    Exit
EndIf

; Return the result as a base64 string
Local $sEncryptedAesKey = $oBdAesKey.GetEncoded("base64")

ConsoleWrite("encrypted AES key = " & $sEncryptedAesKey & @CRLF)