AES Encryption / Decryption for C# and Java


C# class for AES encryption and decryption that can share result with Java.

Code:
/*
* AesUtil.cs, Utility class for encrypt and decrypt using AES algorithm, CBC mode, 128 bits.
* Author: [email protected]
* Date: 01-Apr-14
* Time: 10:31 AM
*/

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace Text.Util
{
  public class AesUtil
  {
  private ASCIIEncoding textConverter = new ASCIIEncoding();
  private byte[] keyBytes;
  private String keySpec = "thisIsAKey16bits";
  private byte[] ivBytes;
  private String ivSpec = "hereA16bitIVspec";
  private PaddingMode padding = PaddingMode.ISO10126;

  public AesUtil()
  {
  keyBytes = textConverter.GetBytes(keySpec);
  ivBytes = textConverter.GetBytes(ivSpec);
  }

  public AesUtil(String keySpec, String ivspec)
  {
  keyBytes = textConverter.GetBytes(keySpec);
  ivBytes = textConverter.GetBytes(ivSpec);
  }

  private RijndaelManaged getAESCBCCipher() {
  RijndaelManaged cipher =  new RijndaelManaged();
  cipher.KeySize = 128;
  cipher.BlockSize = 128;
  cipher.Mode = CipherMode.CBC;
  cipher.Padding = padding;
  cipher.IV = ivBytes;
  cipher.Key = keyBytes;
  return cipher;
  }

  private byte[] encrypt(RijndaelManaged cipher, byte[] toEncrypt)
  {
  ICryptoTransform encryptor = cipher.CreateEncryptor();
  MemoryStream msEncrypt = new MemoryStream();
  CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
  csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
  csEncrypt.FlushFinalBlock();
  return  msEncrypt.ToArray();
  }

  private byte[] decrypt(RijndaelManaged cipher, byte[] encrypted){
  ICryptoTransform decryptor = cipher.CreateDecryptor();
  MemoryStream msDecrypt = new MemoryStream(encrypted);
  CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
  byte[] fromEncrypt = new byte[encrypted.Length];
  csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
  return fromEncrypt;
  }

  public byte[] getEncryption(byte[] messageBytes){
  RijndaelManaged cipher = getAESCBCCipher();
  return encrypt(cipher, messageBytes);
  }

  public byte[] getDecryption(byte[] encryptedMessageBytes){
  RijndaelManaged decipher = getAESCBCCipher();
  return decrypt(decipher, encryptedMessageBytes);
  }
  }
}

Example result.

Code:
Text to encrypt : Hello C#
Text encrypted : fjvBPTb/B60IY+6l7fRdow==
Text decrypted : Hello C#
Text to decrypt : F6b73SkEneMlRQNSmtF4sA==
Text decrypted 2 : Hello Java

Example code for Java http://www.gotoknow.org/dashboard/home#/posts/567070

คำสำคัญ (Tags): #AES Encryption
หมายเลขบันทึก: 567073เขียนเมื่อ 30 เมษายน 2014 15:34 น. ()แก้ไขเมื่อ 4 มิถุนายน 2014 22:24 น. ()สัญญาอนุญาต: ครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า-ไม่ดัดแปลงจำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

อนุญาตให้แสดงความเห็นได้เฉพาะสมาชิก
พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท