zoukankan      html  css  js  c++  java
  • 加密解密

       /// <summary>
            /// 带有密钥的加解密
            /// 加密
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string KeyEncrypt(string str,string key)
            {
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                StringBuilder builder = new StringBuilder();
                foreach (byte num in stream.ToArray())
                {
                    builder.AppendFormat("{0:X2}", num);
                }
                stream.Close();
                return builder.ToString();
            }


         /// <summary>
            /// 带有密钥的加解密
            /// 解密
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string KeyDecrypt(string str,string key)
            {
                try
                {
                    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                    provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                    provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
                    byte[] buffer = new byte[str.Length / 2];
                    for (int i = 0; i < (str.Length / 2); i++)
                    {
                        int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
                        buffer[i] = (byte)num2;
                    }
                    MemoryStream stream = new MemoryStream();
                    CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.FlushFinalBlock();
                    stream.Close();
                    return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
                }
                catch
                {
                    return "";
                }
            }

  • 相关阅读:
    366. Find Leaves of Binary Tree输出层数相同的叶子节点
    716. Max Stack实现一个最大stack
    515. Find Largest Value in Each Tree Row查找一行中的最大值
    364. Nested List Weight Sum II 大小反向的括号加权求和
    156. Binary Tree Upside Down反转二叉树
    698. Partition to K Equal Sum Subsets 数组分成和相同的k组
    244. Shortest Word Distance II 实现数组中的最短距离单词
    187. Repeated DNA Sequences重复的DNA子串序列
    java之hibernate之基于主键的双向一对一关联映射
    java之hibernate之基于主键的单向一对一关联映射
  • 原文地址:https://www.cnblogs.com/paste/p/2029015.html
Copyright © 2011-2022 走看看