Advertisement
Alisator

Cipher

Jul 17th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. class AEScbcCipher
  2.     {
  3.         private int AESblockSize;
  4.         private byte[] keyByte = new byte[16];
  5.         private byte[] ivByte = new byte[16];
  6.         private string keyString = "xxx";
  7.         private string ivString = "xxx";
  8.  
  9.         public AEScbcCipher(int AESblockSize)
  10.         {
  11.             this.AESblockSize = AESblockSize;
  12.             setKey();
  13.             setIV();
  14.         }
  15.         public AEScbcCipher()
  16.         {
  17.             this.AESblockSize = 16;
  18.             setKey();
  19.             setIV();
  20.         }
  21.  
  22.         private void setKey()
  23.         {
  24.             for (int j = 0; j < keyByte.Length; j += 2)
  25.             {
  26.                 byte result;
  27.                 if (byte.TryParse(keyString.Substring(j, 2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out result) == true)
  28.                 {
  29.                     keyByte[j / 2] = result;
  30.                 }
  31.             }
  32.         }
  33.         private void setIV()
  34.         {
  35.             for (int j = 0; j < ivByte.Length; j += 2)
  36.             {
  37.                 byte result;
  38.                 if (byte.TryParse(ivString.Substring(j, 2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out result) == true)
  39.                 {
  40.                     ivByte[j / 2] = result;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         private byte[] PerformCryptography(ICryptoTransform cryptoTransform, byte[] data)
  46.         {
  47.             using (var memoryStream = new MemoryStream())
  48.             {
  49.                 using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
  50.                 {
  51.                     //I get Exception here
  52.                     cryptoStream.Write(data, 0, data.Length);
  53.                     cryptoStream.FlushFinalBlock();
  54.                     return memoryStream.ToArray();
  55.                 }
  56.             }
  57.         }
  58.  
  59.         public byte[] Encrypt(byte[] data)
  60.         {
  61.             if (data == null || data.Length <= 0)
  62.                 throw new ArgumentNullException("plainText");
  63.             if (keyByte == null || keyByte.Length <= 0)
  64.                 throw new ArgumentNullException("Key");
  65.             if (ivByte == null || ivByte.Length <= 0)
  66.                 throw new ArgumentNullException("IV");
  67.             using (Aes aesAlg = Aes.Create())
  68.             {
  69.                 aesAlg.Key = keyByte;
  70.                 aesAlg.IV = ivByte;
  71.                 using (var encryptor = aesAlg.CreateEncryptor(keyByte, ivByte))
  72.                 {
  73.                     return PerformCryptography(encryptor, data);
  74.                 }
  75.                 //unreachable code
  76.                 return data;
  77.             }
  78.  
  79.         }
  80.  
  81.         public byte[] Decrypt(byte[] data)
  82.         {
  83.             using (Aes aesAlg = Aes.Create())
  84.             {
  85.                 aesAlg.Key = keyByte;
  86.                 aesAlg.IV = ivByte;
  87.                 using (var decryptor = aesAlg.CreateDecryptor(keyByte, ivByte))
  88.                 {
  89.                     return PerformCryptography(decryptor, data);
  90.                 }
  91.                 //unreachable code
  92.                 return data;
  93.             }
  94.         }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement