Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[C#][VB .NET]Customizable AES Class - Now with Byte Encryption!

On request, I converted the C# class that I use to VB .NET. It probably isn't the neatest or most efficient, but it works, and that is what matters.

The main reason to use this over other classes is customizability. In my Notepad projects, I allow the user to customize their encryption with different FileTypes for increased flexibility.

Anyway, without further ado, here it is.

The closest source to the original that I found was this, so current credits go to James Craig of GutGames.com. However, I don't think that his post was the original. I didn't remove any credits from my source.

Update 29/1/11: Small indentation, and Using added.
Update 30/1/11: VB .NET Byte encryption added, I'll port it to C# when I can be bothered :) If you get any errors, please notify me.
Update 11/03/11: C# updated on request. I never got round to it before.
Update 12/03/11: Simple bug fix that I missed. C# is now working.

C#
Code
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System;

public class AESEncryption
{
    //Translated by Ethereal of HackForums.net
    //Leave these credits - they aren't hurting anybody.

    #region "Static Functions"

    int ByteCount = 0;
    public static string Encrypt(string PlainText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
    {
  try
  {
    if (string.IsNullOrEmpty(PlainText))
    {
    return "";
    }
    return Convert.ToBase64String(Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(PlainText), Password, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize));
  }
  catch (Exception ex)
  {
    throw;
  }

    }

    public static byte[] Encrypt(byte[] Data, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
    {
  try
  {
    byte[] InitialVectorBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(InitialVector);
    byte[] SaltValueBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Salt);
    byte[] PlainTextBytes = Data;
    System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
    byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
    System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged();
    SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC;
    byte[] CipherTextBytes = null;
    using (System.Security.Cryptography.ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
    {
    using (System.IO.MemoryStream MemStream = new System.IO.MemoryStream())
    {
    using (System.Security.Cryptography.CryptoStream CStream = new CryptoStream(MemStream, Encryptor, System.Security.Cryptography.CryptoStreamMode.Write))
    {
    CStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
    CStream.FlushFinalBlock();
    CipherTextBytes = MemStream.ToArray();
    MemStream.Close();
    CStream.Close();
    }
    }
    }
    SymmetricKey.Clear();
    return CipherTextBytes;
  }
  catch (Exception ex)
  {
    throw;
  }
    }
    public static object Decrypt(string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
    {
  try
  {
    if (string.IsNullOrEmpty(CipherText))
    {
    return "";
    }
    return System.Text.Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(CipherText), Password, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize));
  }
  catch (Exception ex)
  {
    throw;
  }
    }

    public static byte[] Decrypt(byte[] Data, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
    {
  try
  {
    byte[] InitialVectorBytes = System.Text.Encoding.ASCII.GetBytes(InitialVector);
    byte[] SaltValueBytes = System.Text.Encoding.ASCII.GetBytes(Salt);
    byte[] CipherTextBytes = Data;
    System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
    byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
    System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged();
    SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC;
    byte[] PlainTextBytes = new byte[CipherTextBytes.Length + 1];
    using (System.Security.Cryptography.ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
    {
    using (System.IO.MemoryStream MemStream = new System.IO.MemoryStream(CipherTextBytes))
    {
    using (System.Security.Cryptography.CryptoStream CStream = new CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
    {
    MemStream.Close();
    CStream.Close();
    }
    }
    }
    SymmetricKey.Clear();
    return PlainTextBytes;
  }
  catch (Exception ex)
  {
    throw;
  }
    }
    #endregion
}

Usage:
Code
AESEncryption.Encrypt(txtBox.Text, "This.Is.Your.Pass", "This_Is_Your_Salt", "SHA1", 2, "ThisIs16chars123", 256);
Convert.ToString(AESEncryption.Decrypt(encryptedtext,"This.Is.Your.Pass", "This_Is_Your_Salt", "SHA1", 2, "ThisIs16chars123", 256));

VB .NET
Code
Public Class AESEncryption
    'Translated by Ethereal of HackForums.net
    'Leave these credits - they aren't hurting anybody.

#Region "Static Functions"
    Shared ByteCount As Integer = 0

    Public Shared Function Encrypt(ByVal PlainText As String, ByVal Password As String, ByVal Salt As String, ByVal HashAlgorithm As String, ByVal PasswordIterations As Integer, ByVal InitialVector As String, ByVal KeySize As Integer) As String
  Try
    If String.IsNullOrEmpty(PlainText) Then
    Return ""
    End If
    Return Convert.ToBase64String(Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(PlainText), Password, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize))
  Catch ex As Exception
    Throw
  End Try

    End Function

    Public Shared Function Encrypt(ByVal Data As Byte(), ByVal Password As String, ByVal Salt As String, ByVal HashAlgorithm As String, ByVal PasswordIterations As Integer, ByVal InitialVector As String, ByVal KeySize As Integer) As Byte()
  Try
    Dim InitialVectorBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(InitialVector)
    Dim SaltValueBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(Salt)
    Dim PlainTextBytes As Byte() = Data
    Dim DerivedPassword As New System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
    Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
    Dim SymmetricKey As New System.Security.Cryptography.RijndaelManaged()
    SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC
    Dim CipherTextBytes As Byte() = Nothing
    Using Encryptor As System.Security.Cryptography.ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
    Using MemStream As System.IO.MemoryStream = New System.IO.MemoryStream()
    Using CStream As System.Security.Cryptography.CryptoStream = New CryptoStream(MemStream, Encryptor, System.Security.Cryptography.CryptoStreamMode.Write)
    CStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
    CStream.FlushFinalBlock()
    CipherTextBytes = MemStream.ToArray()
    MemStream.Close()
    CStream.Close()
    End Using
    End Using
    End Using
    SymmetricKey.Clear()
    Return CipherTextBytes
  Catch ex As Exception
    Throw
  End Try
    End Function
    Public Shared Function Decrypt(ByVal CipherText As String, ByVal Password As String, ByVal Salt As String, ByVal HashAlgorithm As String, ByVal PasswordIterations As Integer, ByVal InitialVector As String, ByVal KeySize As Integer)
  Try
    If String.IsNullOrEmpty(CipherText) Then
    Return ""
    End If
    Return System.Text.Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(CipherText), Password, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize), 0, ByteCount)
  Catch ex As Exception
    Throw
  End Try
    End Function

    Public Shared Function Decrypt(ByVal Data As Byte(), ByVal Password As String, ByVal Salt As String, ByVal HashAlgorithm As String, ByVal PasswordIterations As Integer, ByVal InitialVector As String, ByVal KeySize As Integer) As Byte()
  Try
    Dim InitialVectorBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(InitialVector)
    Dim SaltValueBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(Salt)
    Dim CipherTextBytes = Data
    Dim DerivedPassword As New System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
    Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
    Dim SymmetricKey As New System.Security.Cryptography.RijndaelManaged()
    SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC
    Dim PlainTextBytes(CipherTextBytes.Length) As Byte
    Using Decryptor As System.Security.Cryptography.ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)
    Using MemStream As System.IO.MemoryStream = New System.IO.MemoryStream(CipherTextBytes)
    Using CStream As System.Security.Cryptography.CryptoStream = New CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read)
    ByteCount = CStream.Read(PlainTextBytes, 0, PlainTextBytes.Length)
    MemStream.Close()
    CStream.Close()
    End Using
    End Using
    End Using
    SymmetricKey.Clear()
    Return PlainTextBytes
  Catch ex As Exception
    Throw
  End Try
    End Function
#End Region
End Class

Usage:
Code
AESEncryption.Encrypt(txtBox.Text, "This.Is.Your.Pass", "This_Is_Your_Salt", "SHA1", 2, "ThisIs16chars123", 256);
AESEncryption.Decrypt(encryptedtext,"This.Is.Your.Pass", "This_Is_Your_Salt", "SHA1", 2, "ThisIs16chars123", 256);

I will be updating this, and I hope that it is useful to you.
μεγαλομανία
Respect for this man
Very nice coded! thanks
You should credit the original creator of that code.
(Jan 28, 2011 04:56 PM)Zer0Fact0r Wrote:  You should credit the original creator of that code.

If I can find the source, I will.
μεγαλομανία
(Jan 28, 2011 04:57 PM)Ethereal Wrote:  If I can find the source, I will.
original creator... James Craig
(Jan 28, 2011 05:01 PM)DragonHunter Wrote:  original creator... James Craig

Whatever, it seems like fairly generic code. Although I think it originated from this: http://www.obviex.com/samples/Code.aspx?...&Lang=C%23
(Jan 28, 2011 05:01 PM)DragonHunter Wrote:  original creator... James Craig

I already updated the OP.

However, the source I originally obtained the class from did not have the credits at the top, nor the Namespace or Imports. The original source was purely the class.

(Jan 28, 2011 05:05 PM)Zer0Fact0r Wrote:  Whatever, it seems like fairly generic code. Although I think it originated from this: http://www.obviex.com/samples/Code.aspx?...&Lang=C%23

That looks likely.

However, this thread was not to discuss the origins of the C# implementation; It was to share a VB .NET version.
μεγαλομανία
It doesn't work for my crypter,
an error about the string/byte, but encryption is nice thanks
hopefully you can fix it
(Jan 29, 2011 08:48 AM)skxprive Wrote:  It doesn't work for my crypter,
an error about the string/byte, but encryption is nice thanks
hopefully you can fix it

Because it takes a string input, and returns one.

I will be adding a byte only overload. Whoever one-starred this thread is a douche.
μεγαλομανία
(Jan 29, 2011 08:51 AM)Ethereal Wrote:  Because it takes a string input, and returns one.

I will be adding a byte only overload. Whoever one-starred this thread is a douche.

Yes that will be it

What do you mean? I will be glad if you could code it to make byte to byte and not only string
You didnt only translate it, but also improved it
(Jan 29, 2011 08:54 AM)skxprive Wrote:  Yes that will be it

What do you mean? I will be glad if you could code it to make byte to byte and not only string
You didnt only translate it, but also improved it

I am adding an overload(which means you can enter both a string, or a byte[]) which will solve this problem.
μεγαλομανία
Oh okay Thanks I now I know what it is :D

thats awesome
ive been trying to make it. no success :) that failed miserably
VB .NET code updated for Byte encryption. Please notify me of any errors you may encounter.

C# will be updated when I can be bothered :)
μεγαλομανία
Really nice job, excellent


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Julius013Mar 2, 2022 05:12 PM 4 311 Mar 2, 2022 05:12 PM
Last Post: Julius013
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[C] by neonbytesJan 8, 2021 04:09 PM 10 346 Jan 8, 2021 04:09 PM
Last Post: poker