Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
loRsa = createobject("CkRsa")
// Generate a 2048-bit key.
loPrivKey = createobject("CkPrivateKey")
llSuccess = loRsa.GenKey(2048,loPrivKey)
if (llSuccess = .F.) then
? loRsa.LastErrorText
release loRsa
release loPrivKey
return
endif
lcPassword = "secret"
// Saving to a relative path (from the current working directory of the process).
lcPath = "rsaKeys/myTestRsaPrivate.pem"
// Encrypt the PEM using 256-bit AES encryption.
loPrivKey.Pkcs8EncryptAlg = "aes256"
llSuccess = loPrivKey.SavePkcs8EncryptedPemFile(lcPassword,lcPath)
if (llSuccess = .F.) then
? loPrivKey.LastErrorText
release loRsa
release loPrivKey
return
endif
//
// We can also save the public key.
// There is no need to encrypt public keys.
loPubKey = createobject("CkPublicKey")
loPrivKey.ToPublicKey(loPubKey)
lcPath = "rsaKeys/myTestRsaPublic.pem"
// Choose PKCS1 or PKCS8
// We'll choose PKCS8.
llPreferPkcs1 = .F.
llSuccess = loPubKey.SavePemFile(llPreferPkcs1,lcPath)
if (llSuccess = .F.) then
? loPubKey.LastErrorText
release loRsa
release loPrivKey
release loPubKey
return
endif
//
? "Success."
release loRsa
release loPrivKey
release loPubKey