Cryptography Reference
In-Depth Information
> bytestofile(convert("This is a file to test the implementation of the
Pseudo-Random One-Time Pad", bytes), "testfile");
75 bytes saved to testfile
> POTPF(k, "testfile", "ctestfile");
75 bytes saved to ctestfile
> POTPF(k, "ctestfile", "dtestfile");
75 bytes saved to dtestfile
> convert(filetobytes("dtestfile"), bytes);
"This is a file to test the implementation of the Pseudo-Random One-Time Pad"
Exercise 3.16 Write a Maple function that integrates POTP and POTPF and permits
the encryption and decryption of messages given either as text strings or as files.
Now, with a similar approach, we can give the encryption/decryption function for
the one-time pad. When we discussed the one-time pad in Sect. 3.2 , the length of the
messages was fixed. Here we deal with variable length messages but we can fix a
message length and then encrypt any message of smaller length by first padding it
(for example, with '0' bytes) until it has the required length and then proceeding to
encrypt it as before. This way, an adversary would not be able to know the length of
the plaintext.
The one-time pad function is OTP below. The inputs are similar to those in POTP
but this function is actually simpler because the key must be at least as long as the
message and randomly generated. Thus the key has to be externally generated and
then supplied to the function and hence the pseudo-random generator is not used.
> OTP := proc(key::{posint, string}, message::string, action::name := 'encrypt')
local k, l, m, blist;
uses StringTools;
if action = 'encrypt' then
l := Length(message);
m := convert(message, bytes)
elif action = 'decrypt' then
if not IsHexDigit(message) then
error "Ciphertext must be a hex string"
end if;
m := hexstringtobytes(message);
l := nops(m)
else error "Unrecognized action"
end if;
if type(key, string) then
if not IsHexDigit(key) then
error "The key must be either a hex string or an integer"
end if;
k := hexstringtobytes(key)
else
k := convert(key, base, 256)
end if;
if nops(k) < l then
error "The key length is less than the message length"
end if;
m := zip(BitXor, m, k);
if action = 'encrypt' then
bytestohexstring(m)
else
convert(m, bytes)
end if
end proc:
Search WWH ::




Custom Search