5
$\begingroup$

I am attempting to decrypt a ciphertext. I split the cipher into blocks of five, and now I want to permute each individual block. My goal is to be able to permute each individual block by the same permutation and then combine the list into one string which I will attempt to decrypt from there. I am not asking for assistance on decrypting the ciphertext, but I do need help learning how to permute this. Here is what I am currently trying, and, as you can see, I am permuting the blocks in the list rather than the elements in the blocks.

What I am trying now

$\endgroup$
  • 3
    $\begingroup$ Could you post it as code we can copy. $\endgroup$ – flinty Jun 25 at 23:25
3
$\begingroup$
stringPermute[str_?StringQ, perm_] := StringJoin@Permute[Characters@str, perm]

stringPermute["JZGRV", FindPermutation[{1, 3, 2, 4, 5}]]

perm = FindPermutation[{1, 3, 2, 4, 5}];
stringPermute[#,perm] /@ {"XYZWQ", "ABCDE", "FJRCT"}
| improve this answer | |
$\endgroup$
2
$\begingroup$

You could split the strings into lists of characters and apply the permutation (or its inverse) by just reading it off. Afterwards, you can create the strings by mapping StringJoin:

p = {1, 3, 2, 4, 5};
q = InversePermutation[p];
StringJoin /@ Characters[{"ABCDG", "EFGHI", "JKLMN"}][[All, p]]
StringJoin /@ Characters[{"ABCDG", "EFGHI", "JKLMN"}][[All, q]]

{"ACBDG", "EGFHI", "JLKMN"}

{"ACBDG", "EGFHI", "JLKMN"}

| improve this answer | |
$\endgroup$
  • $\begingroup$ InversePermutation[{1,3,2,4,5}] returns {1,3,2,4,5} which is the same - i.e self inverse, but your outputs are the same. $\endgroup$ – flinty Jun 26 at 11:49
  • $\begingroup$ Of course: p is an involution so it has the same effect as its inverse. $\endgroup$ – Henrik Schumacher Jun 26 at 12:01
  • $\begingroup$ Thanks I got confused why you were showing the inverse acting on ABCDG, when I thought you were trying to undo the permutation on ACBDG. $\endgroup$ – flinty Jun 26 at 12:15

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.