Yang ini dari temanku, Dandi. Memakai Class bawaan .Net yang menggunakan metode Rinjdael. Memakai bahasa C#.
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Collections;
public class AES_Cryptography
{
public const int KEYSIZE_128 = 128;
public const int KEYSIZE_192 = 192;
public const int KEYSIZE_256 = 256;
public const string HASH_MD5 = "MD5";
public const string HASH_SHA1 = "SHA1";
private string getRandomPassword()
{
string strOut = "";
Random rnd = new Random();
for (int i = 0; i < 8; i++)
strOut = strOut + Convert.ToChar(rnd.Next(0, 254)).ToString();
MD5 md5 = new MD5CryptoServiceProvider();
byte[] strTemp = md5.ComputeHash(Encoding.UTF8.GetBytes(strOut));
string sOut = "";
foreach (byte b in strTemp)
{
sOut += String.Format("{0:x2}", b);
}
sOut = sOut.Substring(8, 16);
return sOut;
}
private string getRandomSalt()
{
string strOut = "";
Random rnd = new Random();
for (int i = 0; i < 8; i++)
strOut = strOut + Convert.ToChar(rnd.Next(0, 254)).ToString();
MD5 md5 = new MD5CryptoServiceProvider();
byte[] strTemp = md5.ComputeHash(Encoding.UTF8.GetBytes(strOut));
string sOut = "";
foreach (byte b in strTemp)
{
sOut += String.Format("{0:x2}", b);
}
sOut = sOut.Substring(8, 16);
return sOut;
}
private string getRandomInitVector()
{
string strOut = "";
Random rnd = new Random();
for (int i = 0; i < 8; i++)
strOut = strOut + Convert.ToChar(rnd.Next(0, 254)).ToString();
MD5 md5 = new MD5CryptoServiceProvider();
byte[] strTemp = md5.ComputeHash(Encoding.UTF8.GetBytes(strOut));
string sOut = "";
foreach (byte b in strTemp)
{
sOut += String.Format("{0:x2}", b);
}
sOut = sOut.Substring(8, 16);
return sOut;
}
public string encryptString(string clearText)
{
try
{
string cipherText = "";
string strPass = getRandomPassword();
string strSalt = getRandomSalt();
string strIV = getRandomInitVector();
string cipherOut;
MemoryStream mStream = new MemoryStream();
Rijndael rCrypt = Rijndael.Create();
PasswordDeriveBytes password = new PasswordDeriveBytes(strPass, Encoding.ASCII.GetBytes(strSalt), AES_Cryptography.HASH_MD5, 9);
rCrypt.Key = password.GetBytes(AES_Cryptography.KEYSIZE_256 / 8 ) ;
rCrypt.IV = Encoding.ASCII.GetBytes(strIV);
CryptoStream cStream = new CryptoStream(mStream, rCrypt.CreateEncryptor(rCrypt.Key, rCrypt.IV), CryptoStreamMode.Write);
cStream.Write(Encoding.UTF8.GetBytes(clearText), 0, Encoding.UTF8.GetBytes(clearText).Length);
cStream.FlushFinalBlock();
cipherText = Convert.ToBase64String(mStream.ToArray());
cStream.Close();
mStream.Close();
cipherText = strPass.Substring( 0 , 8 ) + strIV + cipherText + strSalt + strPass.Substring(8, 8 ) ;
cipherOut = cipherText;
return cipherOut;
}
catch (Exception exp)
{
throw new Exception(exp.Message, exp);
}
}
public string decryptString(string cipherText)
{
try
{
MemoryStream mStream = new MemoryStream();
Rijndael rCrypt = Rijndael.Create();
string textOut;
string strOut = "";
string strPass = cipherText.Substring(0, 8 ) + cipherText.Substring(cipherText.Length - 8, 8 ) ;
string strSaltValue = cipherText.Substring(cipherText.Length - (8 + 16), 16);
string strIV = cipherText.Substring(8, 16);
cipherText = cipherText.Substring(24, cipherText.Length - 16 - 16 - 16);
PasswordDeriveBytes password = new PasswordDeriveBytes(strPass, Encoding.ASCII.GetBytes(strSaltValue), AES_Cryptography.HASH_MD5, 9);
rCrypt.Key = password.GetBytes(AES_Cryptography.KEYSIZE_256 / 8 ) ;
rCrypt.IV = Encoding.ASCII.GetBytes(strIV);
CryptoStream cStream = new CryptoStream(mStream, rCrypt.CreateDecryptor(rCrypt.Key, rCrypt.IV), CryptoStreamMode.Write);
cStream.Write(Convert.FromBase64String(cipherText), 0, Convert.FromBase64String(cipherText).Length);
cStream.FlushFinalBlock();
strOut = Encoding.UTF8.GetString(mStream.ToArray());
cStream.Close();
mStream.Close();
textOut = strOut;
return textOut;
}
catch (Exception exp)
{
throw new Exception(exp.Message, exp);
}
}
public string encryptString(string clearText, string passKey)
{
try
{
string cipherText = "";
string strPass = passKey;
string strSalt = getRandomSalt();
string strIV = getRandomInitVector();
string cipherOut;
MemoryStream mStream = new MemoryStream();
Rijndael rCrypt = Rijndael.Create();
PasswordDeriveBytes password = new PasswordDeriveBytes(strPass, Encoding.ASCII.GetBytes(strSalt), AES_Cryptography.HASH_MD5, 9);
rCrypt.Key = password.GetBytes(AES_Cryptography.KEYSIZE_256 / 8 ) ;
rCrypt.IV = Encoding.ASCII.GetBytes(strIV);
CryptoStream cStream = new CryptoStream(mStream, rCrypt.CreateEncryptor(rCrypt.Key, rCrypt.IV), CryptoStreamMode.Write);
cStream.Write(Encoding.UTF8.GetBytes(clearText), 0, Encoding.UTF8.GetBytes(clearText).Length);
cStream.FlushFinalBlock();
cipherText = Convert.ToBase64String(mStream.ToArray());
cStream.Close();
mStream.Close();
cipherText = strIV + cipherText + strSalt;
cipherOut = cipherText;
return cipherOut;
}
catch (Exception exp)
{
throw new Exception(exp.Message, exp);
}
}
public string decryptString(string cipherText, string passKey)
{
try
{
MemoryStream mStream = new MemoryStream();
Rijndael rCrypt = Rijndael.Create();
string textOut;
string strOut = "";
string strPass = passKey;
string strSaltValue = cipherText.Substring(cipherText.Length - (16), 16);
string strIV = cipherText.Substring(0, 16);
cipherText = cipherText.Substring(16, cipherText.Length - 16 - 16);
PasswordDeriveBytes password = new PasswordDeriveBytes(strPass, Encoding.ASCII.GetBytes(strSaltValue), AES_Cryptography.HASH_MD5, 9);
rCrypt.Key = password.GetBytes(AES_Cryptography.KEYSIZE_256 / 8 ) ;
rCrypt.IV = Encoding.ASCII.GetBytes(strIV);
CryptoStream cStream = new CryptoStream(mStream, rCrypt.CreateDecryptor(rCrypt.Key, rCrypt.IV), CryptoStreamMode.Write);
cStream.Write(Convert.FromBase64String(cipherText), 0, Convert.FromBase64String(cipherText).Length);
cStream.FlushFinalBlock();
strOut = Encoding.UTF8.GetString(mStream.ToArray());
cStream.Close();
mStream.Close();
textOut = strOut;
return textOut;
}
catch (Exception exp)
{
throw new Exception(exp.Message, exp);
}
}
}
