Sample code for 30+ languages & platforms
Tcl

Generate an RSA Key and Get as Base64 DER

See more RSA Examples

Demonstrates how to generate a 2048-bit RSA key and return the public and private parts as unencrypted Base64 encoded DER.

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.
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
}

# Get the public part of the key.
set pubKey [new_CkPublicKey]

CkPrivateKey_ToPublicKey $privKey $pubKey

# There are two possible formats for representing the RSA public key 
# in ASN.1 (DER).  The possible formats are PKCS1 and PKCS8.
# We can get either by setting bChoosePkcs1 to 1 or 0.
set bChoosePkcs1 1
set pubKeyBase64Der [CkPublicKey_getEncoded $pubKey $bChoosePkcs1 "base64"]
puts "Public Key Base64 DER:"
puts "$pubKeyBase64Der"

# Get the private key as Base64 DER:
# We can get PKCS1 or PKCS8, but with different methods:
set privKeyPkcs1 [CkPrivateKey_getPkcs1ENC $privKey "base64"]
puts "Private Key PKCS1 Base64 DER:"
puts "$privKeyPkcs1"

set privKeyPkcs8 [CkPrivateKey_getPkcs8ENC $privKey "base64"]
puts "Private Key PKCS8 Base64 DER:"
puts "$privKeyPkcs8"

delete_CkRsa $rsa
delete_CkPrivateKey $privKey
delete_CkPublicKey $pubKey