Jan 28, 2011 04:42 PM
(This post was last modified: Mar 13, 2011 06:16 AM by Ethereal.)
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#
Usage:
VB .NET
Usage:
I will be updating this, and I hope that it is useful to you.
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 ClassUsage:
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.
μεγαλομανία


