0
$\begingroup$

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_] := 
  FromCharacterCode[
    Select[list, Function[x, IntegerQ[x] && x >= 0 && x <26]] + 65
  ]

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

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

Do you want something like this?

modifiedVigenere[ciphertext_, keylength_] := StringJoin[
    FromLetterNumber[
        LetterNumber[
            ciphertext
        ] - Floor[
            Range[
                0,
                StringLength[ciphertext] - 1
            ]/keylength
        ]
    ]
]

This produces

modifiedVigenere["abcdefghij", 3]  (* "abccdeefgg" *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Yes, that works. Thank you so much! $\endgroup$ – Nathaniel Lackey Jun 25 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$ – CA Trevillian Jun 26 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.