Xbase++ Requires Chilkat v11.0.0+
Xbase++
RSA Encrypt and Decrypt Credit Card Numbers
See more RSA Examples
_LANGUAGE_ sample code to RSA public-key encrypt and decrypt credit card numbers. The RSA key is loaded from an unencrypted PKCS8 file. Chilkat provides many ways of setting the key -- loading from both encrypted and unencrypted PEM, PKCS8, DER, PVK, etc. Keys may be loaded from files or in-memory representations. (The RSA component also provides the ability to generate RSA keys.)Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRsa
LOCAL oPrivKey
LOCAL oPubKey
LOCAL cCcNumber
LOCAL nUsePrivateKey
LOCAL cEncryptedStr
LOCAL oRsaDecryptor
LOCAL cDecryptedStr
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oRsa := CreateObject("Chilkat.Rsa")
oPrivKey := CreateObject("Chilkat.PrivateKey")
// Load an RSA private key from a file:
nSuccess := oPrivKey:LoadAnyFormatFile("rsaPrivateKey.key", "")
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oRsa:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// Get the public part of the private key.
oPubKey := CreateObject("Chilkat.PublicKey")
oPrivKey:ToPublicKey(oPubKey)
nSuccess := oRsa:UsePublicKey(oPubKey)
IF (nSuccess == 0)
? oRsa:LastErrorText
oRsa:destroy()
oPrivKey:destroy()
oPubKey:destroy()
RETURN
ENDIF
// Encrypt a VISA credit card number:
// 1234-5678-0000-9999
cCcNumber := "1234567800009999"
nUsePrivateKey := 0
oRsa:EncodingMode := "base64"
cEncryptedStr := oRsa:EncryptStringENC(cCcNumber, nUsePrivateKey)
? "Encrypted:"
? cEncryptedStr
// Now decrypt:
oRsaDecryptor := CreateObject("Chilkat.Rsa")
oRsaDecryptor:EncodingMode := "base64"
oRsaDecryptor:UsePrivateKey(oPrivKey)
nUsePrivateKey := 1
cDecryptedStr := oRsaDecryptor:DecryptStringENC(cEncryptedStr, nUsePrivateKey)
? "Decrypted:"
? cDecryptedStr
// Important: RSA encryption should only be used to encrypt small amounts of data.
// It is typically used for encrypting symmetric encryption
// keys such that a symmetric encryption algorithm, such as
// AES is then used to encrypt/decrypt bulk data.
oRsa:destroy()
oPrivKey:destroy()
oPubKey:destroy()
oRsaDecryptor:destroy()