C/C#实现RSA签名

版权声明:未经王小波同学允许不得转载本文内容,否则将视为侵权;博主qq:1419758909;反正也没人转载~ https://blog.csdn.net/qq_38900441/article/details/83448805

先是对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));
        }

签名后,运行结果:

 RSA签名的c语言实现:

#include<stdio.h>
#include<stdlib.h>
 void main()
{ 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)
		{
			printf("签名验证");
		}
		else
		{
			 printf("签名验证错误!");
		}
}

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("签名验证错误!");
            }

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38900441/article/details/83448805
今日推荐