Player APIs (BrightScript & Ja...
...
Object Reference
Hashing and Storage Objects
roBlockCipher
5min
this object provides a means for symmetric block encryption it currently supports aes and cbc ciphers, at block sizes of 128, 192, or 256 bits the javascript equivalent is the node js crypto api object creation the roblockcipher object is created with an associative array representing a set of parameters createobject("roblockcipher", parameters as roassociativearray) the associative array should contain the following parameters mode "aes 128 cbc" , "aes 192 cbc" , or "aes 256 cbc" padding "zero" or "pkcs7" the object defaults to zero padding if this parameter is omitted padding is required for inputs that are not an exact multiple of the cipher block size specifying "zero" will add padding only when needed, while specifying "pkcs7" always adds padding, even if the data is already a multiple of the block size (in this case, an entire block will be added) pkcs#7 padding is automatically removed upon decryption, and zero padding will be retained since there are no means to unambiguously distinguish pad values from data ifblockcipher setiv(iv as object) as void sets the initialization vector (iv) for cbc (cipher block chaining) modes if the supplied iv is shorter than required, then it will be zero padded (passing an empty string will set the vector to all zeroes) the iv will typically contain arbitrary characters and be in the form of an robytearray , though it can also be a string encrypt(key as object, plaintext as object) as robytearray uses the specified key to encrypt the plaintext parameter, which can be passed as either a string or an robytearray decrypt(key as object, cipher text as object) as robytearray uses the specified key to decrypt cipher text, which should be passed as an robytearray because the cipher text is encrypted, it can contain any character example ' this is case#4 from rfc3602 key = createobject("robytearray") iv = createobject("robytearray") plain = createobject("robytearray") key fromhexstring("56e47a38c5598974bc46903dba290349") iv fromhexstring("8ce82eefbea0da3c44699ed7db51b7d9") plain fromhexstring("a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf") c = createobject("roblockcipher", { mode "aes 128 cbc" }) c setiv(iv) crypt = c encrypt(key, plain) result = crypt tohexstring() expected = ucase("c30e32ffedc0774e6aff6af0869f71aa0f3af07a9a31a9c684db207eb0ef8e4e35907aa632c3ffdf868bb7b29d3d46ad83ce9f9a102ee99d49a53e87f4c3da55") ' decrypt example to recover the encrypted data c setiv(iv) roundtrip = c decrypt(key, crypt) ' second example selecting pkcs#7 padding c = createobject("roblockcipher", { mode "aes 128 cbc", padding "pkcs7" })