xxxxxxxxxx
rsa_decrypt_pkcs(pRsaKey,cEncryptedData) ---> return a string containing the decryption of cEncryptedData
xxxxxxxxxx
rsa_encrypt_pkcs(pRsaKey,cPlainData) ---> return a string containing the encryption of cPlainData
xxxxxxxxxx
/* encrypt a file using AES key and then encrypt the AES key using an RSA public key */
try
/* read Alice public key */
rsaPublicKeyPEM = Read("alice_public_key.pem")
rsaPublicKey = rsa_import_pem(rsaPublicKeyPEM)
/* encrypt file with random AES-128 key */
cData = Read ("secret_document.txt")
cKey = RandBytes(16)
cIV = RandBytes(16)
cEncryptedData = Encrypt(cData,cKey,cIV,"aes128")
/* encrypt the AES-128 key with the RSA public key */
cEncryptedKey = rsa_encrypt_pkcs(rsaPublicKey,cKey)
/* store IV, encrypted AES key and encrypted data in a file to be sent to Alice*/
Write("encrypted_document.enc", cIV + cEncryptedKey + cEncryptedData)
catch
See "Unexpected error occured: " + cCatchError + nl
done
xxxxxxxxxx
/* decrypt a file by first decrypting AES key that was used to encrypt it
* and then decrypt the whole content using the AES key
*/
try
/* read Alice private key */
rsaKeyPEM = Read("alice_private_key.pem")
rsaKey = rsa_import_pem(rsaKeyPEM)
/* calculate the modulus length */
rsaKeyParams = rsa_export_params(rsaKey)
modulusLen = rsaKeyParams[:bits]/ 8
/* read encrypted file */
cEncryptedContent = Read ("encrypted_document.enc")
/* IV is the first 16 bytes if the file */
cIV = substr(cEncryptedContent, 1, 16)
/* encrypted key follows IV and its length is modulusLen */
cEncryptedKey = substr(cEncryptedContent, 17, modulusLen)
/* encrypted data follows the key */
cEncryptedData = substr(cEncryptedContent, 17 + modulusLen)
/* decrypt the AES-128 key */
cKey = rsa_decrypt_pkcs(rsaKey,cEncryptedKey)
/* decrypt the data using the AES-128 key */
cPlainData = Decrypt(cEncryptedData,cKey,cIV,"aes128")
/* store the decrypted data to a file */
Write("decrypted_document.txt", cPlainData)
catch
See "Unexpected error occured: " + cCatchError + nl
done
xxxxxxxxxx
rsa_encrypt_oaep(pRsaKey,cPlainData[,nHashAlgorithm]) ---> return a string containing the OAEP encryption of cPlainData
nHashAlgorithm indicates the hash algorithm to use for OAEP padding. If omited, SHA-1 is used by default.
Possible values for nHashAlgorithm argument are:
- $OSSL_HASH_MD5 which is equal to 0
- $OSSL_HASH_SHA1 which is equal to 1
- $OSSL_HASH_SHA256 which is equal to 2
- $OSSL_HASH_SHA384 which is equal to 3
- $OSSL_HASH_SHA512 which is equal to 4
xxxxxxxxxx
/* encrypt a file using AES key and then encrypt the AES key using an RSA public key using OAEP padding */
try
/* read Alice public key */
rsaPublicKeyPEM = Read("alice_public_key.pem")
rsaPublicKey = rsa_import_pem(rsaPublicKeyPEM)
/* encrypt file with random AES-128 key */
cData = Read ("secret_document.txt")
cKey = RandBytes(16)
cIV = RandBytes(16)
cEncryptedData = Encrypt(cData,cKey,cIV,"aes128")
/* encrypt the AES-128 key with the RSA public key */
cEncryptedKey = rsa_encrypt_oaep(rsaPublicKey,cKey)
/* store IV, encrypted AES key and encrypted data in a file to be sent to Alice*/
Write("oaep_encrypted_document.enc", cIV + cEncryptedKey + cEncryptedData)
catch
See "Unexpected error occured: " + cCatchError + nl
done
xxxxxxxxxx
rsa_decrypt_oaep(pRsaKey,cEncryptedData[,nHashAlgorithm]) ---> return a string containing the decryption of cEncryptedData
nHashAlgorithm indicates the hash algorithm to use for OAEP padding. If omited, SHA-1 is used by default.
Possible values for nHashAlgorithm argument are:
- $OSSL_HASH_MD5 which is equal to 0
- $OSSL_HASH_SHA1 which is equal to 1
- $OSSL_HASH_SHA256 which is equal to 2
- $OSSL_HASH_SHA384 which is equal to 3
- $OSSL_HASH_SHA512 which is equal to 4
xxxxxxxxxx
/* decrypt a file by first decrypting AES key that was used to encrypt it
* and then decrypt the whole content using the AES key
*/
try
/* read Alice private key */
rsaKeyPEM = Read("alice_private_key.pem")
rsaKey = rsa_import_pem(rsaKeyPEM)
/* calculate the modulus length */
rsaKeyParams = rsa_export_params(rsaKey)
modulusLen = rsaKeyParams[:bits]/ 8
/* read encrypted file */
cEncryptedContent = Read ("oaep_encrypted_document.enc")
/* IV is the first 16 bytes if the file */
cIV = substr(cEncryptedContent, 1, 16)
/* encrypted key follows IV and its length is modulusLen */
cEncryptedKey = substr(cEncryptedContent, 17, modulusLen)
/* encrypted data follows the key */
cEncryptedData = substr(cEncryptedContent, 17 + modulusLen)
/* decrypt the AES-128 key */
cKey = rsa_decrypt_oaep(rsaKey,cEncryptedKey)
/* decrypt the data using the AES-128 key */
cPlainData = Decrypt(cEncryptedData,cKey,cIV,"aes128")
/* store the decrypted data to a file */
Write("oaep_decrypted_document.txt", cPlainData)
catch
See "Unexpected error occured: " + cCatchError + nl
done