This is my first non-trivial Mathematica notebook, which I wrote for a cryptology homework. It encrypts/decrypts strings using an affine cipher. Is the code readable/idiomatic? How could it be improved? (I'm using version 10, so I don't have the ModularInverse
function).
toNum[c_] := LetterNumber[c] - 1
toChar[n_] := Alphabet[][[n + 1]]
encryptAffine[m_, b_] := StringJoin[
toChar[
Mod[m toNum[#] + b, 26]
] & /@ Characters[#]
] &
decryptAffine[m_, b_] := StringJoin[
toChar[
Mod[
PowerMod[m, -1, 26]*(toNum[#] - b),
26
]
] & /@ Characters[#]
] &
decryptAffine[3, 7][encryptAffine[3, 7]["helloworld"]]
(* "helloworld" *)