...

View Full Version : decrypt error



docock
09-08-2010, 11:38 AM
I found some coding with which I can encrypt/decrypt at: http://kellychronicles.spaces.live.com/blog/cns!A0D71E1614E8DBF8!593.entry

Now I wanted to change the coding so there allready is a string and key set.
(I know not very secure, but it suits my purpose).

Anyways I'm getting an 'Invalid key' error while decrypting.. :S How come? The key is set in the beginning, so there can't be any other key.


Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class Form1
Dim line1 As String = "just a testing string"
Dim key As String = "today"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = Crypto.Encrypt(line1, key)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

TextBox1.Text = Crypto.Decrypt(line1, key)
End Sub


End Class

Public Class Crypto
Private Shared DES As New TripleDESCryptoServiceProvider
Private Shared MD5 As New MD5CryptoServiceProvider
Public Shared Function MD5Hash(ByVal value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
End Function
Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
DES.Key = Crypto.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
End Function
Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
Try
DES.Key = Crypto.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
MessageBox.Show("Invalid Key", "Decryption Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Function
End Class

_Aerospace_Eng_
09-08-2010, 03:35 PM
This is the wrong forum. This is the php forum, not the vb.net forum.

Fou-Lu
09-08-2010, 04:36 PM
Moving from PHP to ASP.NET.
Unless its purely VB.NET in which case I can remove it to computer programming after.

accwebworks
09-08-2010, 10:27 PM
Are you sure you're encrypting it right?
Key length for TripleDES has to be 128 bits or 192 bits
128bits = 16 chars



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum