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

    我是代码搬运工!!!
  • 相关阅读:
    Python——文件操作2
    Python——文件操作1
    Python——集合
    Springboot @Transactional捕获异常事务回滚
    elasticsearch5.2 高亮java代码
    Java static作用
    elasticsearch6.3 自动补齐suggest java代码
    spring boot通过官网创建新项目
    eclipse web工程不能 修改build path
    linux虚拟机添加网卡
  • 原文地址:https://www.cnblogs.com/FanKL/p/13528879.html
Copyright © 2020-2023  润新知