Tcl
Tcl
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 Tcl Downloads
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]
set privKey [new_CkPrivateKey]
# Load an RSA private key from a file:
set success [CkPrivateKey_LoadAnyFormatFile $privKey "rsaPrivateKey.key" ""]
if {$success == 0} then {
puts [CkPrivateKey_lastErrorText $privKey]
delete_CkRsa $rsa
delete_CkPrivateKey $privKey
exit
}
# Get the public part of the private key.
set pubKey [new_CkPublicKey]
CkPrivateKey_ToPublicKey $privKey $pubKey
set success [CkRsa_UsePublicKey $rsa $pubKey]
if {$success == 0} then {
puts [CkRsa_lastErrorText $rsa]
delete_CkRsa $rsa
delete_CkPrivateKey $privKey
delete_CkPublicKey $pubKey
exit
}
# Encrypt a VISA credit card number:
# 1234-5678-0000-9999
set ccNumber "1234567800009999"
set usePrivateKey 0
CkRsa_put_EncodingMode $rsa "base64"
set encryptedStr [CkRsa_encryptStringENC $rsa $ccNumber $usePrivateKey]
puts "Encrypted:"
puts "$encryptedStr"
# Now decrypt:
set rsaDecryptor [new_CkRsa]
CkRsa_put_EncodingMode $rsaDecryptor "base64"
CkRsa_UsePrivateKey $rsaDecryptor $privKey
set usePrivateKey 1
set decryptedStr [CkRsa_decryptStringENC $rsaDecryptor $encryptedStr $usePrivateKey]
puts "Decrypted:"
puts "$decryptedStr"
# 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
delete_CkRsa $rsa
delete_CkPrivateKey $privKey
delete_CkPublicKey $pubKey
delete_CkRsa $rsaDecryptor