Cryptography Reference
In-Depth Information
obtaining that, as expected, the lists are identical. This means that the decrypted file
is identical to the original one.
> AESFileDecrypt(k192, "ctestfile", "dtestfile");
evalb(filetobytes("dtestfile") = testlist);
16381 bytes saved to dtestfile
true
Exercise 4.13 In a similar way to the preceding example, generate a test file whose
size in bytes is not a multiple of the block size, encrypt it in OFB mode, decrypt it,
and check that the decrypted file coincides with the original one.
4.5.2.2 Encrypting and Decrypting Text Strings
We are now going to provide encryption and decryption functions for text strings. As
on other occasions, we will use the ASCII character set (ISO Latin 1) as alphabet for
plaintexts (of course, only printable characters will usually appear in plaintexts) but
the ciphertext alphabet will be made out of the lower-case hexadecimal characters
since using the ASCII character set would produce non-printable ciphertexts and,
moreover, the null character—the character with 0ASCII code, which could appear in
a ciphertext with the same chance as any other character—would have to be treated
as a separate case (the 0 ASCII code is assigned to the empty string and not to a
specific character and, when a list of integers in the 0
..
255 range is converted to the
corresponding string, if the list contains a zero value, the string is truncated at that
point). Bearing this in mind, the function that encrypts text strings is the following:
> AESTextEncrypt := proc(key, plaintext::string, mode::name:=CTR, {iv:=selectiv()})
local k, t;
k := checkkey(key);
t := convert(plaintext, bytes);
bytestohexstring(AESModes(k, t, mode, iv))
end proc:
The decryption function is similar:
> AESTextDecrypt := proc(key::{list, string}, ciphertext::string, mode::name:=CTR)
local k, t;
k := checkkey(key);
t := AESModes(k, hexstringtobytes(ciphertext), mode);
convert(t, bytes)
end proc:
Example 4.9 Consider the following text string:
> frc := "Friends, Romans, countrymen, lend me your ears; I come to bury Caesar,
not to praise him.":
We are going to encrypt this string in OFBmode with the following (already used)
key and IV:
> k256 := "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4";
IV := "2ed2fee62859e07f10e5569310e633a6"
Search WWH ::




Custom Search