RSA签名验证

1、RSA加密过程简述

      A和B进行加密通信时,B首先要生成一对密钥。一个是公钥,给A,B自己持有私钥。A使用B的公钥加密要加密发送的内容,然后B在通过自己的私钥解密内容

 2、假设A要想B发送消息,A会先计算出消息的消息摘要,然后使用自己的私钥加密这段摘要加密,最后将加密后的消息摘要和消息一起发送给B,被加密的消息摘要就是“签名”。

       B收到消息后,也会使用和A相同的方法提取消息摘要,然后使用A的公钥解密A发送的来签名,并与自己计算出来的消息摘要进行比较。如果相同则说明消息是A发送给B的,同时,A也无法否认自己发送消息给B的事实。

其中,A用自己的私钥给消息摘要加密成为“签名”;B使用A的公钥解密签名文件的过程,就叫做“验签”。

 3、签名过程

1)A提取消息m的消息摘要h(m),并使用自己的私钥对摘要h(m)进行加密,生成签名s

2)A将签名s和消息m一起,使用B的公钥进行加密,生成密文c,发送给B。

具体:

1.A计算消息m的消息摘要,记为 h(m)

2. A使用私钥(n,d)对h(m)加密,生成签名s ,s满足:

s=(h(m))^d mod n;

由于A是用自己的私钥对消息摘要加密,所以只用使用s的公钥才能解密该消息摘要,这样A就不可否认自己发送了该消息给B。

A发送消息和签名(m,s)给B。

4、验证过程

1)B接收到密文c,使用自己的私钥解密c得到明文m和数字签名s

2)B使用A的公钥解密数字签名s解密得到H(m).

3)B使用相同的方法提取消息m的消息摘要h(m)

4)B比较两个消息摘要。相同则验证成功;不同则验证失败。

具体:

1.B计算消息m的消息摘要,记为h(m);

2.B使用A的公钥(n,e)解密s,得到

H(m) = s^e mod n;

3.B比较H(m)与h(m),相同则证明

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;


namespace SecTest
{
    public partial class RSASV : Form
    {
        public string str1;
        public string MFileNamestr1;
        public static string ToHexString(byte[] bytes)       // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();
            }
            return hexString;
        }

        public RSASV()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var cspPas = new CspParameters();
            cspPas.KeyContainerName = "rsa_key";
            RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider(cspPas);
            RSA1.PersistKeyInCsp = false;
            RSA1.Clear();
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspPas);
            string str_Public_Key;
            string str_Private_Key;
            str_Public_Key = "";
            str_Private_Key = "";
            str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
            str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
            textBox1.Text = str_Public_Key;
            textBox2.Text = str_Private_Key;
            FileStream fs1 = new FileStream("C:\\RsaKey1.dat", FileMode.Create, FileAccess.Write);
            string key = RSA.ToXmlString(false);
            fs1.Write(Encoding.UTF8.GetBytes(key), 0, key.Length);
            fs1.Close();
            fs1.Dispose();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var cspPas = new CspParameters();
            cspPas.KeyContainerName = "rsa_key";
            RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider(cspPas);
            string str_Public_Key;
            string str_Private_Key;
            str_Public_Key = "";
            str_Private_Key = "";
            str_Public_Key = Convert.ToBase64String(RSA1.ExportCspBlob(false));
            str_Private_Key = Convert.ToBase64String(RSA1.ExportCspBlob(true));
            textBox1.Text = str_Public_Key;
            textBox2.Text = str_Private_Key;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            var cspPas = new CspParameters();
            cspPas.KeyContainerName = "rsa_key";
            RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider(cspPas);
            SHA1 sh = new SHA1CryptoServiceProvider();
            if (textBox3.Text == "") 
                return;

            FileStream fin = new FileStream(textBox3.Text, FileMode.Open, FileAccess.Read);
            byte[] Data = new byte[fin.Length];
            try
            {
                fin.Read(Data, 0, Data.Length);
                fin.Seek(0, SeekOrigin.Begin);
            }
            catch
            {

            }
            finally
            {
                if (fin != null)
                    fin.Close();
            }

            byte[] signData = RSA1.SignData(Data, sh);
            textBox4.Text =Convert.ToBase64String(signData);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = openFileDialog.FileName;
                MFileNamestr1 = textBox3.Text;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            bool result;
            FileStream fin = new FileStream(textBox3.Text, FileMode.Open, FileAccess.Read);
            byte[] Data = new byte[fin.Length];
            try
            {
                fin.Read(Data, 0, Data.Length);
                fin.Seek(0, SeekOrigin.Begin);
            }
            catch
            {
            }
            finally
            {
                if (fin != null)
                    fin.Close();
            }
            FileStream fkeyin = new FileStream("C:\\RsaKey1.dat", FileMode.Open, FileAccess.Read);
            long keyL=fkeyin.Length;
            byte[] key_buf = new byte[keyL];
            fkeyin.Read(key_buf, 0, (int)keyL);
            string strPublicKey;
            strPublicKey = System.Text.Encoding.Default.GetString(key_buf); 
            RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider();
            RSA1.FromXmlString(strPublicKey);

            byte[] data = Convert.FromBase64String(textBox4.Text);
            SHA1 sh = new SHA1CryptoServiceProvider();
            result = RSA1.VerifyData(Data, sh, data);
            if (result)
                textBox5.Text = "验证通过。";
            else
                textBox5.Text = "验证失败。";

        }
    }
}

你可能感兴趣的:(其他)