xBox
07-19-2003, 10:44 PM
Can anybody tell me a Algorithm in VB to Encrypt/Decrypt password.
Encrypt and DeCrypt Algorithm in VBxBox 07-19-2003, 10:44 PM Can anybody tell me a Algorithm in VB to Encrypt/Decrypt password. Hawk 07-19-2003, 11:51 PM XBox, Basically programmers can create their own encryption algorythms. Each character has an ASCII value between 0 and 255. You can use the asc( string ) function to get the ascii value of the first letter in string. Now, from here, the possibilities are endless. You can do a simple encryption/decryption by processing each character value by 255 - x, where x is the ascii value of that character. Example: public function CodeIt(Str as string) as string Dim X Dim Y for X = 1 to len(Str) 'Loop for the length of the string Y = mid(str, X, 1) 'Y = the character at the position of X Y = asc(Y) 'Y = the ascii value of that character Y = 255 - Y 'Get the 255 - Y Value Y = chr(Y) 'Turn Y back into a character, this time with the new coded value CodeIt = CodeIt & Y 'Add Y to the return Value next x end function This function returns a string with the same length as the old one, but it also 'Flips'* the characters. * Basically, by doing that you are making z a and a z (if a was 0 and z was 255) but anyway, you can reverse the string, output the ascii character for each value, and much much more. From Here, it's up to you. :cool: Cheers Optikal 07-19-2003, 11:55 PM The standard for encryption if you want something "strong" is the Advanced Encryption Standard (AES), this replaces the old standard DES. The specification can be found here http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf It sounds complicated, but its really not that difficult to implement. I had to write an implementation in C++ for a univ. assignment, only took one evening to write it. Hasin 07-20-2003, 12:02 AM Actually The Best Algorithm is MD5 Hash Algorithm. Please Search Google.com.sg and and you will find a lot of wrapper for visual basic for md5 hash algorithm functions Take care Hasin Optikal 07-20-2003, 12:04 AM what does md5 have to do with encrypting data? md5 is an algorithm used for signing stuff. A-Dam 07-20-2003, 12:05 AM I'm not sure if this is what you're after but you can use the Xor operator for simple encrypt/decrypt functions. You can see MSDN knowledge base Article ID: Q110308 Basically, you convert your string as to a string of bytes. Then you loop through each "password" byte and Xor it with another constant "key" byte. This will change the value of the password byte without ever causing an overflow of the byte data type. If you Xor the result with the same "key" byte it was encrypted with, the original value will be returned. Here's an example: Dim key(2) As Byte, cat(2) As Byte, result(2) As Byte Private Sub Command1_Click() key(0) = Asc("k") key(1) = Asc("e") key(2) = Asc("y") cat(0) = Asc("c") cat(1) = Asc("a") cat(2) = Asc("t") result(0) = cat(0) Xor key(0) result(1) = cat(1) Xor key(1) result(2) = cat(2) Xor key(2) MsgBox Chr$(result(0)) & Chr$(result(1)) & Chr$(result(2)) End Sub Private Sub Command2_Click() result(0) = result(0) Xor key(0) result(1) = result(1) Xor key(1) result(2) = result(2) Xor key(2) MsgBox Chr$(result(0)) & Chr$(result(1)) & Chr$(result(2)) End Sub I left out comments, sorry, but it should be pretty easy to follow. Hope that helps. DaftasBrush 07-20-2003, 12:51 PM I was going to point this out anyway.... NEVER DECRYPT YOUR PASSWORDS! If you do, you end up with your decryption algorithm in your app, and someone with sufficient skill (and time) could decompile, or just plain steal you decryption algorithm, then use it to decrypt all your passwords. The better solution, is encrypt your passwords to store them... fine... but then when the user enters a password, just run it through the same encryption algorithm and check that the result matches the stored version. MD5 will do this. [The MD5 algorithm] takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest. It produces a very unique signature for the item it's used on, that's checked against the same calcuation on the item when you receieve it. The time needed to break these sorts of algorithms is huge ("computationally infeasible"), compared to the time it would take to find the right chunk in an exe and nab it so you could decrypt the passwords. A-Dam 07-21-2003, 09:59 PM Good point. If this is an "important" program dealing with other people's passwords, you should try to make it reasonably secure. In one program I made, I stored an array of random bytes in a resource file. Calling the LoadResData function can get the bytes back into an array. When you form the password, you can add one extra random "character" which tells the offset from the beginning or end of the byte array. From that offset, you start Xor'ing the password bytes to encrypt/decrypt. I like to work backwards from the end to make it even harder to crack. For example: '(this is pseudocode) Dim keybytes() As Byte, password() As Byte, offset As Byte Dim passwordstring As String 'You have a 1000 byte array in the resource file. 'Use LoadResData to get the data into keybytes() 'Let's say the password is Homer. passwordstring = "Homer" 'Make the password() array one character longer than the password. Redim password(Len(passwordstring)) 'Generate a random number from 0 to 255. (Could be even more, but I'm just staying in the byte range.) offset = Int(Rnd * 255 + 1) 'offset = 113. For reps = 0 to Len(passwordstring) - 1 password(reps) = Asc(Mid$(passwordstring, reps + 1, 1)) Xor keybytes(Ubound(keybytes) - offset - reps) Next reps 'Set last byte to offset value password(Ubound(password)) = offset Then when you go to decrypt, the last character in the password tells how far from the end of the keybytes() array you should start from. There are many variations to this method that can make it even harder to crack; such as adding two bytes to the end of the password. (offset = extrabyte1 + extrabyte2) Or Xor'ing on every other byte in the keybytes() array. The possibilities are endless. J0n 07-22-2003, 12:37 AM There's a simple encryption tutorial in the Tutor's Corner: http://www.visualbasicforum.com/t31778.html |
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum