C/C#实现RSA签名

先是对1的数字签名

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

static void Main(string[] args)
        {
            HashAlgorithm hash = HashAlgorithm.Create();
            byte[] hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes("1"));
            //The   value   to   hold   the   signed   value.   
            byte[] SignedHashValue;
            //Generate   a   public/private   key   pair.   
            RSACryptoServiceProvider RSA = new  RSACryptoServiceProvider();

            //Create   an   RSAPKCS1SignatureFormatter   object   and   pass   it   the     
            //RSACryptoServiceProvider   to   transfer   the   private   key.   
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            //Set   the   hash   algorithm   to   SHA1.   
            RSAFormatter.SetHashAlgorithm("SHA1");
            //Create   a   signature   for   HashValue   and   assign   it   to     
            //SignedHashValue.   
            SignedHashValue = RSAFormatter.CreateSignature(hashBytes);
            Console.WriteLine(Convert.ToBase64String(SignedHashValue));
        }

签名后,运行结果:

C/C#实现RSA签名_第1张图片

 RSA签名的c语言实现:

#include
#include
 void main()
{ int m=707;   
	int d=425;   
	int n=3431;   
	int c=1;
		for (int i=0; i

RSA签名的C#语言实现 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int m = 707;
            int d = 425;
            int n = 3431;
            int c = 1;
            for (int i = 0; i < d; i++)
            {
                c = c * m;
                c = c % n;
            }
            int e = 1769;
            m = 1;
            for (int i = 0; i < e; i++)
            {
                m = m * c;
                m = m % n;
            }
            if (m == 707)
            {
                Console.WriteLine("签名验证");
            }
            else
            {
                Console.WriteLine("签名验证错误!");
            }

        }
    }
}

 

你可能感兴趣的:(#,C#)