MD5 不可逆加密,Des对称可逆加密 ,RSA非对称可逆加密 ,数字证书 SSL

Posted Anker_张

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MD5 不可逆加密,Des对称可逆加密 ,RSA非对称可逆加密 ,数字证书 SSL相关的知识,希望对你有一定的参考价值。

:MD5 不可逆加密
2:Des对称可逆加密
3:RSA非对称可逆加密
4:数字证书 SSL

                  Anker_张(博客园)http://www.cnblogs.com/AnkerZhang/

1:MD5 不可逆加密

 

using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace EncryptDemo
{
    /// <summary>
    /// 不可逆加密
    /// 1 防止被篡改
    /// 2 防止明文存储
    /// 3 防止抵赖,数字签名
    /// </summary>
    public class MD5Encrypt
    {
        #region MD5
        /// <summary>
        /// MD5加密,和动网上的16/32位MD5加密结果相同,
        /// 使用的UTF8编码
        /// </summary>
        /// <param name="strSource">待加密的字符串</param>
        /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
        /// <returns>加密后的字串</returns>
        public static string Encrypt(string source, int length = 32)//默认参数
        {
            HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
            if (string.IsNullOrEmpty(source)) return string.Empty;

            byte[] bytes = Encoding.UTF8.GetBytes(source);// Encoding.ASCII.GetBytes(source);
            byte[] hashValue = provider.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            switch (length)
            {
                case 16://16位密文是32位密文的9到24位字符
                    for (int i = 4; i < 12; i++)
                        sb.Append(hashValue[i].ToString("x2"));
                    break;
                case 32:
                    for (int i = 0; i < 16; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
                default:
                    for (int i = 0; i < hashValue.Length; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
            }
            return sb.ToString();
        }
        /// <summary>
        /// MD5加密,和动网上的16/32位MD5加密结果相同,
        /// 使用的UTF8编码
        /// 为文件加密方法
        /// </summary>
        /// <param name="strSource">待加密的文件路径</param>
        /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
        /// <returns>加密后的字串</returns>
        public static string EncryptFile(string pathFile, int length = 32)//默认参数
        {
            using (FileStream fsRead = new FileStream(pathFile, FileMode.Open))
            {
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                string myStr = System.Text.Encoding.UTF8.GetString(heByte);
                return Encrypt(myStr, length);
            }
        }
        #endregion MD5
    }
}

2:Des对称可逆加密

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace EncryptDemo
{
    /// <summary>
    /// DES AES Blowfish
    ///  对称加密算法的优点是速度快,
    ///  缺点是密钥管理不方便,要求共享密钥。
    /// 可逆对称加密  密钥长度8
    /// </summary>
    public class DesEncrypt
    {
        //8位长度
        private static string KEY = "Anker_张1";
        private static byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(KEY.Substring(0, 8));
        private static byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes(KEY.Insert(0, "Z").Substring(0, 8));
        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string Encrypt(string strValue)
        {
            DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
            using (MemoryStream memStream = new MemoryStream())
            {
                CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                StreamWriter sWriter = new StreamWriter(crypStream);
                sWriter.Write(strValue);
                sWriter.Flush();
                crypStream.FlushFinalBlock();
                memStream.Flush();
                return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
            }
        }
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="EncValue"></param>
        /// <returns></returns>
        public static string Decrypt(string EncValue)
        {
            DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
            byte[] buffer = Convert.FromBase64String(EncValue);

            using (MemoryStream memStream = new MemoryStream())
            {
                CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                crypStream.Write(buffer, 0, buffer.Length);
                crypStream.FlushFinalBlock();
                return ASCIIEncoding.UTF8.GetString(memStream.ToArray());
            }
        }
    }
}

3:RSA非对称可逆加密

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace EncryptDemo
{
    /// <summary>
    /// RSA ECC
    /// 可逆非对称加密 
    /// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。
    /// </summary>
    public class RsaEncrypt
    {
        /// <summary>
        /// publicKey:加密,privateKey解密
        /// </summary>
        /// <returns></returns>
        public static KeyValuePair<string, string> GetKeyPair()
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            string publicKey = RSA.ToXmlString(false);
            string privateKey = RSA.ToXmlString(true);
            return new KeyValuePair<string, string>(publicKey, privateKey);
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="content"></param>
        /// <param name="publicKey">返还公钥</param>
        /// <param name="privateKey">返回密钥</param>
        /// <returns>加密后结果</returns>
        public static void GetKey(out string publicKey, out string privateKey)
        {
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
            publicKey = rsaProvider.ToXmlString(false);
            privateKey = rsaProvider.ToXmlString(true);
        }
        /// <summary>
        /// 加密:内容+公钥
        /// </summary>
        /// <param name="content"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static string Encrypt(string content, string publicKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publicKey);
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] DataToEncrypt = ByteConverter.GetBytes(content);
            byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false);
            return Convert.ToBase64String(resultBytes);
        }
        /// <summary>
        /// 解密  内容+私钥
        /// </summary>
        /// <param name="content"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string Decrypt(string content, string privateKey)
        {
            byte[] dataToDecrypt = Convert.FromBase64String(content);
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(privateKey);
            byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false);
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            return ByteConverter.GetString(resultBytes);
        }
    }
}

RSA非对称可逆加密原理

 

 

 

 4:数字证书 SSL

 

 

 

 

.Net中的加密解密:http://www.cnblogs.com/JimmyZhang/archive/2008/10/02/Cryptograph.html

MD5算法原理:http://blog.csdn.net/forgotaboutgirl/article/details/7258109

 详情Des加密: http://www.iplaysoft.com/encrypt-arithmetic.html

 详情RSA加密:http://www.iplaysoft.com/encrypt-arithmetic.html

SSL 与 数字证书 的基本概念和工作原理:http://blog.csdn.net/jhonguy/article/details/7577729

以上是关于MD5 不可逆加密,Des对称可逆加密 ,RSA非对称可逆加密 ,数字证书 SSL的主要内容,如果未能解决你的问题,请参考以下文章

移动端加解密

基于C#的RSA非对称加密算法

密码加密

数据加密介绍

常用的几种加密方式,“MD5”,"DES","RSA"

RSA、SHA、DES、MD5、BASE64的可逆性分析