Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
69
Task/Chaocipher/Nim/chaocipher-1.nim
Normal file
69
Task/Chaocipher/Nim/chaocipher-1.nim
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import strformat
|
||||
|
||||
type
|
||||
Mode = enum
|
||||
Encrypt
|
||||
Decrypt
|
||||
|
||||
const lAlphabet: string = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
|
||||
const rAlphabet: string = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
|
||||
|
||||
proc chao(text: string, mode: Mode, verbose: bool = false): string =
|
||||
var left = lAlphabet
|
||||
var right = rAlphabet
|
||||
var eText = newSeq[char](text.len)
|
||||
var temp: array[26, char]
|
||||
|
||||
for i in 0..<text.len:
|
||||
if verbose:
|
||||
echo &"{left} {right}"
|
||||
var index: int
|
||||
if mode == Encrypt:
|
||||
index = right.find(text[i])
|
||||
eText[i] = left[index]
|
||||
else:
|
||||
index = left.find(text[i])
|
||||
eText[i] = right[index]
|
||||
if (i == text.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 = ""
|
||||
for i in temp:
|
||||
left &= $i
|
||||
|
||||
# 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 = ""
|
||||
for i in temp:
|
||||
right &= $i
|
||||
|
||||
for i in eText:
|
||||
result &= $i
|
||||
|
||||
var plainText = "WELLDONEISBETTERTHANWELLSAID"
|
||||
echo &"The original plaintext is: {plainText}"
|
||||
echo "\nThe left and right alphabets after each permutation during encryption are:\n"
|
||||
var cipherText = chao(plainText, Encrypt, true)
|
||||
echo &"\nThe ciphertext is: {cipherText}"
|
||||
var plainText2 = chao(cipherText, Decrypt, false)
|
||||
echo &"\nThe recovered plaintext is: {plainText2}"
|
||||
47
Task/Chaocipher/Nim/chaocipher-2.nim
Normal file
47
Task/Chaocipher/Nim/chaocipher-2.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import std/[algorithm, strutils]
|
||||
|
||||
type
|
||||
Mode = enum
|
||||
Encrypt
|
||||
Decrypt
|
||||
|
||||
const
|
||||
lAlphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
|
||||
rAlphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
|
||||
|
||||
proc chao(text: string; mode: Mode; verbose = false): string =
|
||||
var
|
||||
left = lAlphabet
|
||||
right = rAlphabet
|
||||
eText = newSeq[char](text.len)
|
||||
|
||||
for i in 0 ..< text.len:
|
||||
if verbose:
|
||||
echo left, " ", right
|
||||
var index: int
|
||||
if mode == Encrypt:
|
||||
index = right.find(text[i])
|
||||
eText[i] = left[index]
|
||||
else:
|
||||
index = left.find(text[i])
|
||||
eText[i] = right[index]
|
||||
if i == text.len - 1:
|
||||
break
|
||||
|
||||
# permute left
|
||||
left.rotateLeft(index)
|
||||
left.rotateLeft(1..13, 1)
|
||||
|
||||
# permute right
|
||||
right.rotateLeft(index + 1)
|
||||
right.rotateLeft(2..13, 1)
|
||||
|
||||
result = eText.join()
|
||||
|
||||
let plainText = "WELLDONEISBETTERTHANWELLSAID"
|
||||
echo "The original plaintext is: ", plainText
|
||||
echo "\nThe left and right alphabets after each permutation during encryption are:\n"
|
||||
let cipherText = chao(plainText, Encrypt, true)
|
||||
echo "\nThe ciphertext is: ", cipherText
|
||||
let plainText2 = chao(cipherText, Decrypt, false)
|
||||
echo "\nThe recovered plaintext is: ", plainText2
|
||||
Loading…
Add table
Add a link
Reference in a new issue