(C#基础)各种加密学习

    之前,一直想对这个做一个了解,但是总是坚持不下去,很急躁。最近看了几遍文章,很有感触,于是又来重新开始学习,从最最基础的开始——正所谓“慢就是快”。心态变了,继续吧!上代码!

 

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

namespace dazilianxi.wenjian
{
    public class RSACryptoHelper
    {
        //加密 
        public static string Encrypt(string publicKeyXml, string plainText)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(publicKeyXml); //使用公匙初始化对象 
            byte[] plainData = Encoding.Default.GetBytes(plainText);
            byte[] encryptedData = provider.Encrypt(plainData, true);
            return Convert.ToBase64String(encryptedData);
        }

        //解密 
        public static string Decrypt(string privateKeyXml, string encryptedText)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(privateKeyXml);
            byte[] encryptedData = Convert.FromBase64String(encryptedText);
            byte[] plainData = provider.Decrypt(encryptedData, true);
            string plainText = Encoding.Default.GetString(plainData);
            return plainText;
        }
        public static KeyValuePair<string, string> CreateRSAKey()
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            string privateKey = RSA.ToXmlString(true);
            string publicKey = RSA.ToXmlString(false);

            return new KeyValuePair<string, string>(publicKey, privateKey);
        }

        public static string SignData(string plainText, string privateKeyXml)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(privateKeyXml);

            byte[] plainData = Encoding.Default.GetBytes(plainText);
            //设置获取摘要的算法 
            HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
            //获取签名过的摘要,是使用私匙加密过的摘要 
            byte[] signedDigest = provider.SignData(plainData, sha1);
            return Convert.ToBase64String(signedDigest);
        }

        public static bool VerifyData(string plainText, string signature, string publicKeyXml)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(publicKeyXml);

            byte[] plainData = Encoding.Default.GetBytes(plainText);
            byte[] signedDigest = Convert.FromBase64String(signature);

            HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
            bool isDataIntact = provider.VerifyData(plainData, sha1, signedDigest);
            return isDataIntact;
        }

        //使用SingnHash 
        public static string SignData2(string plainText, string privateKeyXml)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(privateKeyXml);
            byte[] plainData = Encoding.Default.GetBytes(plainText);

            //设置获取摘要的算法 
            HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
            //获得原始摘要 
            byte[] digestData = sha1.ComputeHash(plainData);
            //对元素摘要进行签名 
            byte[] signedDigest = provider.SignHash(digestData, "SHA1");
            return Convert.ToBase64String(signedDigest);
        }


        //使用VerifyHash 
        public static bool VerifyData2(string plainText, string signedDigest, string publicKeyXml)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(publicKeyXml);

            byte[] plainData = Encoding.Default.GetBytes(plainText);
            byte[] signedDigestData = Convert.FromBase64String(signedDigest);

            //获得本地摘要 
            HashAlgorithm sha1 = HashAlgorithm.Create("SHA1");
            byte[] digest = sha1.ComputeHash(plainData);

            //解密签名 
            bool isDataIntact = provider.VerifyHash(digest, "SHA1", signedDigestData);
            return isDataIntact;
        }
    }
}

 

  RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            string publicPrivate = provider.ToXmlString(true);//获得公/私匙对 
           // string publicOnly = provider.ToXmlString(false); //只获得公匙 
           // string key = "secret key";
            string str = "hello world";
           // string str2 = "hello world";
            //加密
            string encryptedText = RSACryptoHelper.Encrypt(publicPrivate, str);
            Console.WriteLine(encryptedText);
            //学到了重构,this 在构造函数中的灵活运用。不能直接满足条件,就要想方设法创造合适的。
            //解密
            string clearText = RSACryptoHelper.Decrypt(publicPrivate, encryptedText);
            Console.WriteLine(clearText);


            string originalData = "文章不错,这是我的签名:奥巴马!";
            Console.WriteLine("签名数为:{0}", originalData);

            KeyValuePair<string, string> keyPair =RSACryptoHelper.CreateRSAKey();
            string privateKey = keyPair.Value;
            string publicKey = keyPair.Key;
            //1、生成签名,通过摘要算法
            string signedData = RSACryptoHelper.SignData(originalData, privateKey);
            Console.WriteLine("数字签名1:{0}", signedData);

            //2、验证签名
            bool verify = RSACryptoHelper.VerifyData(originalData, signedData, publicKey);
            Console.WriteLine("签名验证结果:{0}", verify);

            string signedData2 = RSACryptoHelper.SignData2(originalData, privateKey);
            Console.WriteLine("数字签名2:{0}", signedData2);

            //2、验证签名
            bool verify2 = RSACryptoHelper.VerifyData2(originalData, signedData2, publicKey);
            Console.WriteLine("签名验证结果2:{0}", verify2);

            

参考,延伸学习:

数据加密:http://www.cnblogs.com/yank/p/3528548.html

数字签名:http://www.cnblogs.com/yank/p/3533998.html

证书:http://www.cnblogs.com/Microshaoft/archive/2009/05/19/1460641.html

加密:http://www.cnblogs.com/Microshaoft/archive/2008/07/21/1247584.html

加密:http://www.cnblogs.com/darrenji/p/3677458.html

 

posted @ 2015-11-17 11:54  山水中华  阅读(607)  评论(0编辑  收藏  举报