惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
K
Kaspersky official blog
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
T
Threat Research - Cisco Blogs
L
LINUX DO - 热门话题
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
月光博客
月光博客
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Recorded Future
Recorded Future
Latest news
Latest news
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
IT之家
IT之家
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
AWS News Blog
AWS News Blog
H
Help Net Security
S
Security @ Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
S
Schneier on Security
S
Security Affairs
T
Tenable Blog

博客园 - melody&bobo

端口数据库连接 ANSI, UNICODE,UTF8编码的区别 调用系统API打印图片文字 windows2003安全设置 图片与字节数组相互转换的方法 jQuery Ajax 方法调用 Asp.Net WebService 的详细例子 使用instantclient_11_2和PL/SQL Developer工具包连接oracle 11g远程数据库 查看SQL 语句执行性能 SSIS 获取时间表达式 获得时间段之间每月的最后一天 ADSL拨号获得不同IP地址 C#(.Net) 解决Informix中文乱码问题 SQL SPLIT() 方法实现 JS获取URL参数值 datatable操作集合 表单提交文件 - melody&bobo - 博客园 nginx实现网站负载均衡测试实例(windows下IIS做负载实测) Web Application Stress Tool(WAS,Web应用负载测试工具)详细说明 Base64 加密字符串和文件
C#加密解密
melody&bobo · 2015-11-17 · via 博客园 - melody&bobo
public sealed class EncryptUtils

    {

        #region Base64加密解密

        /// <summary>

        /// Base64加密

        /// </summary>

        /// <param name="input">需要加密的字符串</param>

        /// <returns></returns>

        public static string Base64Encrypt(string input)

        {

            return Base64Encrypt(input, new UTF8Encoding());

        }

 

        /// <summary>

        /// Base64加密

        /// </summary>

        /// <param name="input">需要加密的字符串</param>

        /// <param name="encode">字符编码</param>

        /// <returns></returns>

        public static string Base64Encrypt(string input, Encoding encode)

        {

            return Convert.ToBase64String(encode.GetBytes(input));

        }

 

        /// <summary>

        /// Base64解密

        /// </summary>

        /// <param name="input">需要解密的字符串</param>

        /// <returns></returns>

        public static string Base64Decrypt(string input)

        {

            return Base64Decrypt(input, new UTF8Encoding());

        }

 

        /// <summary>

        /// Base64解密

        /// </summary>

        /// <param name="input">需要解密的字符串</param>

        /// <param name="encode">字符的编码</param>

        /// <returns></returns>

        public static string Base64Decrypt(string input, Encoding encode)

        {

            return encode.GetString(Convert.FromBase64String(input));

        }

        #endregion

 

        #region DES加密解密

        /// <summary>

        /// DES加密

        /// </summary>

        /// <param name="data">加密数据</param>

        /// <param name="key">8位字符的密钥字符串</param>

        /// <param name="iv">8位字符的初始化向量字符串</param>

        /// <returns></returns>

        public static string DESEncrypt(string data, string key, string iv)

        {

            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

 

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

            int i = cryptoProvider.KeySize;

            MemoryStream ms = new MemoryStream();

            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

 

            StreamWriter sw = new StreamWriter(cst);

            sw.Write(data);

            sw.Flush();

            cst.FlushFinalBlock();

            sw.Flush();

            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

        }

 

        /// <summary>

        /// DES解密

        /// </summary>

        /// <param name="data">解密数据</param>

        /// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>

        /// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>

        /// <returns></returns>

        public static string DESDecrypt(string data, string key, string iv)

        {

            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

 

            byte[] byEnc;

            try

            {

                byEnc = Convert.FromBase64String(data);

            }

            catch

            {

                return null;

            }

 

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

            MemoryStream ms = new MemoryStream(byEnc);

            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);

            StreamReader sr = new StreamReader(cst);

            return sr.ReadToEnd();

        }

        #endregion

 

        #region MD5加密

        /// <summary>

        /// MD5加密

        /// </summary>

        /// <param name="input">需要加密的字符串</param>

        /// <returns></returns>

        public static string MD5Encrypt(string input)

        {

            return MD5Encrypt(input, new UTF8Encoding());

        }

 

        /// <summary>

        /// MD5加密

        /// </summary>

        /// <param name="input">需要加密的字符串</param>

        /// <param name="encode">字符的编码</param>

        /// <returns></returns>

        public static string MD5Encrypt(string input, Encoding encode)

        {

            MD5 md5 = new MD5CryptoServiceProvider();

            byte[] t = md5.ComputeHash(encode.GetBytes(input));

            StringBuilder sb = new StringBuilder(32);

            for (int i = 0; i < t.Length; i++)

                sb.Append(t[i].ToString("x").PadLeft(2, '0'));

            return sb.ToString();

        }

 

        /// <summary>

        /// MD5对文件流加密

        /// </summary>

        /// <param name="sr"></param>

        /// <returns></returns>

        public static string MD5Encrypt(Stream stream)

        {

            MD5 md5serv = MD5CryptoServiceProvider.Create();

            byte[] buffer = md5serv.ComputeHash(stream);

            StringBuilder sb = new StringBuilder();

            foreach (byte var in buffer)

                sb.Append(var.ToString("x2"));

            return sb.ToString();

        }

 

        /// <summary>

        /// MD5加密(返回16位加密串)

        /// </summary>

        /// <param name="input"></param>

        /// <param name="encode"></param>

        /// <returns></returns>

        public static string MD5Encrypt16(string input, Encoding encode)

        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            string result = BitConverter.ToString(md5.ComputeHash(encode.GetBytes(input)), 4, 8);

            result = result.Replace("-", "");

            return result;

        }

        #endregion

 

        #region 3DES 加密解密

 

        public static string DES3Encrypt(string data, string key)

        {

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

 

            DES.Key = ASCIIEncoding.ASCII.GetBytes(key);

            DES.Mode = CipherMode.CBC;

            DES.Padding = PaddingMode.PKCS7;

 

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

 

            byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data);

            return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

        }

 

        public static string DES3Decrypt(string data, string key)

        {

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

 

            DES.Key = ASCIIEncoding.ASCII.GetBytes(key);

            DES.Mode = CipherMode.CBC;

            DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

 

            ICryptoTransform DESDecrypt = DES.CreateDecryptor();

 

            string result = "";

            try

            {

                byte[] Buffer = Convert.FromBase64String(data);

                result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

            }

            catch (Exception e)

            {

 

            }

            return result;

        }

 

        #endregion

    }