Page 1 of 1
String encrytion and decryption
Posted: 08 Dec 2011 02:17
by CodedZyntaX
Hi Guys,
What will be the best code to encrypt and decrypt a string even if it contains a special characters? Such as !@#$%^*()_+" and etc.?
CodedZyntaX
Re: String encrytion and decryption
Posted: 09 Dec 2011 03:25
by orange_batch
It's hard to say. It's best if your strings never contain quotation marks, then you can freely use " and " surrounding your text to deactivate special character interpretation.
But even then you will have problems with exclamation marks if delayed expansion is enabled (but you can selectively enable and disable it when required). You will also have problems with % and ^ characters, maybe more...
Re: String encrytion and decryption
Posted: 09 Dec 2011 21:37
by CodedZyntaX
I already created my own encryption batch. However it reply takes a lot of time to encrypt more than 500 words.
Is there any faster batch encryption?
Re: String encrytion and decryption
Posted: 09 Dec 2011 21:58
by orange_batch
It depends how you encrypt it and your method of doing so. We can't help without code examples.
Re: String encrytion and decryption
Posted: 10 Dec 2011 01:47
by CodedZyntaX
Here's my own method for encrypting the file.
enx.bat
Code: Select all
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=%1"
set str2=%str:~1,-1%
set encrypt_type=127
set encrypted_str=
set ctr=0
set val=
call strlen %str%
:start
if %ctr% == %strlen% goto :output
set chr=!str2:~%ctr%,1!
for /f "tokens=1 delims=:" %%a in ('"findstr /n /c:"^%chr%" etable.etb"') do
set val=%%a
set /a val+=%encrypt_type%
for /f "tokens=2 delims=þ" %%b in ('findstr /n /c:"%val%" etable.etb') do
set encrypted_str=!encrypted_str!%%b
set /a ctr+=1
goto :start
:output
echo %encrypted_str%
Sample Usage:
enx "Hello World"
Output:
¨ÄËË΀·ÎÑËÃ
(This is not the actual output in DOS)
etable.etb contains:
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
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þÒþ
212þÓþ
213þÔþ
214þÕþ
215þÖþ
216þ×þ
218þØþ
219þÙþ
220þÚþ
221þÛþ
222þÜþ
223þÝþ
224þÞþ
225þßþ
226þàþ
227þáþ
228þâþ
229þãþ
230þäþ
231þåþ
232þæþ
233þçþ
234þèþ
235þéþ
236þêþ
237þëþ
238þìþ
239þíþ
240þîþ
241þïþ
242þðþ
243þñþ
244þòþ
245þóþ
246þôþ
247þõþ
248þöþ
249þ÷þ
250þøþ
251þùþ
252þúþ
253þûþ
254þüþ
I also created a batch using calc.exe from autoit to get the ascii code of a character but this method that I posted is twice faster.
Please help me on this.
Re: String encrytion and decryption
Posted: 10 Dec 2011 03:23
by orange_batch
I don't recommend using ! " ^ % in your encryption table.
Also, your
fors are broken. Format:
Code: Select all
for /f "..." %%a in ('command') do (
Code goes here.
)
Also,
Code: Select all
'"findstr /n /c:"^%chr%" etable.etb"'
should be
Code: Select all
'findstr /n /c:"%chr%" etable.etb'
I can't follow what you're doing among all the broken for loops.
Re: String encrytion and decryption
Posted: 10 Dec 2011 03:56
by CodedZyntaX
I used this kind of loop because of the special characters like " ! % > < etc.
If I used
Code: Select all
'findstr /n /c:"%chr%" etable.etb'
This will not work if chr is equal to (") quote or !, >, etc.
This batch is working. My only concern is how to speed up the conversion of this batch file.
Re: String encrytion and decryption
Posted: 10 Dec 2011 04:24
by orange_batch
I see, however your for loops are still broken in the above code. I suppose they should be like?:
Code: Select all
for /f "tokens=1 delims=:" %%a in ('"findstr /n /c:"^%chr%" etable.etb"') do (
set val=%%a
set /a val+=%encrypt_type%
)
for /f "tokens=2 delims=þ" %%b in ('findstr /n /c:"%val%" etable.etb') do (
set encrypted_str=!encrypted_str!%%b
set /a ctr+=1
)
Re: String encrytion and decryption
Posted: 10 Dec 2011 11:16
by CodedZyntaX
I understand your point. However I don't consider my for loop broken since it's working and giving the same output as the one that you suggested.
In fact, I tested the speed of both method but both encrypted the string "Hello World" in 1.56 Sec.
Re: String encrytion and decryption
Posted: 10 Dec 2011 14:03
by dbenham
@CodedZyntax - the code you posted will not work with any standard Windows batch processing that I am aware of. The DO commands must be on the same line as the FOR command, or you must use line continuation, or the commands must be enclosed in parens () like orange_batch suggested. Either the code you are executing is not exactly what you posted, or you are using a non-standard batch processor.
Beyond that, your algorithm has problems.
1) Passing string literals on the command line can cause problems if the string contains both quoted and unquoted special characters. There are also problems if the string contains ! because you have enabled delayed expansion. It is much safer to load the string in a variable and then pass the variable name to the function.
2) Your algorithm outputs extended characters (code value>127). But it fails if your input has extended characters, so you cannot un-encrypt the results.
I was able to build a crude encryption script using the routines in the CHARLIB.BAT script I showed you.
Re: new functions: :chr, :asc, :asciiMap.
Code: Select all
@echo off
:enx inputVar [outputVar]
setlocal enableDelayedExpansion
set "str=!%~1!"
set "offset=112"
call charlib strlen str len
set /a "len-=1"
set "output="
for /l %%N in (0 1 %len%) do (
call charlib asc str %%N ascii
set /a "ascii=(((ascii-32)+offset)%%224)+32"
call charlib chr ascii char
set "output=!output!!char!"
)
for /f "delims=" %%A in ("!output!") do (
endlocal
if "%~2"=="" (echo(%%A) else set "%~2=%%A"
)
exit /b
The 1st argument should be the name of a variable containing the string. The 2nd optional parameter is the name of the variable to receive the output. If the 2nd parameter is missing, then the encrypted string is ECHOed. It encrypts characters with codes >= 32. It leaves any control characters un-encrypted.
Assuming the script is in a file named enx.bat
Code: Select all
C:\utils>set "myString=Hello world!"
C:\utils>enx myString
╕╒▄▄▀Éτ▀Γ▄╘æ
C:\utils>enx myString output
C:\utils>set output
output=╕╒▄▄▀Éτ▀Γ▄╘æ
C:\utils>enx output
Hello world!
C:\utils>
It processes the string "Hello world!" in 0.5 seconds. Using techniques found in the ROT13 function that is in the same CHARLIB.BAT, you should be able to get the time down to around 0.02 seconds. Of course absolute times vary from machine to machine.
But it hardly seems worthwhile to me. The output is certainly not secure. It just makes it so no-one can casually read the output. The ROT13 and ROT13F routines are already written, and in my mind they accomplish the same thing. They just encrypt ASCII alpha characters and leave the rest alone. They are already optimized for speed. (0.02 seconds for "Hello world!")
Dave Benham
Re: String encrytion and decryption
Posted: 11 Dec 2011 02:46
by orange_batch
Thanks Dave, I really didn't have the time or patience for this one. :P
I'll be writing another complex encryptor/decryptor myself fairly soon I think.
Re: String encrytion and decryption
Posted: 11 Dec 2011 08:01
by CodedZyntaX
Thank you guys.
I learned a lot from you guys.