[bb] How encrypt a string by Filax [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : How encrypt a string
Author : Filax
Posted : 1+ years ago

Description : Function for crypt a string with a key

Code :
Code (blitzbasic) Select
Tmp$=FBK_CryptString$("This is a test",$E2CDF032)
Print Tmp$

Tmp2$=FBK_CryptString$(Tmp$,$E2CDF032)
Print Tmp2$

WaitKey

Function FBK_CryptString$(Source$,Key)
For C=1 To Len(Source$)
Char$=Char$+Chr$(Asc(Mid$(Source$,C,1) ) Xor Key)
Next

Return Char$
End Function


Comments :


Rck(Posted 1+ years ago)

 it seems that when i change the decrpyt key to $32 it still decrypts the key... not so safe? Also, some change can be made to the encrypt key and will still be decryptable with $32.. can you explain why this works?


electronin(Posted 1+ years ago)

 Hey, Rck, you're right. Another thing I noticed, is any key starting with $ and ending with 32 will decrypt it. Any ideas, filax?


Koriolis(Posted 1+ years ago)

 The problem is than only the lower byte is used in this scheme. So the key here is in fact $32, whatever the other bytes are.Xoring is a very basic way of encrypting, it's very far from safe, but is probably good enough to prevent most people to rip your medias. But at the very least it would be good to have a longer key. Not that it's a lot safer (still very weak) but as it's as easy and fast to do, why not? Something like this (no optimization):
myKey$ = "yeepeekai"

Tmp$=FBK_CryptString$("This is a test",myKey)
Print Tmp$

Tmp2$=FBK_CryptString$(Tmp$,myKey)
Print Tmp2$

WaitKey

Function FBK_CryptString$(Source$,Key$)
Local ls = Len(Source$)
Local lk = Len(Key)
For C=1 To ls
Char$=Char$+Chr$(Asc(Mid$(Source$,C,1) ) Xor Asc(Mid$(Key$,1 + (C Mod lk),1) ))
Next

Return Char$
End Function
The longer the key, the better it is.


Filax(Posted 1+ years ago)

 Thanks for this !! [/i]