Migrating and Encrypting

Posted by Zu on May 12, 2010 in .Net |

getOldUsers – read the ds one by one from the old db, where the passwords are not yet encrypted.

InsertUserx – writes the users into your new (current) db, with passwords encrypted.

Public Function MigrateUser() As Integer
Dim dr As DataRow
Dim ds As DataSet
Dim dt As DataTable
Dim userID As Integer
ds = getOldUsers()
dt = ds.Tables(0)
For Each dr In dt.Rows
newPass = Encrypt(password)
userID = InsertUserx(dr("id"), "", dr("name").ToString, "", dr("lastName").ToString, dr("email").ToString, "", newPass , dr("address").ToString)
Next
ds.Dispose()
Return userID
End Function

defines the key

Private lbtVector() As Byte = {240, 3, 45, 29, 0, 76, 173, 59}
Private lscryptoKey As String = "ChangeThis!"

Encrypts the password

Public Function Encrypt(ByVal sInputVal As String) As String
Dim loCryptoClass As New TripleDESCryptoServiceProvider
Dim loCryptoProvider As New MD5CryptoServiceProvider
Dim lbtBuffer() As Byte
Try
lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal)
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
loCryptoClass.IV = lbtVector
sInputVal = Convert.ToBase64String(loCryptoClass.CreateEncryptor().TransformFinalBlock(lbtBuffer, 0, lbtBuffer.Length()))
Encrypt = sInputVal
Catch ex As CryptographicException
Throw ex
Catch ex As FormatException
Throw ex
Catch ex As Exception
Throw ex
Finally
loCryptoClass.Clear()
loCryptoProvider.Clear()
loCryptoClass = Nothing
loCryptoProvider = Nothing
End Try
End Function

Decrypts the password:

Public Function Decrypt(ByVal sQueryString As String) As String
Dim buffer() As Byte
Dim loCryptoClass As New TripleDESCryptoServiceProvider
Dim loCryptoProvider As New MD5CryptoServiceProvider
Try
buffer = Convert.FromBase64String(sQueryString)
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
loCryptoClass.IV = lbtVector
Return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
Catch ex As Exception
Throw ex
Finally
loCryptoClass.Clear()
loCryptoProvider.Clear()
loCryptoClass = Nothing
loCryptoProvider = Nothing
End Try
End Function

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2007-2010 Xrum! Blog... All rights reserved.
Desk Mess Mirrored v1.5.1 theme from BuyNowShop.com.