C# 加密解密字符串

有时候可能会用到,给数据库连接的字符串加密,让人不那么容易看出服务器的地址和帐号信息,所以大家可以用这个工具,把字符串加密,然后执行的时候解码,就可以了。方便实用。

方法一:

using System;
using System.Collections.Generic;
using System.Text;

namespace StringEncry
{
    class EncodeAndDecode
    {
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="codeName">加密采用的编码方式</param>
        /// <param name="source">待加密的明文</param>
        /// <returns></returns>
        public static string EncodeBase64(Encoding encode, string source)
        {
            string enstring = "";
            byte[] bytes = encode.GetBytes(source);
            try
            {
                enstring = Convert.ToBase64String(bytes);
            }
            catch
            {
                enstring = source;
            }
            return enstring;
        }
 
        /// <summary>
        /// Base64加密,采用utf8编码方式加密
        /// </summary>
        /// <param name="source">待加密的明文</param>
        /// <returns>加密后的字符串</returns>
        public static string EncodeBase64(string source)
        {
            return EncodeBase64(Encoding.UTF8, source);
        }
 
        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string DecodeBase64(Encoding encode, string result)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = encode.GetString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }
 
        /// <summary>
        /// Base64解密,采用utf8编码方式解密
        /// </summary>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string DecodeBase64(string result)
        {
            return DecodeBase64(Encoding.UTF8, result);
        }
    }
}

这个类,大家可以直接使用。

效果图如下:


方法二:

/// <summary>
    /// MD5加密,输入明文,返回密文
    /// </summary>
    /// <param name="pToEncrypt"></param>
    /// <returns>加密后字符串</returns>
    [WebMethod(Description = "【函数描述】加密数据库连接字符串")]
    public string MD5Encrypt(string pToEncrypt)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
        //des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
        //des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey);  

        byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量   
        byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 
        des.Key = key;
        des.IV = iv;
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        StringBuilder ret = new StringBuilder();
        foreach (byte b in ms.ToArray())
        {
            ret.AppendFormat("{0:X2}", b);
        }
        ret.ToString();
        return ret.ToString();
    }


    /// <summary>
    /// 解密连接字符串,输入加密后字符串,返回解密密后的字符串
    /// </summary>
    /// <param name="pToDecrypt">密文</param>
    /// <returns>明文</returns>
    [WebMethod(Description = "【函数描述】解密数据库连接字符串")]
    public string MD5Decrypt(string pToDecrypt)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
        for (int x = 0; x < pToDecrypt.Length / 2; x++)
        {
            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
            inputByteArray[x] = (byte)i;
        }

        //des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey);  
        //des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey);  

        byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量   
        byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 
        des.Key = key;
        des.IV = iv;

        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        StringBuilder ret = new StringBuilder();

        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值