3
$\begingroup$

Can someone help me with putting what I can say in words into code. I want to create autokey encrypt and decrypt functions (just wanting help encrypting and then can figure out decrypting). Everything is in Z 26.

The function would begin like:

autokeyEncrypt(x_, key_) :=

x is plaintext (e.g. rendezvous) and k is the key (e.g. 8), I first would need to convert the plaintext into a sequence of integers. I have a function to do this already that is used like:

stringToNumbers[x]

this would give

{17, 4, 13, 3, 4, 25, 21, 14, 20, 18}

Call this list xlist. The keystream then would be

{8, 17, 4, 13, 3, 4, 25, 21, 14, 20} 

That is, the key argument prepended to xlist. Now form xlist[[i]] + keystrean[[i]] , reducing mod 26, and the resulting ciphertext (as numbers then letters) would be:

{25, 21, 17, 15, 7, 3, 20, 9, 8, 12}
ZVRQHDUJIM

I am familiar with java and wondering if there is a way to accomplish something with the structure similiar to

autokeyEncrypt(x, key) {
   x = stringToNumbers(x) 
   create list with just the key in it
   loop to take numbers from x and fill list (until reaches specific length)
   etc
$\endgroup$
  • $\begingroup$ You dropped the value 18 in the example above. I don't know if this was intentional; I shall answer as if it were not. $\endgroup$ – Mr.Wizard Feb 4 '15 at 8:53
  • $\begingroup$ Perhaps I don't understand what you mean by "add the elements, reducing mod 26" as I get a different result: {8, 25, 3, 16, 19, 23, 22, 17, 5, 25, 17}. I'll use that until further clarification. $\endgroup$ – Mr.Wizard Feb 4 '15 at 8:58
  • $\begingroup$ In your pseudo-code you say "until reaches specific length" -- where is that specified? $\endgroup$ – Mr.Wizard Feb 4 '15 at 9:01
3
$\begingroup$

Here is an implementation that reads almost like a literal translation of the problem statement:

stringToNumbers[x_] := ToCharacterCode @ ToUpperCase @ x - 65

autokeyEncrypt[x_String, key_Integer] :=
  Module[{xlist, keystream}
  , xlist = stringToNumbers[x]
  ; keystream = Most @ Prepend[xlist, key]
  ; FromCharacterCode[Mod[xlist + keystream, 26] + 65]
  ]

autokeyEncrypt["rendezvous", 8]
(* "ZVRQHDUJIM" *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.