Cryptography Reference
In-Depth Information
4.4.2.4 Encryption and Decryption of Lists of Bytes
We are now going to give a Maple function to encrypt or decrypt a list of bytes such
as the one that can be obtained by reading a file from disk or by converting a text
message to the list of corresponding 8-bit ASCII codes. Since an AES block consists
of 16bytes, the length of the list must be a multiple of 16 and we will see later
how messages can be padded to produce an integral number of blocks. The input
is a key (given either as a list of bytes or as a hexadecimal string), a list of bytes
(as integers in the 0
255 range), and an optional name that can be either 'encrypt'
or 'decrypt' with the former as default. The output is the list of bytes corresponding
to the encrypted or decrypted input blocks. We warn the reader that 'encrypt' and
'decrypt' here are just shorthand for the application of the forward cipher function
and the inverse cipher function, respectively. As we have seen when discussing the
modes of operation, these functions are seldom applied directly to plaintexts in order
to obtain the corresponding ciphertexts (and, if they are, the resulting scheme is not
secure, as happens with ECB mode).
..
> AES := proc(key, bytelist::list, action::name:=encrypt)
local k, ek, states, len, i;
k := checkkey(key);
ek := KeyExpansion(k);
states := [ListTools:-LengthSplit(bytelist, 16)];
len := nops(states)-1;
states := Array(0..len, map(x -> Array(0..3, 0..3, (i,j) -> x[i+4*j+1]), states));
if action = encrypt then
for i from 0 to len do
states[i]:=AESEncrypt(ek, states[i])
end do;
elif action = decrypt then
for i from 0 to len do
states[i]:=AESDecrypt(ek, states[i])
end do;
else
error "encrypt or decrypt expected"
end if;
states := map(x -> ArrayTools:-Alias(x, [16]), states);
[seq(seq(states[i][j], j=1..16), i=0..len)]
end proc:
Example 4.2 Let us consider the 256-bit key and the 128-bit plaintext given in [73,
Example C.3]. The result of encrypting and decrypting the plaintext is:
> k := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f":
m := "00112233445566778899aabbccddeeff":
> c := bytestohexstring(AES(k, hexstringtobytes(m)));
"8ea2b7ca516745bfeafc49904b496089"
> bytestohexstring(AES(k, hexstringtobytes(c), decrypt));
"00112233445566778899aabbccddeeff"
 
Search WWH ::




Custom Search