(Ruby) Generate RSA Public/Private Key Pair and Export to PEM
Ruby example code showing how to generate an RSA public/private key pair and export to PEM files.
require 'chilkat'
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
rsa = Chilkat::CkRsa.new()
# Generate a 1024-bit key. Chilkat RSA supports
# key sizes ranging from 512 bits to 4096 bits.
success = rsa.GenerateKey(1024)
if (success != true)
print rsa.lastErrorText() + "\n";
exit
end
# Keys are exported in XML format:
publicKeyXml = rsa.exportPublicKey()
print publicKeyXml + "\n";
privateKeyXml = rsa.exportPrivateKey()
print privateKeyXml + "\n";
# Save the private key in PEM format:
privKey = Chilkat::CkPrivateKey.new()
success = privKey.LoadXml(privateKeyXml)
success = privKey.SavePemFile("privateKey.pem")
# Save the public key in PEM format:
pubKey = Chilkat::CkPublicKey.new()
success = pubKey.LoadFromString(publicKeyXml)
success = pubKey.SavePemFile(false,"publicKey.pem")
|