Perform AES symmetric encryption.
Syntax:
aes(input,key,iv;charset)
Note:
The function uses AES symmetric encryption algorithm to encrypt/decrypt parameter input.
key is the access key. Initiation vector parameter iv is only needed when CBC encryption mode is used; use ECB encryption mode by default when @c option representing the CBC mode is absent.
Usually parameters input, key and iv are BLOB type; the function also supports automatic string encoding.
Both key and iv must have 16 bytes. Note that they must have 16 bytes after encoding if the to-be-encrypted value is a string.
Parameter:
input |
Data to be encrypted/decrypted. |
key |
Access key. |
iv |
Initiation vector. |
charset |
Character set, which is utf-8 by default; No need to set up this parameter when both input and key are BLOB type. |
Option:
@d |
Perform decryption. Values of parameters key, iv and charset should be same for encryption and decryption. |
@c |
Use CBC encryption mode; use the default mode when parameter iv is absent. |
@n |
Use NoPadding; in this case the number of bytes in input must be the integer multiple of 16, otherwise error will be reported. |
Return value:
BLOB value
Example:
Encryption and decryption:
|
A |
|
1 |
Hello World! |
|
2 |
v7wz+QhTtHkYmXl/ |
|
3 |
=charencode(A1; "GBK") |
Generate a BLOB value using A1’s string. |
4 |
=charencode(A2; "GBK") |
Generate a 16-byte BLOB value using A2’s string. |
5 |
=aes(A3,A4) |
Encrypt A3 using AES symmetric encryption algorithm while setting up A4 as the access key, and return a BLOB value. |
6 |
=aes@d(A5,A4) |
Use @d option to decrypt A5 – the access key must be consistent with that in A5’s encryption, and return a BLOB value. |
7 |
=charencode(A6;"GBK") |
Generate a string using A6’s BLOB value and return the result: Hello World! |
CBC encryption mode:
|
A |
|
1 |
Hello World! |
|
2 |
v7wz+QhTtHkYmXl/ |
|
3 |
abcdefghijkl+-$/ |
|
4 |
=aes@c(A1,A2,A3;"utf-8") |
@c option enables using the CBC encryption mode; A3 is the initiation vector and the character set is utf-8; both the encoded access key and initiation value must have 16 bytes. |
5 |
=aes@cd(A4,A2,A3) |
Decrypt A4 – access key and initiation vector must be consistent with those in A4’s encryption and use the default utf-8 as parameter charset is absent, and return a BLOB value. |
6 |
=charencode(A5;"utf-8") |
Generate a string using A5’s BLOB value and return the result: Hello World! |
NoPadding:
|
A |
|
1 |
Hello!! |
|
2 |
v7wz+Qh |
|
3 |
=charencode(A1;"unicode" ) |
Generate a 16-byte BLOB value using A1’s string. |
4 |
=charencode(A2;"unicode") |
Generate a 16-byte BLOB value using A2’s string. |
5 |
=aes@n(A3,A4) |
@n option enables NoPadding; in this case the number of bytes in parameter input must be the integer multiple of 16; return a BLOB value. |
6 |
=aes@nd(A5,A4) |
Decrypt A5. |
7 |
=charencode(A6;"unicode") |
Generate a string using A6’s BLOB value and return same result as A1. |