田草博客
日志搜索


 标题   内容 评论


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件
-电信用户-|-网通用户-
-博客论坛-|-软件下载-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2020 - 6 48
 123456
78910111213
14151617181920
21222324252627
282930


站点统计

最新评论



win7 搜索功能不能使用的解决办法 word的锁定与解锁
未知 MD5 加密、解密、数字签名及验证   [ 日期:2017-05-08 ]   [ 来自:转帖 ]  HTML
转帖http://www.cnblogs.com/yn-jiang/p/3844151.html

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


namespace ConsoleApplication2
{
 
    class Class1
    {

        //创建了密钥对
        static RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        private string privateKey = RSA.ToXmlString(true);
        private string publicKey = RSA.ToXmlString(false);
        static byte[] AOutput;

        public static string GetKeyFromContainer(string ContainerName, bool privatekey)
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = ContainerName;
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
            return rsa.ToXmlString(privatekey);
        }

        //加密
        public static string RSAEncrypt(string publicKey, string content)
        {
            RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider();

            Rsa.FromXmlString(publicKey);
            byte[] cipherbytes = Rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
            AOutput = Rsa.Encrypt(cipherbytes, false);
            return Convert.ToBase64String(AOutput);
        }

        //解密
        public static string RSADecrypt(string privateKey, string content)
        {
            RSACryptoServiceProvider Rsa2 = new RSACryptoServiceProvider();
            Rsa2.FromXmlString(privateKey);
            byte[] cipherbytes = Rsa2.Decrypt(Convert.FromBase64String(content), false);
            return Encoding.UTF8.GetString(cipherbytes);
        }

        //获取hash值
        private byte[] GetHash(string content)
        {
            try
            {
                //FileStream objFile = File.OpenRead(filePath);
                byte[] data_Bytes = Encoding.ASCII.GetBytes(content);
                HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
                byte[] Hashbyte = MD5.ComputeHash(data_Bytes);
                //objFile.Close();
                return Hashbyte;
            }
            catch
            {
                return null;
            }
            throw new NotImplementedException();
        }

        //获取文件hash
        public static byte[] GetFileHash(string filePath)
        {
            try
            {
                FileStream objFile = File.OpenRead(filePath);
                HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
                byte[] Hashbyte = MD5.ComputeHash(objFile);
                objFile.Close();
                return Hashbyte;
            }
            catch
            {
                return null;
            }
        }

        //创建数字签名
        byte[] EncryptHash(string privateKey, byte[] hash_Bytes)
        {
            RSACryptoServiceProvider Rsa3 = new RSACryptoServiceProvider();
            Rsa3.FromXmlString(privateKey);

            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(Rsa3);
            RSAFormatter.SetHashAlgorithm("MD5");
            
            

            //AOutput = Rsa3.SignData(data_Bytes, "SHA1");

            return RSAFormatter.CreateSignature(hash_Bytes); ;
        }

        //验证数字签名
        public bool DecryptHash(string publicKey, byte[] hash_byte, byte[] eSignature)
        {
            try
            {
                RSACryptoServiceProvider Rsa4 = new RSACryptoServiceProvider();
                Rsa4.FromXmlString(publicKey);
            
                //bool bVerify = Rsa4.VerifyData(strData, "SHA1", AOutput);
                RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(Rsa4);
                RSADeformatter.SetHashAlgorithm("MD5");

                bool bVerify = RSADeformatter.VerifySignature(hash_byte,eSignature);

                if (bVerify)
                {
                    return true;
                }
                return false;
            }
            catch (CryptographicException e)
            {
                return false;
            }
        }

        // 将Byte[]转换成十六进制字符串
        public static string ConvertBytesToString(byte[] bytes)
        {
            string bytestring = string.Empty;
            if (bytes != null && bytes.Length > 0)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytestring += bytes[i].ToString("X") + " ";
                }
            }
            return bytestring;
        }

        static void Main(string[] args)
        {
            //打开文件,或者是获取网页数据
            Console.WriteLine("接收方收到电子文件。");
            string strMsg = "dadfdfgdgdgagdgadg";  //test data
            Console.WriteLine(strMsg);
            Class1 cls = new Class1();


            //对电子文件进行哈希
            Console.WriteLine("哈希值:");
     &nb