decrypt()
The decrypt() function decrypts encrypted binary data using a specified cipher algorithm and key, and returns the result as binary.
Syntax
Parameters
CIPHER- A string in
algorithm/mode/paddingformat. If mode and padding are omitted and only the algorithm is specified, the default values for that algorithm are applied.
Refer to the Java Security Standard Algorithm Names documentation for available algorithms, modes, and paddings.
- AES/CBC/NoPadding (128)
- AES/CBC/PKCS5Padding (128)
- AES/ECB/NoPadding (128)
- AES/ECB/PKCS5Padding (128)
- AES/GCM/NoPadding (128)
- DES/CBC/NoPadding (56)
- DES/CBC/PKCS5Padding (56)
- DES/ECB/NoPadding (56)
- DES/ECB/PKCS5Padding (56)
- DESede/CBC/NoPadding (168)
- DESede/CBC/PKCS5Padding (168)
- DESede/ECB/NoPadding (168)
- DESede/ECB/PKCS5Padding (168)
- RSA/ECB/PKCS1Padding (1024, 2048)
- RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
- RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)
KEY- A binary key of the size required by the specified cipher algorithm. Key lengths by algorithm are as follows.
- AES: choose from 16 bytes (128-bit), 24 bytes (192-bit), or 32 bytes (256-bit)
- RSA: choose from 128 bytes (1024-bit) or 256 bytes (2048-bit)
DATA- The binary data to decrypt.
IV- A binary initialization vector (IV) required by operation modes such as CBC.
Description
The decrypt() function uses the Java Cipher class to decrypt binary data with the specified cipher algorithm and key, and returns the result as binary.
Returns null if KEY or DATA is null or not a binary type. If IV is specified, it must also be a binary type; otherwise null is returned. Returns null if an error occurs during decryption.
Error codes
| Error code | Description |
|---|---|
| 90650 | Fewer than 3 parameters were provided. |
| 90651 | An invalid cipher algorithm was specified. |
Usage examples
-
Decrypt data encrypted with the AES algorithm
json "{}" | eval key = frombase64("mRcOlK9V47rjVL/RBYQYRw=="), encrypted = frombase64("y7+NQQ9/9xGtbBq5pgBvCA=="), decrypted = decode(decrypt("AES", key, encrypted)) | # decrypted: hello, world! -
Decrypt with an IV specified in CBC mode
json "{}" | eval key = frombase64("mRcOlK9V47rjVL/RBYQYRw=="), iv = frombase64("AAAAAAAAAAAAAAAAAAAAAA=="), encrypted = frombase64("..."), decrypted = decrypt("AES/CBC/PKCS5Padding", key, encrypted, iv) -
KEYorDATAisnulljson "{}" | eval result = decrypt("AES", null, binary("data")) | # result: null
Compatibility
The decrypt() function has been available since before Sonar 4.0.