-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtcl.tcl
executable file
·211 lines (180 loc) · 5.89 KB
/
tcl.tcl
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/tclsh
set size_of_char 16
set msg "слава роботам"
set key "нашсуперключ."
puts "msg \n $msg"
puts "key \n $key"
proc splitter { size param {args {}} } {
# Разбивает входную последовательность =param=
# на подпоследовательности размером =size=.
# Если остается хвост, размером меньше size
# - сигнализирует ошибку"
set len [string length $param]
if {$len >= $size} {
set args [lindex $args 0]
lappend args [string range $param 0 $size-1]
splitter $size [string range $param $size end] $args
} else {
if { 0 == $len } {
return [lindex $args 0]
} else {
error $param
}
}
}
# puts [splitter 2 "sdfergergerg"] ;# => sd fe rg er ge rg
proc integer->bit-list { int {args {}} } {
# Преобразует входное число в список битов
if { $int > 0 } {
set args [lindex $args 0]
set i [expr $int / 2]
set r [expr $int % 2]
integer->bit-list $i [linsert $args 0 $r]
} else {
if { 0 == [llength [lindex $args 0]] } {
[linsert $args 0 $r]
} else {
return [lindex $args 0]
}
}
}
# puts [integer->bit-list 13] ;# => 1 1 0 1
proc string->bit-list { str } {
global size_of_char
set acc {}
foreach char [split $str ""] {
set char_code [scan $char %c]
set bit_list [integer->bit-list $char_code]
set append_cnt [expr { $size_of_char - [llength $bit_list]}]
set appender [split [string repeat "0" $append_cnt] ""]
set result [concat $appender $bit_list]
set acc [concat $acc $result]
}
return $acc
}
puts "msg bit-list \n [string->bit-list $msg]"
puts "key bit-list \n [string->bit-list $key]"
proc crypt { msg key } {
set idx 0;
set max [llength $msg]
set result {}
while { $idx < $max } {
set result [concat $result [expr [lindex $msg $idx] ^ [lindex $key $idx]]]
incr idx
}
return $result
}
puts "encrypted \n [crypt [string->bit-list $msg] [string->bit-list $key]]"
proc show { bit_list } {
set result {}
foreach item [splitter 4 [join $bit_list ""]] {
switch $item {
0000 { set res 0 }
0001 { set res 1 }
0010 { set res 2 }
0011 { set res 3 }
0100 { set res 4 }
0101 { set res 5 }
0110 { set res 6 }
0111 { set res 7 }
1000 { set res 8 }
1001 { set res 9 }
1010 { set res A }
1011 { set res B }
1100 { set res C }
1101 { set res D }
1110 { set res E }
1111 { set res F }
}
set result [concat $result $res]
}
return [join $result ""]
}
puts "show \n [show [crypt [string->bit-list $msg] [string->bit-list $key]]]"
proc unshow { hex_string } {
set result {}
foreach item [split $hex_string ""] {
switch $item {
0 { set res 0000 }
1 { set res 0001 }
2 { set res 0010 }
3 { set res 0011 }
4 { set res 0100 }
5 { set res 0101 }
6 { set res 0110 }
7 { set res 0111 }
8 { set res 1000 }
9 { set res 1001 }
A { set res 1010 }
B { set res 1011 }
C { set res 1100 }
D { set res 1101 }
E { set res 1110 }
F { set res 1111 }
}
set result [concat $result $res]
}
return [split [join $result ""] ""]
}
puts "unshow \n [unshow [show [crypt [string->bit-list $msg] [string->bit-list $key]]]]"
puts "decrypted bit-list \n [crypt [unshow [show [crypt [string->bit-list $msg] [string->bit-list $key]]]] [string->bit-list $key]]"
proc bit-list->integer { bit_list {args {}} } {
# Преобразует список битов в число
set acc 0
set idx 0
set max [expr [llength $bit_list] - 1]
while {$idx <= $max} {
set pos [expr $max - $idx]
if { 1 == [lindex $bit_list $pos] } {
set acc [expr $acc + { round(pow(2, $idx)) }]
}
incr idx
}
return $acc
}
# puts [bit-list->integer [integer->bit-list 11]] ;# => 11
proc bit-list->string { bit_list } {
global size_of_char
set acc {}
foreach code [splitter $size_of_char [join $bit_list ""]] {
set char [format "%c" [bit-list->integer [split $code ""]]]
if { 0 == [llength $char] } {
set acc [concat $acc {" "}]
} else {
set acc [concat $acc $char]
}
}
return [join $acc ""]
}
puts "decrypted \n [bit-list->string [crypt [unshow [show [crypt [string->bit-list $msg] [string->bit-list $key]]]] [string->bit-list $key]]]"
# ttk::notebook .nb -width 600 -height 400
# text .nb.t1 -width 40 -height 10
# text .nb.t2 -width 40 -height 10
# button .nb.b1 -text "Encrupt" -command {encrypt [.nb.t1 get 1.0 end] $key}
# button .nb.b2 -text "Decrypt" -command {decrypt}
# pack .nb -fill both -expand true
# pack .nb.t1 -fill both -expand true
# pack .nb.t2 -fill both -expand true
# pack .nb.b1 -side left
# pack .nb.b2 -side right
# .nb.t1 insert 1.0 "qwertyuiopцуа"
# package require ttk
# pack [ttk::notebook .nb]
# .nb add [frame .nb.f1] -text "First tab"
# .nb add [frame .nb.f2] -text "Second tab"
# .nb select .nb.f2
# ttk::notebook::enableTraversal .nb
# button .btn -text "Ok" -command {set res [expr 2+2]; puts "Результат = $res";}
# pack .btn
proc btn {} {
puts "btn pressed"
}
package require Tk
ttk::notebook .n -width 600 -height 400
text .n.t1 -width 600 -height 10
text .n.t2 -width 600 -height 10
button .n.b -text "Encrypt" -command btn
pack .n
pack .n.t1 -fill both -expand t
pack .n.t2 -fill both -expand t
pack .n.b -fill both -expand t