zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }

  • 相关阅读:
    Python笔记 【无序】 【五】
    Python笔记 【无序】 【四】
    Python 面向对象【2】
    Python 面向对象【1】
    OpenCV 入门
    Python笔记 【无序】 【三】
    js制作秒表
    C语言No such file or directory错误
    js注册实现
    js中setTimeout和setInterval的应用方法(转)
  • 原文地址:https://www.cnblogs.com/changeMe/p/4421425.html
Copyright © 2011-2022 走看看