AES Encryption/Decryption Interop for C#

Go down

AES Encryption/Decryption Interop for C# Empty AES Encryption/Decryption Interop for C#

ตั้งหัวข้อ by Admin on Tue Apr 01, 2014 9:17 am

AES Utility Class.
Code:
/*
* AesUtil.cs, Utility class for encrypt and decrypt using AES algorithm, CBC mode, 128 bits.
* Author: psupawa@gmail.com
* 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
 
For Java example please goto Java Programming forum. http://maithai.thai-forum.net/t4-topic

Admin
Admin

จำนวนข้อความ : 13
Join date : 29/01/2014

http://maithai.thai-forum.net

ขึ้นไปข้างบน Go down

ขึ้นไปข้างบน


 
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ