Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: decrypt error

  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    decrypt error

    I found some coding with which I can encrypt/decrypt at: http://kellychronicles.spaces.live.c...DBF8!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.

    Code:
    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

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    This is the wrong forum. This is the php forum, not the vb.net forum.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Moving from PHP to ASP.NET.
    Unless its purely VB.NET in which case I can remove it to computer programming after.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Mar 2010
    Location
    nyc
    Posts
    31
    Thanks
    0
    Thanked 1 Time in 1 Post
    Are you sure you're encrypting it right?
    Key length for TripleDES has to be 128 bits or 192 bits
    128bits = 16 chars


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •