Sample code for 30+ languages & platforms
AutoIt

Generate an RSA Key and Save to Encrypted PEM

See more RSA Examples

Demonstrates how to generate an RSA key and save to an encrypted PEM file.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oRsa = ObjCreate("Chilkat.Rsa")

; Generate a 2048-bit key.
$oPrivKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oRsa.GenKey(2048,$oPrivKey)
If ($bSuccess = False) Then
    ConsoleWrite($oRsa.LastErrorText & @CRLF)
    Exit
EndIf

Local $sPassword = "secret"
; Saving to a relative path (from the current working directory of the process).
Local $sPath = "rsaKeys/myTestRsaPrivate.pem"
; Encrypt the PEM using 256-bit AES encryption.
$oPrivKey.Pkcs8EncryptAlg = "aes256"
$bSuccess = $oPrivKey.SavePkcs8EncryptedPemFile($sPassword,$sPath)
If ($bSuccess = False) Then
    ConsoleWrite($oPrivKey.LastErrorText & @CRLF)
    Exit
EndIf

; image

; We can also save the public key.
; There is no need to encrypt public keys.
$oPubKey = ObjCreate("Chilkat.PublicKey")
$oPrivKey.ToPublicKey($oPubKey)

$sPath = "rsaKeys/myTestRsaPublic.pem"
; Choose PKCS1 or PKCS8
; We'll choose PKCS8.
Local $bPreferPkcs1 = False
$bSuccess = $oPubKey.SavePemFile($bPreferPkcs1,$sPath)
If ($bSuccess = False) Then
    ConsoleWrite($oPubKey.LastErrorText & @CRLF)
    Exit
EndIf

; image

ConsoleWrite("Success." & @CRLF)