Decrypting with an RSA key (xml.Encryption.RSADecrypt)

When decrypting data with an RSA key, this example shows how to use a PEM key file or a key referenced from FGLPROFILE entries to perform the decryption.

The following examples show how to use the xml.Encryption.RSADecrypt method to decrypt a string using an RSA key.

Example 1: Using a PEM file path


IMPORT xml

DEFINE encryptedStr, decryptedStr STRING

MAIN
  LET encryptedStr = xml.Encryption.RSAEncrypt("/opt/local/cert-key.pem", "Mary had a little lamb")
  LET decryptedStr = xml.Encryption.RSADecrypt("/opt/local/cert-key.pem", encryptedStr)
  DISPLAY "Encrypted string: ", encryptedStr
  DISPLAY "Decrypted string: ", decryptedStr
END MAIN

Example 2: Using an FGLPROFILE entry

Assuming your FGLPROFILE contains:
# fglprofile
xml.myRSA.key = "/opt/local/cert-key.pem"
Ensure your FGLPROFILE environment variable points to the correct fglprofile file.
The code:

IMPORT xml

DEFINE encryptedStr, decryptedStr STRING
MAIN
  LET encryptedStr = xml.Encryption.RSAEncrypt("myRSA", "Mary had a little lamb")
  LET decryptedStr = xml.Encryption.RSADecrypt("myRSA", encryptedStr)
  DISPLAY "Encrypted string: ", encryptedStr
  DISPLAY "Decrypted string: ", decryptedStr
END MAIN

In both cases, the first argument to xml.Encryption.RSAEncrypt and xml.Encryption.RSADecrypt can be a file path or a logical key name defined in the FGLPROFILE.

Tip:

You can also use The fglpass tool (fglpass -e) to encrypt a plaintext password to RSA/BASE64 outside the application (for example, when preparing FGLPROFILE entries). For usage and examples, go to Decrypt a password from BASE64 (fglpass)