zoukankan      html  css  js  c++  java
  • C# 128位AES 加密解密 (转)

    /// AES加密
    2         /// </summary>
    3         /// <param name="inputdata">输入的数据</param>
    4         /// <param name="iv">向量128位</param>
    5         /// <param name="strKey">加密密钥</param>
    6         /// <returns></returns>
    7         public static byte[] AESEncrypt(byte[] inputdata, byte[] iv, string strKey)
    8         {
    9             //分组加密算法  
    10             SymmetricAlgorithm des = Rijndael.Create();
    11             byte[] inputByteArray = inputdata;//得到需要加密的字节数组      
    12             //设置密钥及密钥向量
    13             des.Key = Encoding.UTF8.GetBytes(strKey.Substring(0, 32));
    14             des.IV = iv;
    15             using (MemoryStream ms = new MemoryStream())
    16             {
    17                 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
    18                 {
    19                     cs.Write(inputByteArray, 0, inputByteArray.Length);
    20                     cs.FlushFinalBlock();
    21                     byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组  
    22                     cs.Close();
    23                     ms.Close();                  
    24                     return cipherBytes;
    25                 }
    26             }
    27         }
    28
    29
    30         /// <summary>
    31         /// AES解密
    32         /// </summary>
    33         /// <param name="inputdata">输入的数据</param>
    34         /// <param name="iv">向量128</param>
    35         /// <param name="strKey">key</param>
    36         /// <returns></returns>
    37         public static byte[] AESDecrypt(byte[] inputdata, byte[] iv, string strKey)
    38         {
    39             SymmetricAlgorithm des = Rijndael.Create();
    40             des.Key = Encoding.UTF8.GetBytes(strKey.Substring(0, 32));
    41             des.IV = iv;
    42             byte[] decryptBytes = new byte[inputdata.Length];
    43             using (MemoryStream ms = new MemoryStream(inputdata))
    44             {
    45                 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
    46                 {
    47                     cs.Read(decryptBytes, 0, decryptBytes.Length);
    48                     cs.Close();
    49                     ms.Close();
    50                 }
    51             }
    52             return decryptBytes;
    53         }

  • 相关阅读:
    297.白盒测试
    301.多媒体讲台使用事项
    289.南信大知网登录
    296.deepin下载安装、root改密、cpu不降频、修改快捷键、创建启动器快捷方式、win文件访问、直接进win无deepin启动项
    295.博客园win&苹果PC客户端开源项目整理
    利用loganalyzer展示MySQL中rsyslog日志
    利用inotify和rsync实现数据的实时同步
    samba服务配置实践
    NFS服务配置实践
    FTP服务配置实践
  • 原文地址:https://www.cnblogs.com/jason-davis/p/5960636.html
Copyright © 2011-2022 走看看