Results 1 to 13 of 13

Thread: Anyone know how to Encrypt a String via 128+-bit Encryption?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    11

    Question Anyone know how to Encrypt a String via 128+-bit Encryption?

    Hey guys. Me one more time, except I am not sure if there is such thing in VB.NET via 128-bit Encryption or more. I only know of 64-bit, can someone please teach me? Here is my code for 64-bit:

    Encryption:
    Code:
    Public Function EncryptXString(ByVal sData As String) As String
            Dim mSHA As New SHA1Managed
            Dim dString() As Byte = ASCIIEncoding.ASCII.GetBytes(sData)
            Dim cString As String = Convert.ToBase64String(dString)
            Convert.ToBase64String(mSHA.ComputeHash(Encoding.ASCII.GetBytes(sData)))
            EncryptXString = cString
        End Function
    And Decryption:
    Code:
        Public Function FindXString(ByVal sData As String) As String
            Dim dData() As Byte = Convert.FromBase64String(sData)
            Dim dString As String = ASCIIEncoding.ASCII.GetString(dData)
            FindXString = dString
        End Function
    Teach me... I want to know the truth, please.

  2. #2
    Lively Member
    Join Date
    Aug 2010
    Location
    Look to the right... yep, there i am.
    Posts
    111

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    You could try using AES,
    Code:
    Function AESEncrypit(ByVal PlainText As String, ByVal Password As String)
            Dim salt As String = "xXEmoNinjaUnicornXx" 'Mostly a Second Password ^_^
            Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
            Dim PasswordIterations As String = 2
            Dim InitialVector As String = "OFRna73m*aze01xY" 'This should be a string of 16 ASCII characters.
            Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
    
    
    
            If (String.IsNullOrEmpty(PlainText)) Then
                Return ""
                Exit Function
            End If
            Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
            Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
            Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText)
            Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
            Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
            Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
            SymmetricKey.Mode = CipherMode.CBC
    
            Dim CipherTextBytes As Byte() = Nothing
            Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
                Using MemStream As New MemoryStream()
                    Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
                        CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
                        CryptoStream.FlushFinalBlock()
                        CipherTextBytes = MemStream.ToArray()
                        MemStream.Close()
                        CryptoStream.Close()
                    End Using
                End Using
            End Using
            SymmetricKey.Clear()
            Return Convert.ToBase64String(CipherTextBytes)
        End Function
    To use this:
    Code:
    Dim aesEncrypted As String = AESEncrypit("TEXT TO ENCRYPT HERE", "PASSWORD-for-ENCRYPTION")
    MessageBox.Show(aesEncrypted)
    If you want decrypt function i'll type it out.
    Last edited by n00bl3z; Apr 23rd, 2011 at 07:35 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    11

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    Sweet mother of pearl, nice code you just typed up for me! Really appreciated thanks a ton! I'd +rep you if I could

  4. #4
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    I am interested in using this encryption function...however some of the variables arn't declared. ie Encoding and PasswordDeriveBytes

    I presume i need to implement some information.

    Can I please have the full code to use this function?

    thanks

  5. #5
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    Imports System.Security.Cryptography

    http://msdn.microsoft.com/en-us/libr...ptography.aspx for more info
    Remember: An encryption is not much of a use, if you can't decrypt it.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    I remember a post not too long ago from someone who managed to make a compression routine that could make the file about 95% of its original size. He had worked on the method for ages and released the code. He stopped before making the decompression routine as he was too busy .

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    Quote Originally Posted by Radjesh Klauke View Post
    Remember: An encryption is not much of a use, if you can't decrypt it.
    I have to disagree with you on this statement. There are many uses for irreversible encryption. One of the more common use of these one-way encryption algorithms is to encrypt a password.They are also use to compare files. Just Goolge for "MD5 usage" and you'll be hit by the millions... Consider they are just tools and therefore, you should pick the right tool for the right job.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    Quote Originally Posted by Grimfort View Post
    I remember a post not too long ago from someone who managed to make a compression routine that could make the file about 95% of its original size. He had worked on the method for ages and released the code. He stopped before making the decompression routine as he was too busy .
    Hahaha, I remember that too.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9
    New Member
    Join Date
    Sep 2011
    Posts
    1

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    After googling I found the same exact code all over the place, so here are both encrypt and decrypt functions along with how to use them. I had to do this because I couldn't find the decrypt code here on this website and I also was getting a error on this line of code:
    PHP Code:
    AES.Mode Security.Cryptography.CipherMode.ECB 
    I had to add "System." in front of Security. Now it looks like this:
    PHP Code:
    AES.Mode System.Security.Cryptography.CipherMode.ECB 
    (And it works! ) I also had to get rid of the "Try" statements on both decrypt and decrypt functions... or the End Functions would turn green in visual studio 2010... But hey its working now, no errors and it works.

    I thought this would help out people like myself, so I hope it does.

    1st • Make a new project, import:
    Imports MySql.Data.MySqlClient
    Imports System.Text
    Imports System.IO


    2nd • add some textboxes named: TextBoxinput.text, TextBoxoutput and TextBoxSalt.text

    3rd add 2 buttons named: Button1 and Button2

    Button1 (encrypt code)
    PHP Code:
     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Button1.Click

            
    'encrypt

            Dim aesEncrypted As String = AES_Encrypt(TextBoxinput.Text, TextBoxSalt.Text)
            TextBoxoutput.Text = aesEncrypted

        End Sub 
    Button2 (decrypt code)
    PHP Code:
     Private Sub Button2_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Button2.Click

            
    'decrypt

            Dim aesDecrypted As String = AES_Decrypt(TextBoxoutput.Text, TextBoxSalt.Text)
            TextBoxinput.Text = aesDecrypted

        End Sub 

    4th copy and paste the 2 functions below into your project

    PHP Code:

        
    Public Function AES_Decrypt(ByVal input As StringByVal pass As String) As String

            Dim AES 
    As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES 
    As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim decrypted 
    As String ""

            
    Dim hash(31) As Byte
            Dim temp 
    As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.
    Copy(temp0hash016)
            Array.
    Copy(temp0hash1516)
            
    AES.Key hash
            AES
    .Mode Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter 
    As System.Security.Cryptography.ICryptoTransform AES.CreateDecryptor
            Dim Buffer 
    As Byte() = Convert.FromBase64String(input)
            
    decrypted System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer0Buffer.Length))
            Return 
    decrypted

        End 
    Function

        Public Function 
    AES_Encrypt(ByVal input As StringByVal pass As String) As String

            Dim AES 
    As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES 
    As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted 
    As String ""

            
    Dim hash(31) As Byte
            Dim temp 
    As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.
    Copy(temp0hash016)
            Array.
    Copy(temp0hash1516)
            
    AES.Key hash
            AES
    .Mode Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter 
    As System.Security.Cryptography.ICryptoTransform AES.CreateEncryptor
            Dim Buffer 
    As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            
    encrypted Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer0Buffer.Length))
            Return 
    encrypted

        End 
    Function


        
    End Class 

    Here is the complete form1 code:
    PHP Code:
    Imports MySql.Data.MySqlClient
    Imports System
    .Text
    Imports System
    .IO



    Public Class Form1

      
       

        
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Button1.Click

            
    'encrypt

            Dim aesEncrypted As String = AES_Encrypt(TextBoxinput.Text, TextBoxSalt.Text)
            TextBoxoutput.Text = aesEncrypted

        End Sub

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

            '
    decrypt

            Dim aesDecrypted 
    As String AES_Decrypt(TextBoxoutput.TextTextBoxSalt.Text)
            
    TextBoxinput.Text aesDecrypted

        End Sub

        
    Public Function AES_Decrypt(ByVal input As StringByVal pass As String) As String

            Dim AES 
    As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES 
    As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim decrypted 
    As String ""

            
    Dim hash(31) As Byte
            Dim temp 
    As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.
    Copy(temp0hash016)
            Array.
    Copy(temp0hash1516)
            
    AES.Key hash
            AES
    .Mode Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter 
    As System.Security.Cryptography.ICryptoTransform AES.CreateDecryptor
            Dim Buffer 
    As Byte() = Convert.FromBase64String(input)
            
    decrypted System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer0Buffer.Length))
            Return 
    decrypted

        End 
    Function

        Public Function 
    AES_Encrypt(ByVal input As StringByVal pass As String) As String

            Dim AES 
    As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES 
    As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted 
    As String ""

            
    Dim hash(31) As Byte
            Dim temp 
    As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.
    Copy(temp0hash016)
            Array.
    Copy(temp0hash1516)
            
    AES.Key hash
            AES
    .Mode Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter 
    As System.Security.Cryptography.ICryptoTransform AES.CreateEncryptor
            Dim Buffer 
    As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            
    encrypted Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer0Buffer.Length))
            Return 
    encrypted

        End 
    Function


        
    End Class 
    Last edited by cwd1; Sep 25th, 2011 at 04:25 AM.

  10. #10
    Lively Member
    Join Date
    Aug 2010
    Location
    Look to the right... yep, there i am.
    Posts
    111

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    Hello, Several of you have sent me PMs asking for the decryption code. It was stupid of me to post the encrypt code and not the decryption code. So, Here it is -- Fixed typos in Encrypt function and made it easier to use with Decrypt function.

    Encrypt:
    Code:
        Function AESEncrypt(ByVal PlainText As String, ByVal Password As String, ByVal salt As String)
            Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
            Dim PasswordIterations As String = 2
            Dim InitialVector As String = "OFRna73m*aze01xY" 'This should be a string of 16 ASCII characters.
            Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
    
    
    
            If (String.IsNullOrEmpty(PlainText)) Then
                Return ""
                Exit Function
            End If
            Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
            Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
            Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText)
            Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
            Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
            Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
            SymmetricKey.Mode = CipherMode.CBC
    
            Dim CipherTextBytes As Byte() = Nothing
            Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
                Using MemStream As New MemoryStream()
                    Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
                        CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
                        CryptoStream.FlushFinalBlock()
                        CipherTextBytes = MemStream.ToArray()
                        MemStream.Close()
                        CryptoStream.Close()
                    End Using
                End Using
            End Using
            SymmetricKey.Clear()
            Return Convert.ToBase64String(CipherTextBytes)
        End Function
    Decrypt:
    Code:
        Function AESDecrypt(ByVal CipherText As String, ByVal password As String, ByVal salt As String) As String
            Dim HashAlgorithm As String = "SHA1"
            Dim PasswordIterations As String = 2
            Dim InitialVector As String = "OFRna73m*aze01xY"
            Dim KeySize As Integer = 256
    
            If (String.IsNullOrEmpty(CipherText)) Then
                Return ""
            End If
            Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
            Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
            Dim CipherTextBytes As Byte() = Convert.FromBase64String(CipherText)
            Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations)
            Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
            Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
            SymmetricKey.Mode = CipherMode.CBC
            Dim PlainTextBytes As Byte() = New Byte(CipherTextBytes.Length - 1) {}
    
            Dim ByteCount As Integer = 0
    
            Using Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)
                Using MemStream As MemoryStream = New MemoryStream(CipherTextBytes)
                    Using CryptoStream As CryptoStream = New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)
                        ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length)
                        MemStream.Close()
                        CryptoStream.Close()
                    End Using
                End Using
            End Using
            SymmetricKey.Clear()
            Return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount)
        End Function
    Usage:
    Code:
            Dim encryptedString As String = AESEncrypt("This is a plain string!!!", "password", "xXEmoNinjaUnicornXx")
            MessageBox.Show(encryptedString)
            Dim decryptedString As String = AESDecrypt(encryptedString, "password", "xXEmoNinjaUnicornXx")
            MessageBox.Show(decryptedString)

  11. #11
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    Ola,

    I tried the latest code from n00bl3z, but since I have Option Strict On there are several issues and was wondering if anyone could help with this. I've already fixed some error, but a few remain:

    vb.net Code:
    1. Imports System.Security.Cryptography
    2. Imports System.Security.Cryptography.CryptoStream
    3. Imports System.Text
    4. Imports System.IO
    5.  
    6. Function AESEncrypt(ByVal PlainText As String, ByVal Password As String, ByVal salt As String) As String
    7.  
    8.         Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
    9.         Dim PasswordIterations As String = CStr(2)
    10.         Dim InitialVector As String = "OFRna73m*aze01xY" 'This should be a string of 16 ASCII characters.
    11.         Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
    12.  
    13.         If (String.IsNullOrEmpty(PlainText)) Then
    14.             Return ""
    15.             Exit Function
    16.         End If
    17.  
    18.         Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
    19.         Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
    20.         Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText)
    21.        
    22. Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
    23. 'Error  1   Overload resolution failed because no accessible 'New' can be called with these arguments:
    24.         'Public Sub New(password() As Byte, salt() As Byte, hashName As String, iterations As Integer)': Value of type 'String' cannot be converted to '1-dimensional array of Byte'.
    25.         'Public Sub New(password() As Byte, salt() As Byte, hashName As String, iterations As Integer)': Option Strict On disallows implicit conversions from 'String' to 'Integer'.
    26.         'Public Sub New(strPassword As String, rgbSalt() As Byte, strHashName As String, iterations As Integer)': Option Strict On disallows implicit conversions from 'String' to 'Integer'.
    27.  
    28.        
    29. Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8))
    30. 'Warning    2   'Public Overrides Function GetBytes(cb As Integer) As Byte()' is obsolete: 'Rfc2898DeriveBytes replaces PasswordDeriveBytes for deriving key material from a password and is preferred in new applications.'.
    31.        
    32.  
    33.  
    34.         Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
    35.         SymmetricKey.Mode = CipherMode.CBC
    36.  
    37.         Dim CipherTextBytes As Byte() = Nothing
    38.         Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
    39.             Using MemStream As New MemoryStream()
    40.                 Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
    41.                     CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
    42.                     CryptoStream.FlushFinalBlock()
    43.                     CipherTextBytes = MemStream.ToArray()
    44.                     MemStream.Close()
    45.                     CryptoStream.Close()
    46.                 End Using
    47.             End Using
    48.         End Using
    49.         SymmetricKey.Clear()
    50.         Return Convert.ToBase64String(CipherTextBytes)
    51.     End Function

    Thanks in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  12. #12
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    A question:

    Dim KeySize As Integer = 256 'Can be 128, 192, or 256.

    I presume this is the encryption strength? Is this correct?
    Last edited by Simon Canning; Dec 16th, 2011 at 12:18 AM.

  13. #13
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Anyone know how to Encrypt a String via 128+-bit Encryption?

    You have a combination lock with 2, 4 or 8 digits, which is harder to crack . This article has an interesting point however.

Posting Permissions

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



Click Here to Expand Forum to Full Width