zoukankan      html  css  js  c++  java
  • 对称加密AES

     static void Main(string[] args)
            {
                //string str = "rqiJI7eEICT+YZmScpAdbjzLnA4mgL3uU5uHXLBeaE6s8P3noNriVQZ/fO52ar8I9BSkVZIiyP8ArdG/KkXZi5Cn8rZaOVyEjgqhfFlTRIg=";
                string str = "{ "UserID":1,"Email":null,"LoginTime":"2018 - 12 - 12T11: 25:38.6208949 + 08:00"";
                string result = EncodeAES(str, "yhkZvOJSnTHG9hKT", "yhkZvOJSnTHG9hKT");
                Console.WriteLine(result);
                //Console.WriteLine(DecodeAES(str, "ptQJqRKxICCTeo6w", "O3vZvOJSnQDP9hKT"));
                Console.ReadKey();
            }

    加密代码    需要引用System.Security.Cryptography命名空间

    public static string EncodeAES(string text, string key, string iv)
            {
                RijndaelManaged rijndaelCipher = new RijndaelManaged();
                rijndaelCipher.Mode = CipherMode.CBC;
                rijndaelCipher.Padding = PaddingMode.Zeros;
                rijndaelCipher.KeySize = 128;
                rijndaelCipher.BlockSize = 128;
                byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
                byte[] keyBytes = new byte[16];
                int len = pwdBytes.Length;
                if (len > keyBytes.Length)
                    len = keyBytes.Length;
                System.Array.Copy(pwdBytes, keyBytes, len);
                rijndaelCipher.Key = keyBytes;
                rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);
                ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
                byte[] plainText = Encoding.UTF8.GetBytes(text);
                byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
                return Convert.ToBase64String(cipherBytes);
            }

    解密

    public static string DecodeAES(string text, string key, string iv)
    {
    RijndaelManaged rijndaelCipher = new RijndaelManaged();
    rijndaelCipher.Mode = CipherMode.CBC;
    rijndaelCipher.Padding = PaddingMode.Zeros;
    rijndaelCipher.KeySize = 128;
    rijndaelCipher.BlockSize = 128;
    byte[] encryptedData = Convert.FromBase64String(text);
    byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
    byte[] keyBytes = new byte[16];
    int len = pwdBytes.Length;
    if (len > keyBytes.Length)
    len = keyBytes.Length;
    System.Array.Copy(pwdBytes, keyBytes, len);
    rijndaelCipher.Key = keyBytes;
    rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);
    ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
    byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    return Encoding.UTF8.GetString(plainText);
    }

    原:https://blog.csdn.net/blueplus/article/details/80512438

  • 相关阅读:
    LintCode2016年8月22日算法比赛----骰子求和
    LintCode2016年8月22日算法比赛----平面列表
    LintCode2016年8月22日算法比赛----将数组重新排序以构造最小值
    LintCode2016年8月22日算法比赛----克隆二叉树
    Leetcode算法比赛----Longest Absolute File Path
    Leetcode算法比赛----First Unique Character in a String
    vue运行报错Error: listen EADDRNOTAVAIL 192.168.1.105:8080
    vue使用lrz插件压缩图片
    <input type="file">原型难看
    vue创建全局变量以及全局方法
  • 原文地址:https://www.cnblogs.com/fangyyy/p/10333057.html
Copyright © 2011-2022 走看看