• 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         }

  • 相关阅读:
    使用VisualStudio2015开发QT项目
    界面控件
    SmartGit 试用过期
    视图和模型变换
    模型变换和视图变换
    一元二次方程
    论cudnn与cuda之间的关系,和实际例子测试。
    在Ubuntu 18.04上安装Tensorflow
    ubuntu14.04安装CUDA8.0
    Windows10系统远程桌面连接出现卡顿如何解决
  • 原文地址:https://www.cnblogs.com/jason-davis/p/5960636.html
Copyright © 2020-2023  润新知