Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
54
Task/Chaocipher/Wren/chaocipher.wren
Normal file
54
Task/Chaocipher/Wren/chaocipher.wren
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
class Chao {
|
||||
static encrypt { 0 }
|
||||
static decrypt { 1 }
|
||||
|
||||
static exec(text, mode, showSteps) {
|
||||
var len = text.count
|
||||
if (len != text.bytes.count) Fiber.abort("Text contains non-ASCII characters.")
|
||||
var left = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
|
||||
var right = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
|
||||
var eText = List.filled(len, "")
|
||||
var temp = List.filled(26, "")
|
||||
for (i in 0...len) {
|
||||
if (showSteps) System.print("%(left) %(right)")
|
||||
var index
|
||||
if (mode == Chao.encrypt) {
|
||||
index = right.indexOf(text[i])
|
||||
eText[i] = left[index]
|
||||
} else {
|
||||
index = left.indexOf(text[i])
|
||||
eText[i] = right[index]
|
||||
}
|
||||
if (i == len - 1) break
|
||||
|
||||
// permute left
|
||||
for (j in index..25) temp[j-index] = left[j]
|
||||
for (j in 0...index) temp[26-index+j] = left[j]
|
||||
var store = temp[1]
|
||||
for (j in 2..13) temp[j-1] = temp[j]
|
||||
temp[13] = store
|
||||
left = temp.join()
|
||||
|
||||
// permute right
|
||||
for (j in index..25) temp[j-index] = right[j]
|
||||
for (j in 0...index) temp[26-index+j] = right[j]
|
||||
store = temp[0]
|
||||
for (j in 1..25) temp[j-1] = temp[j]
|
||||
temp[25] = store
|
||||
store = temp[2]
|
||||
for (j in 3..13) temp[j-1] = temp[j]
|
||||
temp[13] = store
|
||||
right = temp.join()
|
||||
}
|
||||
return eText.join()
|
||||
}
|
||||
}
|
||||
|
||||
var plainText = "WELLDONEISBETTERTHANWELLSAID"
|
||||
System.print("The original plaintext is : %(plainText)")
|
||||
System.write("\nThe left and right alphabets after each permutation ")
|
||||
System.print("during encryption are :\n")
|
||||
var cipherText = Chao.exec(plainText, Chao.encrypt, true)
|
||||
System.print("\nThe ciphertext is : %(cipherText)")
|
||||
var plainText2 = Chao.exec(cipherText, Chao.decrypt, false)
|
||||
System.print("\nThe recovered plaintext is : %(plainText2)")
|
||||
Loading…
Add table
Add a link
Reference in a new issue