• C#中的3DES加密和MD5加密


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

    namespace Interact.TeleSales
    {
    /// <summary>
    /// DesCrypt 的摘要说明。
    /// </summary>
    public class DesCrypt
    {
    public DesCrypt()
    {

    }
    private TripleDESCryptoServiceProvider mCSP = null;
    private string sKey = "";
    private string sIV = "";

    public void Init(string key,string iv)
    {
    mCSP = new TripleDESCryptoServiceProvider();
    mCSP.Mode = CipherMode.CBC;
    mCSP.Padding = PaddingMode.PKCS7;

    sKey = key;
    sIV = iv;
    byte[] byk = Encoding.ASCII.GetBytes(sKey);
    mCSP.Key = byk;
    byte[] byi = Encoding.ASCII.GetBytes(sIV);
    mCSP.IV = byi;

    }
    //退款接口特别之处:文档为utf8,但是3des加密和md5加密使用gb2312来做
    public string EncryptString(string Value)
    {
    return EncryptString(Value,"utf-8");
    }
    public string EncryptString(string Value,string encodingname)
    {
    ICryptoTransform ct;
    MemoryStream ms;
    CryptoStream cs;
    byte[] byt;
    ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
    //byt = Encoding.UTF8.GetBytes(Value);
    Encoding enc = GetEncoding(encodingname);
    byt = enc.GetBytes(Value);

    ms = new MemoryStream();
    cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
    cs.Write(byt, 0, byt.Length);
    cs.FlushFinalBlock();
    cs.Close();
    return Convert.ToBase64String(ms.ToArray());
    }
    public string DecryptString(string Value)
    {
    ICryptoTransform ct;
    MemoryStream ms;
    CryptoStream cs;
    byte[] byt;
    ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
    byt = Convert.FromBase64String(Value);
    ms = new MemoryStream();
    cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
    cs.Write(byt, 0, byt.Length);
    cs.FlushFinalBlock();
    cs.Close();
    return Encoding.UTF8.GetString(ms.ToArray());
    }
    public string GetMD5(string val)
    {
    string md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(val, "MD5");

    md5 = md5.ToUpper();
    return md5;
    }


    /// <summary>
    /// 生成MD5摘要
    /// </summary>
    /// <param name="p_Bytes"></param>
    /// <returns></returns>
    public string CryptoMD5(string str)
    {
    return CryptoMD5(str,"utf-8");
    }
    public string CryptoMD5(string str,string encodingname)
    {
    Encoding enc = GetEncoding(encodingname);
    byte []bi = enc.GetBytes(str);

    MD5 tmpMD5 = new MD5CryptoServiceProvider();
    byte[] tmpResult = tmpMD5.ComputeHash(bi);
    return ConvertToHex(tmpResult);
    }
    public string CryptoMD5(byte[] p_Bytes)
    {
    MD5 tmpMD5 = new MD5CryptoServiceProvider();
    byte[] tmpResult = tmpMD5.ComputeHash(p_Bytes);
    return ConvertToHex(tmpResult);
    }
    private string ConvertToHex(byte[] p_ResultBytes)
    {
    string rtnValue = string.Empty;
    foreach (byte tmpEachByte in p_ResultBytes)
    {
    string tmpEachCode = Convert.ToString(tmpEachByte, 16);
    if (tmpEachCode.Length == 1)
    {
    tmpEachCode = "0" + tmpEachCode;
    }
    rtnValue += tmpEachCode;
    }
    return rtnValue;
    }
    private Encoding GetEncoding(string encodingname)
    {
    Encoding enc = null;
    try
    {
    enc = Encoding.GetEncoding(encodingname);
    }
    catch
    {
    enc = Encoding.UTF8;
    }
    return enc;
    }
    }
    }

    我是代码搬运工!!!
  • 相关阅读:
    java面试题总汇
    数据库sql语句规则
    docker安装redis(网上很多答案都是错误的,小心误入歧途!)
    docker 安装redis后,可视化工具无法连接
    Session
    #{}和${}区别
    axios和ajax区别
    Git使用
    Git常用命令
    如何简单粗暴的搞定dubbo调用模块
  • 原文地址:https://www.cnblogs.com/FanKL/p/13528879.html
Copyright © 2020-2023  润新知