AES256decrypt and AES256encrypt (String functions)

From m204wiki
Revision as of 22:55, 11 March 2016 by JAL (talk | contribs) (add many details and example)
Jump to navigation Jump to search

AES256 encryption or decryption (String class)

[Introduced in Model 204 7.6]


AES256decrypt and AES256encrypt are synonyms for the String class function that applies the AES (Advanced Encryption Standard) using a 256-bit symmetric key to decrypt or encrypt each 128-bit block of input text. For more details about how the algorithm works, see Advanced Encryption Standard.

The length of the returned string is the same as that of the object string.

AES is a two-way cipher, so encrypting a string with a key and then decrypting the result of that encryption with the same key produces the original string. That is, the following assertion should always hold:

assert %string:aes256encrypt(%key):aes256decrypt(%key) eq %string

You can use AES256decrypt to "document" that you are decrypting, and use AES256encrypt to "document" that you are encrypting. You can use AES256decrypt and AES256encrypt interchangeably on the rest of this page.

Syntax

%outString = string:AES256decrypt( key) Throws UnsupportedCrypto, InvalidCryptoKey, InvalidCryptoData

%outString = string:AES256encrypt( key) Throws UnsupportedCrypto, InvalidCryptoKey, InvalidCryptoData

Syntax terms

%outString A string variable to receive the encrypted or decrypted method object string. Its length is the same as string.
string The string to which the method is applied. string must be a multiple of sixteen bytes in length; its maximum is 32768 bytes. It may be null.
key A 32-byte (only) string variable whose value is used to encrypt or decrypt the method object, string.

Usage notes

  • You are not prevented from creating confusion by encrypting with AES256decrypt and decrypting with AES256encrypt.
  • The AES256decrypt and AES256encrypt functions are available as of Model 204 7.6.

Examples

In the following example, the output string from the AES256encrypt method is converted to hex using the StringToHex function to reveal its non-displayable characters, then decrypted to return the original input string:

b %ls is longstring %string is longstring %key is string len 32 %string = 'onetwothreefour0onetwothreefour0' %key = '12345678161616162424242432323232' %ls = %string:aes256encrypt(%key) printText {~} is {%ls:stringTohex} printText {~} is: {%ls:aes256decrypt(%key)} end

The result is:

%ls:stringTohex is: 513D0D34ABD198699043708FB076EC71513D0D34ABD198699043708FB076EC71 %ls:aes256decrypt(%key) is: onetwothreefour0onetwothreefour0

See also