I am trying to decrypt a modified Vigenère cipher text. I am unfamiliar with Mathematica, so I need some guidance. Here is what I want:

modifiedVigenere[ciphertext_, keylength_]:= *insert code here*

Here is what I want the code to do. First, convert the ciphertext to numbers mod 26. I already have a command which will do this.

stringToNumbers[string_] := ToCharacterCode[StringReplace[ToUpperCase[string], 
  RegularExpression["[^A-Z]"] -> ""]] - 65

Then I want to modify the numbers based on the keylength. Let's say the string changes to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. Then, let's say the keylength is 3. I want to subtract 0 from the first three entries in the list, 1 from the second three, 2 from the third three, and then 3 from the final three (in this case, only the number 10). The resulting string would look like this: {1, 2, 3, 3, 4, 5, 5, 6, 7, 7}.

I would then change the numbers back into a string with the following command:

numbersToString[list_] := 
    Select[list, Function[x, IntegerQ[x] && x >= 0 && x <26]] + 65

If anyone could help me, that would be greatly appreciated.

  • 2
    $\begingroup$ Instead of stringToNumbers and numbersToString you can also use the functions LetterNumber and FromLetterNumber. $\endgroup$
    – A.Z.
    Jun 25 '20 at 20:07

Do you want something like this?

modifiedVigenere[ciphertext_, keylength_] := StringJoin[
        ] - Floor[
                StringLength[ciphertext] - 1

This produces

modifiedVigenere["abcdefghij", 3]  (* "abccdeefgg" *)
  • $\begingroup$ Yes, that works. Thank you so much! $\endgroup$ Jun 25 '20 at 23:01
  • 1
    $\begingroup$ @NathanielLackey if you find an answer valuable & useful, it’s always better to make that checkmark turn green by accepting the answer :) not only does it provide others in the future a nice and easy way to see if a problem is solved or not, it gives the answering party a good bit of points ;D $\endgroup$ Jun 26 '20 at 14:45

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.