-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaocipher.tcl
More file actions
68 lines (56 loc) · 1.86 KB
/
Copy pathchaocipher.tcl
File metadata and controls
68 lines (56 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env tclsh
proc rotate_left_alphabet {alphabet index} {
set rotated [string range $alphabet $index end][string range $alphabet 0 [expr {$index - 1}]]
set store [string index $rotated 1]
set part1 [string range $rotated 2 13]
set part2 [string range $rotated 14 end]
return "[string index $rotated 0]$part1$store$part2"
}
proc rotate_right_alphabet {alphabet index} {
set rotated [string range $alphabet $index end][string range $alphabet 0 [expr {$index - 1}]]
set rotated [string range $rotated 1 end][string index $rotated 0]
set store [string index $rotated 2]
set part1 [string range $rotated 3 13]
set part2 [string range $rotated 14 end]
return "[string range $rotated 0 1]$part1$store$part2"
}
proc chaocipher {text mode} {
set lAlphabet "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
set rAlphabet "PTLNBQDEOYSFAVZKGJRIHWXUMC"
set output ""
set text [string toupper $text]
foreach c [split $text ""] {
if {![string is alpha $c]} {
append output $c
continue
}
if {$mode eq "encrypt"} {
set index [string first $c $rAlphabet]
set mapped [string index $lAlphabet $index]
} else {
set index [string first $c $lAlphabet]
set mapped [string index $rAlphabet $index]
}
append output $mapped
set lAlphabet [rotate_left_alphabet $lAlphabet $index]
set rAlphabet [rotate_right_alphabet $rAlphabet $index]
}
return $output
}
# Argumentos
set decrypt 0
if {[llength $argv] > 0 && [lindex $argv 0] eq "-d"} {
set decrypt 1
}
# Leitura da entrada
set inputText ""
while {[gets stdin line] >= 0} {
append inputText "$line"
}
# Execução
set inputText [string trim $inputText]
if {$decrypt} {
puts [chaocipher $inputText "decrypt"]
} else {
puts [chaocipher $inputText "encrypt"]
}