RSA中的RSA密钥生成 [英] RSA Key generation in C++

查看:136
本文介绍了RSA中的RSA密钥生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下用于为RSA生成密钥的C ++代码:



I wrote the following C++ code for generating keys for RSA :

void keygen(long long int &e_key,long long int &d_key,long long int phival)
    {
        //Code to find the encryption key.

        e_key = genrndnum(2,(phival));

        while(gcd(e_key,phival)!=1)
        {
            e_key = genrndnum(2,(phival-1));
        }

        //Code to find decryption key.

    long long int k =0;
        d_key = (1+(k*phival))/e_key;

        while(((1+(k*phival))%e_key)!=0)
        {
            ++k;
            d_key = (1+(k*phival))/e_key;
        }

        return;
    }





[更新]



genrndnum的代码()。





[Update]

Code for genrndnum().

long genrndnum(long lower_limit,long upper_limit)
{
    srand(time(NULL));
    long rdnum =0;

    rdnum = lower_limit + (rand() % (int)(upper_limit - lower_limit + 1));

    return rdnum;
}





[结束更新]



[更新2]



Keygen是一个生成私钥和公钥的函数。

这里e_key是公钥,d_key是私钥。两者都通过引用传递。

然后phival是Euler totient函数(即,phi(x))。 (这是在前一段时间计算出来的,未显示)。



我从维基百科获得了所有这些信息。





[结束更新2]



[更新3] < br $> b $ b

寻找gcd()的代码。





[End Update]

[Update 2]

Keygen is a function to generate private and public keys.
Here e_key is Public key and d_key the private key. Both are passed by reference.
Then phival is the Euler totient function (ie., phi(x)). (This was calculated sometime earlier , not shown).

I got all these information from wikipedia.


[End Update 2]

[Update 3]

Code for finding gcd().

long long int gcd(long long int num1 , long long int num2)
{
		/*
	
		Returns the Greatest Common Divisor (GCD) of two given numbers.
		
	*/
	
	
	if(num1 ==0 && num2==0)
	{
		return 0;
	}
	
	if(num1==0)
	{
		return num2;
	}
	
	if(num2 ==0)
	{
		return num1;
	}
	long long int remainder = 0;
	
	do
	{	
	    remainder = num1%num2;
		num1 = num2;	
		num2 = remainder;
				
	}while(remainder!=0);
	
	return num1;
}





keygen()的输入值。 < br $>


keygen()的唯一输入值是:



Input Values for keygen().

The only input value for keygen() is :

phival = 43392.







[结束更新3]



[更新4]



输出值:[随机生成]








[End Update 3]

[Update 4]

Output values : [Randomly generated]


p =337
 q = 149
 phi = 49728
Encryption key = 8893
k = 7
Private key = 39 (There is another problem here : Encryption and Private keys should be the same. No where in the code have i changed e_key during decryption.)

Public key = 39
Modulus = 50213









[结束更新4]





但我得到公钥和私钥的相同值。代码中的错误是什么?





[End Update 4]


But i get the same value for Public key and private key. What is the mistake in the code ?

推荐答案

引用:

long genrndnum( long lower_limit,long upper_limit)

{

srand(time(NULL)); //< ==================错误在这里

long rdnum = 0;



rdnum = lower_limit +(rand()%(int)(upper_limit - lower_limit + 1));



返回rdnum;

}

long genrndnum(long lower_limit,long upper_limit)
{
srand(time(NULL)); //<================== mistake here
long rdnum =0;

rdnum = lower_limit + (rand() % (int)(upper_limit - lower_limit + 1));

return rdnum;
}





每次需要新的随机数时,不应该调用 srand 。您应该在第一次调用 rand 之前调用一次。



You should not call srand every time you need a new random number. You should call it once, before calling rand for the very first time.


这篇关于RSA中的RSA密钥生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆