zoukankan      html  css  js  c++  java
  • C#中用到的加密和解密函数

     添加

    using System.Security.Cryptography;
    using System.Text;
    using System.IO;

    //加密函数

    public static string Encode(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();
        }

    //解密函数

    public static string Decode(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[] 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());
        }

  • 相关阅读:
    数据库unsigned char*类型图片存进
    int main(int argc, char *argv[])中的argc和argv
    数据库任务进度记录
    数据库存入数据后id保持不变,或者直接报错
    数据库图片存入并显示成功,但查询时不能全显示
    数据库存入图片成功但显示不出来
    JWT(Json web token)认证详解
    重启eclips后启动项目出现监听文件找不到
    解决服务器连接错误Host ‘XXX’ is not allowed to connect to this MySQL server
    Linux系统常用命令
  • 原文地址:https://www.cnblogs.com/ypyhy/p/2877074.html
Copyright © 2011-2022 走看看