-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathde_crypt_block1.tcl
168 lines (137 loc) · 3.6 KB
/
de_crypt_block1.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
# de_crypt_block1.tcl :
# ֌, 2004 year
global rseed ;
set rseed 0 ;
proc UIPB__random { } {
global rseed
set rseed [ expr int($rseed) ] ;
set rseed [ expr abs((37 * $rseed + 67) % 27) ] ;
return $rseed ;
}
## proc for de_crypt
proc UIPB__decrypt { src_file dst_file } {
global rseed
set dc [ open $dst_file "w" ] ;
set src [ open $src_file "r" ] ;
set rseedint 0 ; ## number coding
set k -1 ;
while { ![ eof $src ] } {
incr k;
gets $src line ;
if {[ eof $src ]} { break; }
set l [ string length $line ]
set c '' ;
if {$k} {
set v4 [ UIPB__random ];
set line1 [ string range $line $v4 end ]
set s1 [ string length $line1 ] ;
set s2 [ UIPB__random ] ;
set sl [ expr ( $s1 - $s2 ) ];
set line2 "";
for { set i 0 } { $i<$sl } { incr i } {
set j [ expr ($sl - $i - 1) ] ;
set c [ string index $line1 $j ] ;
set c [ expr 158 - [ scan $c "%c" ] ];
set c [ format "%c" $c ]
append line2 $c ;
}
;#if {$sl>0} { set sl [ expr $sl - 1 ] ; }
set line2 [ string range $line2 0 $sl ]
;# append line2 "\n\0" ;
puts $dc $line2
} else {
set cseed "" ;
set rseed 0 ;
set sl [ string length $line ] ;
for { set i 0 } { $i<$sl } { incr i } {
set c [ string index $line $i ] ;
set c [ expr 158 - [ scan $c "%c" ] ];
append cseed [ format "%c" $c ] ;
}
set rseed [ expr ceil($cseed) ] ;
set rseedint [ expr int($rseed) ] ;
}
}
close $src
flush $dc
close $dc
puts "\n First number N=$rseedint \n $src_file -> $dst_file = done \n" ;
return 0;
}
## proc for crypt
proc UIPB__crypt { rnd src_file dst_file } {
global rseed
set dc [ open $dst_file "w" ] ;
set src [ open $src_file "r" ] ;
set rseed $rnd ;
set line "$rseed" ;
set cseed "" ;
set sl [ string length $line ] ;
for { set i 0 } { $i<$sl } { incr i } {
set c [ string index $line $i ] ;
set c [ expr 158 - [ scan $c "%c" ] ];
append cseed [ format "%c" $c ] ;
}
puts $dc $cseed ;
while { ![ eof $src ] } {
gets $src line ;
if {[ eof $src ]} { break; }
set l [ string length $line ]
set c '' ;
set sl $l
set line1 $line
set line2 "";
for { set i 0 } { $i<$sl } { incr i } {
set j [ expr ($sl - $i - 1) ] ;
set c [ string index $line1 $j ] ;
set c [ expr 158 - [ scan $c "%c" ] ];
set c [ format "%c" $c ]
append line2 $c ;
}
set line2 [ string range $line2 0 $sl ]
set f1 [ UIPB__random ];
set str1 ""
for { set i 0 } { $i<$f1 } { incr i } {
append str1 "0" ;
}
set f2 [ UIPB__random ];
set str2 ""
for { set i 0 } { $i<$f2 } { incr i } {
append str2 "0" ;
}
puts $dc "${str1}${line2}${str2}"
}
close $src
flush $dc
close $dc
puts "\n First number N=$rnd \n $src_file -> $dst_file = done \n" ;
return 0;
}
#===========================
proc main { } {
#===========================
set rnd 99 ;
set file_name1 "file_name1"
set file_name2 "file_name2"
## Crypt
## UIPB__crypt $rnd $file_name1 $file_name2 ;
## DeCrypt
## UIPB__decrypt $file_name1 $file_name2 ;
return 0;
}
set script [info script]
if {$script != {}} {
set dir [file dirname $script]
} else {
set dir [pwd]
}
set ext ".tcl"
foreach fn [glob -nocomplain -directory $dir *.txt] {
set tclFile [file rootname $fn]$ext
if {![file exists $tclFile]} {
puts "UIPB__decrypt $fn -> $tclFile" ;
UIPB__decrypt $fn $tclFile ;
} else {
puts " ------ $tclFile - exists" ;
}
}