RosettaCodeData/Task/Chaocipher/EMal/chaocipher.emal

52 lines
2 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
type Chaocipher:Mode
enum
int ENCRYPT, DECRYPT
end
type Chaocipher
2024-10-16 18:07:41 -07:00
text L_ALPHABET ← "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
text R_ALPHABET ← "PTLNBQDEOYSFAVZKGJRIHWXUMC"
fun exec ← text by text value, Chaocipher:Mode mode, logic showSteps
2023-07-01 11:58:00 -04:00
^|since texts are mutable, we can operate directly on them without the need of Lists|^
2024-10-16 18:07:41 -07:00
text left ← *L_ALPHABET # by using the valueOf operator we are sure that the string is copied
text right ← *R_ALPHABET
text eText ← text(" ", value.length)
text temp ← text(" ", 26)
for int i ← 0; i < value.length; ++i
2023-07-01 11:58:00 -04:00
if showSteps do writeLine(left + " " + right) end
2024-10-16 18:07:41 -07:00
int index ← 0
if mode æ Chaocipher:Mode.ENCRYPT
index ← right.find(value[i])
eText[i] ← left[index]
2023-07-01 11:58:00 -04:00
else
2024-10-16 18:07:41 -07:00
index ← left.find(value[i])
eText[i] ← right[index]
2023-07-01 11:58:00 -04:00
end
2024-10-16 18:07:41 -07:00
if i æ value.length - 1 do break end
2023-07-01 11:58:00 -04:00
# permute left
2024-10-16 18:07:41 -07:00
for int j ← index; j < 26; ++j do temp[j - index] ← left[j] end
for int j ← 0; j < index; ++j do temp[26 - index + j] ← left[j] end
var store ← temp[1]
for int j ← 2; j < 14; ++j do temp[j - 1] ← temp[j] end
temp[13] ← store
left ← *temp
2023-07-01 11:58:00 -04:00
# permute right
2024-10-16 18:07:41 -07:00
for int j ← index; j < 26; ++j do temp[j - index] ← right[j] end
for int j ← 0; j < index; ++j do temp[26 - index + j] ← right[j] end
store ← temp[0]
for int j ← 1; j < 26; ++j do temp[j - 1] ← temp[j] end
temp[25] ← store
store ← temp[2]
for int j ← 3; j < 14; ++j do temp[j - 1] ← temp[j] end
temp[13] ← store
right ← *temp
2023-07-01 11:58:00 -04:00
end
return eText
end
2024-10-16 18:07:41 -07:00
var plainText ← "WELLDONEISBETTERTHANWELLSAID"
writeLine("The original plaintext is : ", plainText)
writeLine(EOL, "The left and right alphabets after each permutation during encryption are :", EOL)
var cipherText ← exec(plainText, Chaocipher:Mode.ENCRYPT, true)
writeLine(EOL, "The ciphertext is : ", cipherText)
var plainText2 ← exec(cipherText, Chaocipher:Mode.DECRYPT, false)
writeLine(EOL, "The recovered plaintext is : ", plainText2)