DH
Programmer
- Dec 8, 2000
- 168
I am new to Encrypting and Decrypting text and would like to learn more.
I am working with the following code from ASP.NET Unleased. The code encrypts text and saves the text to a file. I can then decrypt the file when needed. To learn more I would like to:
1.) Display the encrypted text on the screen in the lbldecryptedtext.Text label
2.) Save the encrypted text to a database instead of a file.
Here is the code:
Const DESKey As String = "ABCDEFGH"
Const DESIV As String = "HGFEDCBA"
Function Convert2ByteArray(ByVal strInput As String) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()
arrChar = strInput.ToCharArray()
Dim arrByte(arrChar.Length - 1) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte(intCounter) = Convert.ToByte(arrChar(intCounter))
Next
Return arrByte
End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrInput As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream
arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray(DESIV)
arrInput = Convert2ByteArray(txtInput.Text)
objDES = New DESCryptoServiceProvider
objEncryptor = objDES.CreateEncryptor(arrDESKey, arrDESIV)
objFileStream = New FileStream( _
MapPath("secret.txt"), _
FileMode.Create, _
FileAccess.Write)
objCryptoStream = New CryptoStream( _
objFileStream, _
objEncryptor, _
CryptoStreamMode.Write)
objCryptoStream.Write(arrInput, 0, arrInput.Length)
lblencyptedtext.Text = ????????????
objCryptoStream.Close()
End Sub
Sub DESDecrypt()
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream
arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray(DESIV)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor(arrDESKey, arrDESIV)
objFileStream = New FileStream( _
MapPath("secret.txt"), _
FileMode.Open, _
FileAccess.Read)
objCryptoStream = New CryptoStream( _
objFileStream, _
objDecryptor, _
CryptoStreamMode.Read)
lbldecryptedtext.Text = New StreamReader(objCryptoStream).ReadToEnd()
objFileStream.Close()
End Sub
Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
DESDecrypt()
End Sub
Any suggestions on how to...
1.) Display the encrypted text on the screen in the lbldecryptedtext.Text label
2.) Save the encrypted text to a database instead of a file.
Thank you,
DH
I am working with the following code from ASP.NET Unleased. The code encrypts text and saves the text to a file. I can then decrypt the file when needed. To learn more I would like to:
1.) Display the encrypted text on the screen in the lbldecryptedtext.Text label
2.) Save the encrypted text to a database instead of a file.
Here is the code:
Const DESKey As String = "ABCDEFGH"
Const DESIV As String = "HGFEDCBA"
Function Convert2ByteArray(ByVal strInput As String) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()
arrChar = strInput.ToCharArray()
Dim arrByte(arrChar.Length - 1) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte(intCounter) = Convert.ToByte(arrChar(intCounter))
Next
Return arrByte
End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim arrInput As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream
arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray(DESIV)
arrInput = Convert2ByteArray(txtInput.Text)
objDES = New DESCryptoServiceProvider
objEncryptor = objDES.CreateEncryptor(arrDESKey, arrDESIV)
objFileStream = New FileStream( _
MapPath("secret.txt"), _
FileMode.Create, _
FileAccess.Write)
objCryptoStream = New CryptoStream( _
objFileStream, _
objEncryptor, _
CryptoStreamMode.Write)
objCryptoStream.Write(arrInput, 0, arrInput.Length)
lblencyptedtext.Text = ????????????
objCryptoStream.Close()
End Sub
Sub DESDecrypt()
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream
arrDESKey = Convert2ByteArray(DESKey)
arrDESIV = Convert2ByteArray(DESIV)
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor(arrDESKey, arrDESIV)
objFileStream = New FileStream( _
MapPath("secret.txt"), _
FileMode.Open, _
FileAccess.Read)
objCryptoStream = New CryptoStream( _
objFileStream, _
objDecryptor, _
CryptoStreamMode.Read)
lbldecryptedtext.Text = New StreamReader(objCryptoStream).ReadToEnd()
objFileStream.Close()
End Sub
Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
DESDecrypt()
End Sub
Any suggestions on how to...
1.) Display the encrypted text on the screen in the lbldecryptedtext.Text label
2.) Save the encrypted text to a database instead of a file.
Thank you,
DH