Page 1 of 1 1
Topic Options
#198132 - 2010-03-23 10:18 AM Encrypt/Decrypt a string with kix
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
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...

Top
#198133 - 2010-03-23 10:54 AM Re: Encrypt/Decrypt a string with kix [Re: Sam_B]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4667
Loc: The Netherlands
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
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198134 - 2010-03-23 11:06 AM Re: Encrypt/Decrypt a string with kix [Re: Sam_B]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198140 - 2010-03-23 04:57 PM Re: Encrypt/Decrypt a string with kix [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Just make sure that you tokenise any production script otherwise someone will just copy the decryption code and run it \:o
Top
#198142 - 2010-03-23 05:49 PM Re: Encrypt/Decrypt a string with kix [Re: Richard H.]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
It would be interesting to see encryption/decryption for:
TripleDES (3DES)
AES (Rijndael)
SHA01 (Hash)
MD5 (Hash)
HmacMD5
DES
RSA

Have fun!

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#198147 - 2010-03-23 07:11 PM Re: Encrypt/Decrypt a string with kix [Re: Richard H.]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4520
Loc: USA
Thanks Richard. You answered exactly what I was thinking/wondering.
Top
#198148 - 2010-03-23 07:28 PM Re: Encrypt/Decrypt a string with kix [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
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).


Edited by Glenn Barnas (2010-03-23 09:09 PM)
Edit Reason: added example
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198153 - 2010-03-24 07:58 AM Re: Encrypt/Decrypt a string with kix [Re: Glenn Barnas]
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
Hi Glenn, perfect, absolutely perfect, exactly what I was looking for! Thanks to you and all the other contributors of this great board
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (mole) and 302 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.041 seconds in which 0.014 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org