欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#代码实现对AES加密解密

程序员文章站 2022-07-06 12:43:48
es(the advanced encryption standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和*数...

es(the advanced encryption standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和*数字信息的方法。

本文实例为大家介绍c#实现对aes加密解密的详细代码,分享给大家供大家参考,具体内容如下

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.security.cryptography; 
using system.io; 
 
 
namespace aesdemo 
{ 
 public static class aeshelper 
 { 
  /// <summary> 
  /// aes加密 
  /// </summary> 
  /// <param name="data">被加密的明文</param> 
  /// <param name="key">密钥</param> 
  /// <param name="vector">向量</param> 
  /// <returns>密文</returns> 
  public static byte[] aesencrypt(byte[] data, string key, string vector) 
  { 
   byte[] bkey = new byte[32]; 
   array.copy(encoding.utf8.getbytes(key.padright(bkey.length)), bkey, bkey.length); 
   byte[] bvector = new byte[16]; 
   array.copy(encoding.utf8.getbytes(vector.padright(bvector.length)), bvector, bvector.length); 
 
 
   byte[] cryptograph = null; // 加密后的密文 
 
 
   rijndael aes = rijndael.create(); 
   try 
   { 
    // 开辟一块内存流 
    using (memorystream memory = new memorystream()) 
    { 
     // 把内存流对象包装成加密流对象 
     using (cryptostream encryptor = new cryptostream(memory, 
      aes.createencryptor(bkey, bvector), 
      cryptostreammode.write)) 
     { 
      // 明文数据写入加密流 
      encryptor.write(data, 0, data.length); 
      encryptor.flushfinalblock(); 
 
 
      cryptograph = memory.toarray(); 
     } 
    } 
   } 
   catch 
   { 
    cryptograph = null; 
   } 
 
 
   return cryptograph; 
  } 
 
 
  /// <summary> 
  /// aes解密 
  /// </summary> 
  /// <param name="data">被解密的密文</param> 
  /// <param name="key">密钥</param> 
  /// <param name="vector">向量</param> 
  /// <returns>明文</returns> 
  public static byte[] aesdecrypt(byte[] data, string key, string vector) 
  { 
   byte[] bkey = new byte[32]; 
   array.copy(encoding.utf8.getbytes(key.padright(bkey.length)), bkey, bkey.length); 
   byte[] bvector = new byte[16]; 
   array.copy(encoding.utf8.getbytes(vector.padright(bvector.length)), bvector, bvector.length); 
 
 
   byte[] original = null; // 解密后的明文 
 
 
   rijndael aes = rijndael.create(); 
   try 
   { 
    // 开辟一块内存流,存储密文 
    using (memorystream memory = new memorystream(data)) 
    { 
     // 把内存流对象包装成加密流对象 
     using (cryptostream decryptor = new cryptostream(memory, 
     aes.createdecryptor(bkey, bvector), 
     cryptostreammode.read)) 
     { 
      // 明文存储区 
      using (memorystream originalmemory = new memorystream()) 
      { 
       byte[] buffer = new byte[1024]; 
       int32 readbytes = 0; 
       while ((readbytes = decryptor.read(buffer, 0, buffer.length)) > 0) 
       { 
        originalmemory.write(buffer, 0, readbytes); 
       } 
 
 
       original = originalmemory.toarray(); 
      } 
     } 
    } 
   } 
   catch 
   { 
    original = null; 
   } 
 
 
   return original; 
  } 
 } 
} 
 
 
不使用向量的方式: 
public static class aescrypto 
 { 
/// <summary> 
/// iv向量为固定值 
/// </summary> 
  //private static byte[] _iv = { 
  // 85, 60, 12, 116, 
  // 99, 189, 173, 19, 
  // 138, 183, 232, 248, 
  // 82, 232, 200, 242 
  //}; 
 
 
  public static byte[] decrypt(byte[] encryptedbytes, byte[] key) 
  { 
memorystream mstream = new memorystream( encryptedbytes ); 
//mstream.write( encryptedbytes, 0, encryptedbytes.length ); 
//mstream.seek( 0, seekorigin.begin ); 
rijndaelmanaged aes = new rijndaelmanaged( ); 
   aes.mode = ciphermode.ecb; 
   aes.padding = paddingmode.pkcs7; 
   aes.keysize = 128; 
aes.key = key; 
//aes.iv = _iv; 
cryptostream cryptostream = new cryptostream( mstream, aes.createdecryptor( ), cryptostreammode.read ); 
try { 
 
 
byte[] tmp = new byte[ encryptedbytes.length + 32 ]; 
int len = cryptostream.read( tmp, 0, encryptedbytes.length + 32 ); 
byte[] ret = new byte[ len ]; 
array.copy( tmp, 0, ret, 0, len ); 
return ret; 
} 
finally { 
cryptostream.close( ); 
mstream.close( ); 
aes.clear( ); 
} 
} 
 
 
  public static byte[] encrypt(byte[] plainbytes, byte[] key) 
  { 
   memorystream mstream = new memorystream(); 
   rijndaelmanaged aes = new rijndaelmanaged(); 
 
 
   aes.mode = ciphermode.ecb; 
   aes.padding = paddingmode.pkcs7; 
   aes.keysize = 128; 
   //aes.key = _key; 
 
 
   aes.key = key; 
   //aes.iv = _iv; 
   cryptostream cryptostream = new cryptostream(mstream, aes.createencryptor(), cryptostreammode.write); 
   try 
   { 
    cryptostream.write(plainbytes, 0, plainbytes.length); 
    cryptostream.flushfinalblock(); 
    return mstream.toarray(); 
   } 
   finally 
   { 
    cryptostream.close(); 
    mstream.close(); 
    aes.clear(); 
   } 
  } 
 }

希望通过这篇文章大家对aes加密解密有所了解,对c#程序设计有所帮助。