Friday, May 29, 2009

.NET Encryption Using SymmetricAlgorithm & Rijndael

After much research into finding the best way to encrypt and decrypt strings I have come up with a working solution. I have tried other solutions such as one I found on CodeProject that worked sometimes but I would run into the error "Length of the data to decrypt is invalid." this is due to the Public and Private Keys and how they are generated. I have used this sytem to encrypt email body text into a database to secure sensitive information. In this senario the encrypted email data is sent to a centralized MSMQ where it is picked up and logged into a database and then the email is decrypted and sent out. Below is my encryption class that works great.

Imports System

Imports System.Security.Cryptography

Imports System.IO

Imports System.Text


'clsEncryption
is a wrapper of System.Security.Cryptography.SymmetricAlgorithm classes


'and simplifies
the interface. It supports customized SymmetricAlgorithm as well.


Public Class clsEncryption

'================================================================

'MEMBER VARIABLES


'================================================================

Shared encryptionAlgorithm As SymmetricAlgorithm = InitializeEncryptionAlgorithm()


'================================================================


'METHODS


'================================================================


Private Shared Function InitializeEncryptionAlgorithm() As SymmetricAlgorithm

Try


Dim rijaendel As SymmetricAlgorithm = RijndaelManaged.Create()


rijaendel.Key = GetLegalKey("vGCrSnMUBIwJX0+6jc/k2294nmkwWF2fm3B8ImGKqPI=")


rijaendel.IV = New Byte() {&HF, &H6F, &H13, &H2E, &H35, &HC2, &HCD,&HF9,&H5,&H46,&H9C, &HEA, &HA8, &H4B, &H73, &HCC}


Return rijaendel

Catch ex As Exception

Return Nothing


End Try



End Function


Public Function GetKey() As String

Return Convert.ToBase64String(encryptionAlgorithm.Key)

End Function


Public Function GetIV() As String

Return Convert.ToBase64String(encryptionAlgorithm.IV)

End Function

Public Shared Function GetLegalKey(ByVal KeyString As String) As Byte()


Try


Dim rijaendel As SymmetricAlgorithm = RijndaelManaged.Create()

' Adjust key if necessary, and return a valid key

If rijaendel.LegalKeySizes.Length > 0 Then

' Key sizes in bits

Dim keySize As Integer = KeyString.Length * 8

Dim minSize As Integer = rijaendel.LegalKeySizes(0).MinSize

Dim maxSize As Integer = rijaendel.LegalKeySizes(0).MaxSize


Dim skipSize As Integer = rijaendel.LegalKeySizes(0).SkipSize

If keySize > maxSize Then


' Extract maximum size allowed

KeyString = KeyString.Substring(0, maxSize / 8)


ElseIf keySize < color="blue">Then

' Set valid size

Dim validSize As Integer = If((keySize <= minSize), minSize,(keySize - keySize Mod skipSize) + skipSize)

If keySize < color="blue">Then

' Pad the key with asterisk to make up the size

KeyString = KeyString.PadRight(validSize / 8, "*"c)

End If

End If

End If

Return UTF8Encoding.UTF8.GetBytes(KeyString)

Catch ex As Exception

Return Nothing

End Try


End Function


Public Function EncryptString(ByVal clearText As String) As String


Try

'The purpose of this function is to take plain text and encrypt it using rijaendel algorithm

Dim clearTextBytes As Byte() = Encoding.UTF8.GetBytes(clearText)

Dim encrypted As Byte() = encryptionAlgorithm.CreateEncryptor().TransformFinalBlock(clearTextBytes, 0, clearTextBytes.Length)

Return Convert.ToBase64String(encrypted)

Catch ex As Exception

Return ""

End Try

End Function


Public Function DecryptString(ByVal encryptedEncodedText As String) As String


Try

'The purpose of this function is to take encrypted text and decrypt it using rijaendel algorithm


Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedEncodedText)

Dim decryptedBytes As Byte() = encryptionAlgorithm.CreateDecryptor().TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length)

Return Encoding.UTF8.GetString(decryptedBytes)


Catch fe As FormatException

Return ""

Catch e As CryptographicException

Return ""

End Try

End Function


End Class




2 comments:

  1. In the GetLegal Key Function there is a line:
    ElseIf keySize < color="blue">Then

    The color="blue"> should be maxsize

    ReplyDelete
  2. Hello - this works great.

    There is another example on the net that uses rijaendel to first SHA512 the Key and IV before use. Do you know why this might be done and what the purpose is?

    Regards,

    Ben

    ReplyDelete