|
Post by oswald on Sept 6, 2008 13:30:41 GMT -5
Can anyone help me to transform that code from VB to RB?Function EncryptString(ByVal Text As String, ByVal Password As String) As String Dim passLen As Long Dim i As Long Dim passChr As Integer Dim passNdx As Long passLen = Len(Password) ' null passwords are invalid If passLen = 0 Then Err.Raise 5 ' move password chars into an array of Integers to speed up code ReDim passChars(0 To passLen - 1) As Integer CopyMemory passChars(0), ByVal StrPtr(Password), passLen * 2 ' this simple algorithm XORs each character of the string ' with a character of the password, but also modifies the ' password while it goes, to hide obvious patterns in the ' result string For i = 1 To Len(Text) ' get the next char in the password passChr = passChars(passNdx) ' encrypt one character in the string Mid$(Text, i, 1) = Chr$(Asc(Mid$(Text, i, 1)) Xor passChr) ' modify the character in the password (avoid overflow) passChars(passNdx) = (passChr + 17) And 255 ' prepare to use next char in the password passNdx = (passNdx + 1) Mod passLen Next
EncryptString = Text End Function
|
|
|
Post by StefanPendl on Sept 6, 2008 16:42:41 GMT -5
Here is the converted function. Encrypted$ = EncryptString$("I am encrypted Text", "1a3c763b930d3725e173645f") Decrypted$ = EncryptString$(Encrypted$, "1a3c763b930d3725e173645f")
print Encrypted$ print Decrypted$ end
Function EncryptString$(Text$, MyPassword$) EncryptString$ = "" passNdx = 0 passLen = Len(MyPassword$)
' null passwords are invalid If passLen > 0 Then ' move password chars into an array of Integers to speed up code Dim passChars(passLen) for i = 0 to passLen - 1 passChars(i) = asc(mid$(MyPassword$, i + 1, 1)) next
' this simple algorithm XORs each character of the string ' with a character of the password, but also modifies the ' password while it goes, to hide obvious patterns in the ' result string For i = 1 To Len(Text$) ' get the next char in the password passChr = passChars(passNdx) ' encrypt one character in the string EncryptString$ = EncryptString$ + Chr$(Asc(Mid$(Text$, i, 1)) Xor passChr) ' modify the character in the password (avoid overflow) passChars(passNdx) = (passChr + 17) And 255 ' prepare to use next char in the password passNdx = (passNdx + 1) Mod passLen Next end if End Function
|
|
|
Post by oswald on Sept 7, 2008 7:52:59 GMT -5
Thank you very much! But unfortunatily the encoded string is not what I expected it to be. I need a way to encrypt/decrypt a string conting any combination of text and numbers to a string that only consists of lowercase a-z and 0-9. All the encoders/decoders I found so far for RB or LB either sometimes produce false data, only work in one direction or generate strings of garbage and mysterious signs (like the code above does). Anyone knows how to program a really reliable encoder/decoder that could do it?
btw.... if I out-comment the following line in the above code, nothing changes.... seems like there is still something missing in the code!?passChars(passNdx) = (passChr + 17) And 255
|
|
|
Post by Janet on Sept 7, 2008 8:54:08 GMT -5
You asked for a translation of XOR encryption and that's what Stefan gave you. If you want to limit the encrypted characters, see MONOALPHA.BAS at LBPE: Cryptography with Liberty Basic. The example uses , but you could just as easily substitute lower case letters and remove the last 3 characters. You'll also need to change instances of UPPER$ to LOWER$ in the example code.
|
|
|
Post by oswald on Sept 7, 2008 9:27:00 GMT -5
Not the strongest encryption though.... but might still be ok for some unimportant things though.... Maybe someone can do and AES encoder/decoder for RB?? ;D
|
|
|
Post by Janet on Sept 7, 2008 10:00:56 GMT -5
Not the strongest encryption though.... but might still be ok for some unimportant things though.... Maybe someone can do and AES encoder/decoder for RB?? ;D If you want a stronger encryption, read Part 2 of the article. The 2 part article was written by an LB user who is a professional encryptor / decryptor for banks and credit cards. I'm sure you'll find some useful material in the article that will fit your needs.
|
|