Encrypting with an RSA key (xml.Encryption.RSAEncrypt)

When encrypting 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 encryption.

The following examples show how to use the xml.Encryption.RSAEncrypt method to encrypt 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 The fglpass tool (fglpass -e) to encrypt a plain text password as BASE64. For details, go to Encrypt a password from RSA key (fglpass) and Encrypt a HTTP authenticate password for FGLPROFILE