21 lines
842 B
Plaintext
21 lines
842 B
Plaintext
toNumPlTxt[s_] := FromDigits[ToCharacterCode[s], 256];
|
|
fromNumPlTxt[plTxt_] := FromCharacterCode[IntegerDigits[plTxt, 256]];
|
|
enc::longmess = "Message '``' is too long for n = ``.";
|
|
enc[n_, _, mess_] /;
|
|
toNumPlTxt[mess] >= n := (Message[enc::longmess, mess, n]; $Failed);
|
|
enc[n_, e_, mess_] := PowerMod[toNumPlTxt[mess], e, n];
|
|
dec[n_, d_, en_] := fromNumPlTxt[PowerMod[en, d, n]];
|
|
text = "The cake is a lie!";
|
|
n = 9516311845790656153499716760847001433441357;
|
|
e = 65537;
|
|
d = 5617843187844953170308463622230283376298685;
|
|
en = enc[n, e, text];
|
|
de = dec[n, d, en];
|
|
Print["Text: '" <> text <> "'"];
|
|
Print["n = " <> IntegerString[n]];
|
|
Print["e = " <> IntegerString[e]];
|
|
Print["d = " <> IntegerString[d]];
|
|
Print["Numeric plaintext: " <> IntegerString[toNumPlTxt[text]]];
|
|
Print["Encoded: " <> IntegerString[en]];
|
|
Print["Decoded: '" <> de <> "'"];
|