DES Crypto not working ???

A

Alex Nitulescu

Hi. I have the following simple code, and yet.... I enter for instance
"Encryption" and I get back ">ZH(5f^0"

Web.Config:
---------------------------------------------------------------------
<appSettings>
<add key="DESKey" value="MAMMAMIA"></add>
<add key="DESIV" value="PAPPAPIA"></add>
</appSettings>
---------------------------------------------------------------------

Module Utils:
---------------------------------------------------------------------
Imports System.IO
Namespace Raducu
Module Utils

Public Function ConvertToByteArray(ByVal strInput As String) As
Byte()
Dim intCounter As Integer
Dim arrChar As Char()
Dim arrByte As Byte()

arrChar = strInput.ToCharArray
ReDim arrByte(arrChar.Length - 1)
For intCounter = 0 To arrByte.Length - 1
arrByte(intCounter) = Convert.ToByte(arrChar(intCounter))
Next
Return arrByte
End Function

Public Function FileExists(ByVal strPath As String) As Boolean
Return File.Exists(strPath)
End Function

End Module
End Namespace
---------------------------------------------------------------------

and in the form:
---------------------------------------------------------------------
Imports System.Security.Cryptography
Imports System.IO

Public Class Encrypt_Decrypt
Inherits System.Web.UI.Page

Protected WithEvents txtEncrypt As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdEncrypt As System.Web.UI.WebControls.Button
Protected WithEvents txtDecrypt As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdDecrypt As System.Web.UI.WebControls.Button
Protected WithEvents lblResults As System.Web.UI.WebControls.Label

Private Sub cmdEncrypt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdEncrypt.Click

Dim strInput As String
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

Try
strInput = txtEncrypt.Text
If strInput <> "" Then
arrInput = Raducu.Utils.ConvertToByteArray(strInput)
arrDESKey =
Raducu.Utils.ConvertToByteArray(ConfigurationSettings.AppSettings("DESKey"))
arrDESIV =
Raducu.Utils.ConvertToByteArray(ConfigurationSettings.AppSettings("DESIV"))
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)
objCryptoStream.Close()
objFileStream.Close()
End If
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objEncryptor.Dispose()
End Try
End Sub

Private Sub cmdDecrypt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDecrypt.Click

Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream

If Raducu.Utils.FileExists(MapPath("Secret.txt")) Then
Try
arrDESKey =
Raducu.Utils.ConvertToByteArray(ConfigurationSettings.AppSettings("DESKey"))
arrDESIV =
Raducu.Utils.ConvertToByteArray(ConfigurationSettings.AppSettings("DESIV"))
objDES = New DESCryptoServiceProvider()
objDecryptor = objDES.CreateEncryptor(arrDESKey, arrDESIV)
objFileStream = New FileStream(MapPath("Secret.txt"),
FileMode.Open, FileAccess.Read)
objCryptoStream = New CryptoStream(objFileStream,
objDecryptor, CryptoStreamMode.Read)
txtDecrypt.Text = New
StreamReader(objCryptoStream).ReadToEnd
objCryptoStream.Close()
objFileStream.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objDecryptor.Dispose()
End Try
Else
lblResults.Text &= "<li>" & "The file '" & MapPath("Secret.txt")
& " does not exist !"
End If
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top