469,282 Members | 2,008 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Encrypting Password

Hi!I am doing a login form but i have this problem I want to encrypt the password they entered and save it in database..Can someone help me?Thank You in Advance..


-cassey
Jan 24 '07 #1
14 12667
ADezii
8,800 Expert 8TB
Hi!I am doing a login form but i have this problem I want to encrypt the password they entered and save it in database..Can someone help me?Thank You in Advance..


-cassey
I'll look up a simple encryption algorithm as soon as I get a chance and post it for you.
Jan 24 '07 #2
NeoPa
32,173 Expert Mod 16PB
This is a very basic one that I use.
Expand|Select|Wrap|Line Numbers
  1. 'If I told you what this did I'd have to kill you.
  2. Public Function Scramble(strPW As String)
  3.     Dim intIdx As Integer
  4.  
  5.     Scramble = strPW
  6.     For intIdx = 1 To Len(strPW)
  7.         Mid(Scramble, intIdx, 1) = _
  8.             Chr(&H40 Or (Not Asc(Mid(strPW, intIdx, 1))) And &H3F)
  9.     Next intIdx
  10. End Function
I expect the ADezii comes up with will be less predictable.
Jan 24 '07 #3
ADezii
8,800 Expert 8TB
Hi!I am doing a login form but i have this problem I want to encrypt the password they entered and save it in database..Can someone help me?Thank You in Advance..


-cassey
As promised, any questions feel free to ask:
Expand|Select|Wrap|Line Numbers
  1. Public Function fEncryptString(TheString As String, ByVal nbrPlaces As Byte) As String
  2. '*******************************
  3. ' Author: Philfr               *
  4. ' Email : philfrank@yahoo.com  *
  5. ' Date  : 25 - 5 - 02          *
  6. '******************************************************************************************
  7. 'This is a simple Encryption Algorithm. Knowing all 'printable' characters are 8
  8. 'Bytes long, you can shift the bits to the left and still have a printable character.
  9. 'To Decrypt, all you have to do is to shift them left until you have come full circle
  10. 'as in the following calling procedures:
  11. 'to Encrypt: fEncryptString(TheString, nbrPlaces)
  12. 'to Decrypt: fEncryptString(TheString, 8 - nbrPlaces)
  13.  
  14. Dim tmp As Integer, i As Integer, mult As Integer
  15. Dim intLength As Integer, tmpSt As String
  16.  
  17. intLength = Len(TheString)
  18. tmpSt = ""
  19. nbrPlaces = nbrPlaces Mod 8     'no point doing more than 7, besides
  20. mult = 2 ^ nbrPlaces                  'mult (an Integer) would be too small
  21.  
  22. For i = 1 To intLength
  23.   tmp = Asc(Mid$(TheString, i, 1))          'get the ASCII value of each character
  24.   tmp = tmp * mult                             'apply the multiplier
  25.   tmp = tmp Mod 256 + tmp \ 256             'rotate any 'carry' bit
  26.   tmpSt = tmpSt & Chr$(tmp)                 'add the character to the String
  27. Next i
  28.  
  29. fEncryptString = tmpSt
  30. End Function
Jan 24 '07 #4
Thank You so much for all the reply..I will test this 2 codes today and ill let you know what happen..thanx!
Jan 25 '07 #5
NeoPa
32,173 Expert Mod 16PB
You're welcome.
We're interested in success stories as well as problems. It helps other readers of the thread to know that the answer works.
Jan 25 '07 #6
You're welcome.
We're interested in success stories as well as problems. It helps other readers of the thread to know that the answer works.

Hi!I tried your code..Can you please explain to me how it works?
Jan 25 '07 #7
NeoPa
32,173 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. 'If I told you what this did I'd have to kill you.
  2. Public Function Scramble(strPW As String)
  3.     Dim intIdx As Integer
  4.  
  5.     Scramble = strPW
  6.     For intIdx = 1 To Len(strPW)
  7.         Mid(Scramble, intIdx, 1) = _
  8.             Chr(&H40 Or (Not Asc(Mid(strPW, intIdx, 1))) And &H3F)
  9.     Next intIdx
  10. End Function
Clearly you're not worried about the warning on line one then ;(
It's essentially a Bitwise OR operation.
There is, however, provision made for ensuring that all characters produced are printable characters so, no matter where it us used / stored, you should have no problems.
Jan 25 '07 #8
Expand|Select|Wrap|Line Numbers
  1. 'If I told you what this did I'd have to kill you.
  2. Public Function Scramble(strPW As String)
  3.     Dim intIdx As Integer
  4.  
  5.     Scramble = strPW
  6.     For intIdx = 1 To Len(strPW)
  7.         Mid(Scramble, intIdx, 1) = _
  8.             Chr(&H40 Or (Not Asc(Mid(strPW, intIdx, 1))) And &H3F)
  9.     Next intIdx
  10. End Function
Clearly you're not worried about the warning on line one then ;(
It's essentially a Bitwise OR operation.
There is, however, provision made for ensuring that all characters produced are printable characters so, no matter where it us used / stored, you should have no problems.

Ok. Thanx so much I already got it..Thank you so much!
Jan 26 '07 #9
Hi.

I know that this is a very old thread, but I found it because I wanted to do exactly this - but found that neither solution quite worked for me.

NeoPa's solution works well, but not for punctuation. When you run it on the encrypted string, alpha and numbers return to normal but e.g. space turns to '

ADezii's solution is better, in that sense, but for the right settings will cheerfully return a character less than 32 - for example "P" shifted by 5 gives a LF. Fine if all you are doing is storing it in a field that doesn't care, but in my case I wanted to write it to an external file and that would mess up the record.

So what I came up with in the end was this. Thought I'd share in case anyone else found it useful.

Expand|Select|Wrap|Line Numbers
  1. Function sEncryptString(ByVal sPlainText As String) As String
  2.  
  3.     Dim iIndex As Integer
  4.     Dim iEncoder As Integer
  5.     Dim iEncodedVal As Integer
  6.  
  7.     ' Each character will be XORed with a (different) random value 0-255.
  8.     ' The beauty of XOR is that you undo it simply by XORing with the same value
  9.     '
  10.     ' The output string will be twice as long as the input string,
  11.     ' because we need to store the value that we used to XOR
  12.     '
  13.     ' Because it will be stored in a "plain text" file (e.g. a definition file)
  14.     ' We MUST have printable characters so no XOR value or resultant character code
  15.     ' is permitted to be <32 or 127 (DEL). That is why we are encoding each character
  16.     ' with a different code, because it is easier to check for these values rather than
  17.     ' repeating the entire encode endlessly until we find one that is good.
  18.  
  19.     Randomize                                           ' Initialise the random number generator
  20.     sEncryptString = ""
  21.     For iIndex = 1 To Len(sPlainText)
  22.         Do
  23.             iEncoder = Int(94 * Rnd + 32)               ' Use an encoder value between 32 and 126
  24.             iEncodedVal = Asc(Mid(sPlainText, iIndex, 1)) Xor iEncoder
  25.         Loop While iEncodedVal = 127 Or iEncodedVal < 32
  26.         sEncryptString = sEncryptString & Chr(iEncodedVal) & Chr(iEncoder)
  27.     Next iIndex
  28.  
  29. End Function
  30.  
  31. Function sDecryptString(ByVal sEncryptedString As String) As String
  32.  
  33.     Dim iIndex As Integer
  34.     Dim iDecodedVal As Integer
  35.  
  36.     ' This is the reverse of the encrypt function
  37.     ' Simply take the first of each pair of characters and XOR it with the second
  38.  
  39.     sDecryptString = ""
  40.     For iIndex = 1 To Len(sEncryptedString) Step 2
  41.         iDecodedVal = Asc(Mid(sEncryptedString, iIndex, 1)) Xor Asc(Mid(sEncryptedString, iIndex + 1, 1))
  42.         sDecryptString = sDecryptString & Chr(iDecodedVal)
  43.     Next iIndex
  44.  
  45. End Function
  46.  
Nov 6 '13 #10
NeoPa
32,173 Expert Mod 16PB
There's no reason not to add to an old discussion if it's still relevant and you feel you have something to add. It may be interesting to see an explanation of what the code's doing though.
Nov 7 '13 #11
By all means.

The Encrypt routine goes through the string, one letter at a time, and performs a bitwise Exclusive-Or (XOR) on the ASCII value of the character using a random number that has been generated in the range 32-126. It then adds both the new character and the character representing the random number to the encrypted string.

If you are not familiar with XOR, it is like OR except that two 1s produce a 0 output. It's advantage is that it is completely reversible, so that if C = A XOR B, then A = C XOR B.

My requirement was to write the encrypted string to a plain text file, so I had to have printable characters which didn't include DEL, BKSP or any of the control characters (ASCII values less than 32). So that is why the random number is created in the range it is, and why the XOR function has a loop to ensure that I keep repeating the encoding with different random numbers until I get a printable character.

I could have increased the range of the random number to 255 using a similar loop around the random number generator and setting it to give numbers up to 255, but thought that that was overkill for this application.

The decrypt routine reads the characters from the string in pairs. The first is the encrypted character, and the second is the value that was used to encrypt it. The first is the XORed with the second to recover the original character, which is then added to the output string
Nov 7 '13 #12
NeoPa
32,173 Expert Mod 16PB
Thank you. That was well worth the read :-)

My latest version handles the same problem differently. It uses all 256 possible values, but instead of storing the character itself, which can cause problems in some environments due to many possible values not being printable, it stores (the ASCII codes of) the hex digits of the character. The encryption algorithm used is RC4 (RC4 Encryption Algorithm for VBA and VBScript) and different projects can use different hashes to ensure the data doesn't become too predictable or recognisable.
Nov 7 '13 #13
Interesting, thanks. When I have a little more time, I'll try and get my mind around the RC4 code in the linked article. For the application I am developing, what I have now is probably sufficient, but always good to learn new things.
Nov 7 '13 #14
NeoPa
32,173 Expert Mod 16PB
That is much as my attitude was when I first came across it. Eventually I found the time to incorporte it into my standard code, and I haven't looked back :-)
Nov 7 '13 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Marshall Dudley | last post: by
3 posts views Thread by CLEAR-RCIC | last post: by
1 post views Thread by jimfortune | last post: by
reply views Thread by Sunny | last post: by
4 posts views Thread by Andy G | last post: by
3 posts views Thread by Thirsty Traveler | last post: by
19 posts views Thread by Cord-Heinrich Pahlmann | last post: by
2 posts views Thread by SeeSharp Bint | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.