Results 1 to 8 of 8

Thread: [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Cool [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009

    Updated - August/31/2009

    FYI:
    Someone asked me if one should use the whole algorithm to encrypt or hash; the answer is NO, if you want to encrypt a string (with AES or Rijndael) then just copy the region and paste it in your code, same thing goes for hashing. The code is divided into regions to serve that purpose.

    Updates:
    • Added an enumeration to hold the cryptographic actions that can be performed (encryption or decryption).
    • Added a function to generate an IV from the given key (after playing around with it ).
    • Added an exception handler... Just in case the password isn't the right one.

    Hello,

    I have been working on this for a while now, I have based my work on Jenner's submission. Since I had noticed that it was for VS 2005, I decided to create my own, for VS 2008...

    I have included AES for encryption and used SHA256 for hashing the strings, also some overloads so it'll handle more object types (to permit more "flexibility").

    Even though AES and Rijndael ARE THE SAME ALGORITHMS, they have some particularities:

    • AES:
    • Has a fixed block size of 128bits.
    • Allows key sizes of 128, 192 and 256 bits.
      Rijndael:
    • Allows block sizes of 128bits, 192bits and 256bits.
    • Permits the use of 64, 96, 128, 192, 256 bits key sizes.

    (The block size is specific to each algorithm since it indicates which the size of the IV to be used or that is supported by one of these two algorithms.)

    Some usage examples:

    vb.net Code:
    1. Imports EncryptDecrypt.SymmetricEncryptionAlgorithms.AESManagedEncryption
    2. Imports EncryptDecrypt.SymmetricEncryptionAlgorithms.RijndaelManagedEncryption
    3. Imports EncryptDecrypt.SHAManagedHash.SHA256ManagedHash
    4. Imports System.Text
    5.  
    6. Module Module1
    7.  
    8.     Sub Main()
    9.         Dim StringToBeEncrypted As String = "Hello world!"
    10.         'AES:
    11.         Dim AESEncryptedString As String = AESEncryptDecrypt(StringToBeEncrypted, "vbforums", _
    12.                                                         AESCryptographicAction.Encrypt)
    13.         Console.WriteLine(AESEncryptedString)
    14.         Console.WriteLine(AESEncryptDecrypt(AESEncryptedString, "vbforums", _
    15.                                             AESCryptographicAction.Decrypt))
    16.         'Rijndael:
    17.         Dim StringBytes As Byte() = Encoding.UTF8.GetBytes(StringToBeEncrypted)
    18.         Dim KeyBytes As Byte() = Encoding.UTF8.GetBytes("tassa")
    19.         Dim RijndaelEncryptedString As String = RijndaelEncryptDecrypt(StringBytes, KeyBytes, _
    20.                                                                        RijndaelCryptographicAction.Encrypt)
    21.         Console.WriteLine(RijndaelEncryptedString)
    22.         Console.WriteLine(RijndaelEncryptDecrypt(RijndaelEncryptedString, KeyBytes, _
    23.                                                  RijndaelCryptographicAction.Decrypt))
    24.         'Hash
    25.         Dim salt As Byte() = Nothing
    26.         Dim hashedString As String = SHA256StringHash("vbforums.com", salt)
    27.         Console.WriteLine(hashedString)
    28.         Console.WriteLine(Convert.ToBase64String(salt))
    29.         'Verify the hash
    30.         Dim checkHash As Boolean = VerifyHash("vbforums.com", hashedString, salt)
    31.         If checkHash = True Then
    32.             Console.WriteLine("It's a match!")
    33.         Else
    34.             Console.WriteLine("It's not a match!")
    35.         End If
    36.         Console.ReadKey()
    37.     End Sub
    38.  
    39.  
    40. End Module

    Due to character limitations, the code is in post #2 and #3.

    Comments, critics are appreciated!
    Hope you like it :P .
    Attached Files Attached Files
    Last edited by tassa; Dec 14th, 2009 at 01:55 AM. Reason: Check the update list ;)...
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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