Sample code for 30+ languages & platforms
Tcl

Generate RSA Public/Private Key Pair and Export to PEM

See more RSA Examples

_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set rsa [new_CkRsa]

# Generate a 2048-bit key.  Chilkat RSA supports
# key sizes ranging from 512 bits to 8192 bits.
set privKey [new_CkPrivateKey]

set success [CkRsa_GenKey $rsa 2048 $privKey]
if {$success == 0} then {
    puts [CkRsa_lastErrorText $rsa]
    delete_CkRsa $rsa
    delete_CkPrivateKey $privKey
    exit
}

set pubKey [new_CkPublicKey]

CkPrivateKey_ToPublicKey $privKey $pubKey

# Save the private key in PEM format:
set success [CkPrivateKey_SavePemFile $privKey "privateKey.pem"]
if {$success == 0} then {
    puts [CkPrivateKey_lastErrorText $privKey]
    delete_CkRsa $rsa
    delete_CkPrivateKey $privKey
    delete_CkPublicKey $pubKey
    exit
}

# Save the public key in PEM format:
set success [CkPublicKey_SavePemFile $pubKey 0 "publicKey.pem"]
if {$success == 0} then {
    puts [CkPublicKey_lastErrorText $pubKey]
    delete_CkRsa $rsa
    delete_CkPrivateKey $privKey
    delete_CkPublicKey $pubKey
    exit
}

puts "Success."

delete_CkRsa $rsa
delete_CkPrivateKey $privKey
delete_CkPublicKey $pubKey