Sam_B
(Getting the hang of it)
2010-03-23 10:18 AM
Encrypt/Decrypt a string with kix

Hi guys,

It's me again, stuck as usual...

I'm searching for a function that encrypts/decrypts a string. I don't need a very very secure algorithm, but I need to write a bit sensitive data to a text file and I want to prevent that people are able to read it. So I plan to encrypt the text before writing it to the text file. I do not want to encrypt the file itself, because I need to retrieve values from it during script execution via ReadProfileString()

There a quite a lot VB samples available, but I'm too stupid to transform those scripts to kix. I search the UDF's but was unable to find one that does this job already...


Mart
(KiX Supporter)
2010-03-23 10:54 AM
Re: Encrypt/Decrypt a string with kix

Hi Sam,

Someone did do this in Kix. You could use the Vigenere UDF. Not sure if it is posted but is was the outcome of a round of KixGolf.

 Code:
Break on

;Set the encryption key.
$EncryptionKey = UCase("encryption key goes here")
;Set the text you want to encrypt.
$text = "TEXT YOU WANT TO ENCRYPT"
;Encrypt text.
$TextEncrypted = Vigenere($EncryptionKey, $text)
;Show encrypted text.
? $TextEncrypted
;Decrypt text.
$TextDecrypted = Vigenere($EncryptionKey, $TextEncrypted, 1)
;Show decryptedt text.
?$TextDecrypted

Sleep 5

;Parameters are $key (Key, must be in uppercase), $text (string to encrypt, must be in uppercase) And $decrypt (0=encrypt, 1=decrypt).
;in this version you can define the alphabet which will be accepted, so it can be scaled to most needs:
; The alphabet used in the UDF is the classic "English letters only" version.
; If you want a richer alphabet, just change $ to reflect it, for example:
; $="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !"$%^&*()_+{}:@~<>?,./;'#[]-="+'"'
; If you want to use lower case, don't for get to set case sensitivity on.

Function Vigenere($key,$text,Optional $decrypt)
	Dim $range
	$range = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /"
	While $text
		$i=InStr($range,Left($text,1))
		If $i
			$Vigenere=$Vigenere+SubStr($range+$range+$range,$i+Len($range)+(1-2*$decrypt)*(Asc($key)-65),1)
			$key=SubStr($key,2)+Left($key,1)
		EndIf
		$text=SubStr($text,2)
	Loop
EndFunction


Glenn BarnasAdministrator
(KiX Supporter)
2010-03-23 11:06 AM
Re: Encrypt/Decrypt a string with kix

The CODEC (CODe-DECode) UDF is availble here and on my web site. It can encode/decode text strings using the full ASCII printable character set (codes 32-127) and can even include select control codes. By employing control codes such as CR, LF, and TAB, the actual appearance of a text document will change, although you should not use CR or LF when encoding data used in INI files. CODEC is a case-sensitive cypher tool that employs a random cypher sequence - never in natural or ASCII order - making it somewhat more difficult to hack.

Note that this is not "encryption", but encoding (simple cypher) - sufficient to hide information from prying eyes. If you are looking to mask a password in a config file, this is usually enough.

Glenn


Richard H.Administrator
(KiX Supporter)
2010-03-23 04:57 PM
Re: Encrypt/Decrypt a string with kix

Just make sure that you tokenise any production script otherwise someone will just copy the decryption code and run it \:o

Kdyer
(KiX Supporter)
2010-03-23 05:49 PM
Re: Encrypt/Decrypt a string with kix

It would be interesting to see encryption/decryption for:
TripleDES (3DES)
AES (Rijndael)
SHA01 (Hash)
MD5 (Hash)
HmacMD5
DES
RSA

Have fun!

Kent


AllenAdministrator
(KiX Supporter)
2010-03-23 07:11 PM
Re: Encrypt/Decrypt a string with kix

Thanks Richard. You answered exactly what I was thinking/wondering.

Glenn BarnasAdministrator
(KiX Supporter)
2010-03-23 07:28 PM
Re: Encrypt/Decrypt a string with kix

Tokenizing is kind of a given.. \:\)

I've begun using a different (unpublished) form of CODEC that is based on a cypher used in an episode of Mission: Impossible from the late 1960's. \:D What's cool about this cypher is that it is not succeptible to hacking based on letter frequency methods. For example, the word "cat" could potentially be represented by "zzz". (unlikely, but possible!) The cypher key is numeric and composed of potentially thousands of digits.

Glenn

PS - here's a sample cypher from the simple version:
 Code:
kgt+,x*6]1io^t7nq8 w|6a&#8962;XyOi%f+(~~z{o$u&#8962;^e{&#8962;m1,\ )ozdx% `l{'&#8962;" xhvv
Note the "~~" and "vv", which are actually "ro" and "e!" in the clear text. There are thre occurrences of "ss" in the original text, yet they cypher to 'o$', ')o', and '~"'. This cypher used a 3-digit key with a 13-byte rotation, although I can use byte rotations that number in the thousands (I usually use 32768 when encoding files).


Sam_B
(Getting the hang of it)
2010-03-24 07:58 AM
Re: Encrypt/Decrypt a string with kix

Hi Glenn, perfect, absolutely perfect, exactly what I was looking for! Thanks to you and all the other contributors of this great board