• C#Base64加密


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    namespace Business
    {
    /// <summary>
    /// 加密类
    /// </summary>
    public class EncryptDes
    {
    /// <summary>
    /// 解密连接字符串
    /// </summary>
    /// <param name="strInput">连接字符串</param>
    /// <returns>解密后的连接字符串</returns>
    public string Decrypt(string strInput)
    {
    bool bolSuccess = false;

    strInput = strInput.Trim();

    string strResult;
    string strKey = "31415926";
    string strIV = "31415926";

    byte[] bytKey = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strKey);
    byte[] bytIV = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strIV);

    if ((strInput.Length > 0))
    {
    try
    {
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    byte[] bytData = Convert.FromBase64String(strInput);
    MemoryStream ms = new MemoryStream(bytData);
    CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(cs);
    strResult = sr.ReadToEnd();
    bolSuccess = true;

    }
    catch (Exception ex)
    {
    strResult = strInput;
    }
    }
    else
    {
    strResult = "";
    }
    return strResult;
    }

    /// <summary>
    /// 加密连接字符串
    /// </summary>
    /// <param name="strInput">连接字符串</param>
    /// <returns>加密后的连接字符串</returns>
    public string Encrypt(string strInput)
    {
    bool bolSuccess = false;

    strInput = strInput.Trim();

    string strResult;

    string strKey = "31415926";
    string strIV = "31415926";

    byte[] bytKey = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strKey);
    byte[] bytIV = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strIV);
    if ((strInput.Length > 0))
    {
    try
    {
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write);

    StreamWriter sw = new StreamWriter(cs);

    sw.Write(strInput);
    sw.Flush();
    cs.FlushFinalBlock();
    ms.Flush();
    strResult = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
    bolSuccess = true;
    }
    catch (Exception ex)
    {
    strResult = strInput;
    }
    }
    else
    {
    strResult = "";
    }
    return strResult;
    }
    }
    }

  • 相关阅读:
    Android入门:Button
    Android入门:部署时的常见错误
    to be
    Android入门:单元测试
    忘记 MySQL 的 root 帐号密码该怎么办
    Eclipse开发build path中jar包部署到应用中
    报告两个bug
    本站导引
    一个用Word做报表设计的报表系统windwardreports
    智能互联网
  • 原文地址:https://www.cnblogs.com/changeMe/p/4421425.html
Copyright © 2020-2023  润新知