⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsakg-45.cpp

📁 RSA key generation, enjoy and remember me in your prayers
💻 CPP
字号:

#include <iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
//tahamufti@hotmail.com
void main(void)
{
clrscr();
textbackground(1);
//textcolor(BLACK);
long int n,p,q;
long int phyn,p1,q1;
printf("\n\n//////////Taha Mufti///////////////// RSA key generation ///////////////////////");
long int count=0;
char choice='y';

	while(choice =='y' || choice == 'Y')	//for 'p'
	 {
		printf("\n\n\tEnter a value for 'P':");
		scanf("%ld",&p);
		count=0;
		for(int i=2; i<= pow(p,.5); i++)
		{
			if(p%i == 0)
			{
			count++;
			break;
				 }
				}

			if(count == 0)
			{
		  printf("\n\nValue of P= %ld is a prime number!",p);
		 p1=p;  break;         }

	else

	printf("\n\nValue of 'P'= %ld is not a prime number!",p);

	 printf("\n\n\tRetry for 'P'  ? <y/n>: ");

	scanf("%s",&choice);     }



	while(choice =='y' || choice == 'Y')         //for 'q'
	 {
		printf("\n\n\tEnter a value for Q:");
		scanf("%ld",&q);
		count=0;
		for(int i=2; i<= pow(q,.5); i++)
		{
			if(q%i == 0)			{
			count++;
			break;
				}
				}

			if(count == 0)
			{
			  printf("\n\nValue of 'Q'= %ld is a prime number!",q);
			 q1=q;  break;         }

		else

		printf("\n\nValue of 'Q'= %ld is not a prime number!",q);

		 printf("\n\nRetry for 'q'  ? <y/n>: ");

		scanf("%s",&choice);
					}

n=p*q;

phyn=((p-1)*(q-1));

printf("\n\n\n\tphyn=%ld\n\n\tN= %ld\n\n\tP= %ld\n\n\tQ= %ld\n\n\t",phyn,n,p,q);

long  int e,e1;
 e= rand() % 100;



	while(choice =='y' || choice == 'Y')         //for 'q'
	 {
	       //	printf("\n\n\tEnter a value for e:");
	       //	scanf("%d",&t);

			e= rand() % 100;

			printf("E=%ld",e);
		count=0;
		for(int i=2; i<= pow(e,.5); i++)
		{
			if(e%i == 0)
			{
			count++;
			break;
				}
				}

			if(count == 0)
			{
			  printf("\n\nValue of 'E'= %ld is a prime number!",e);
			 e1=e;  break;         }

		else

		printf("\n\nValue of 'E'= %ld is not a prime number!",e);

		 printf("\n\nRetry for 'E'  ? <y/n>: ");

		scanf("%s",&choice);
		 e= rand()%100;

					}

long int d, k=1;
while((((k * phyn)+1) % e) != 0)
{k++;
d=(((k * phyn)+1) / e);
//printf("\n\nd=%d",d);
		    }
printf("\n\n\n\tFinal 'D' is %ld",d);


printf("\n\n\nPrivate Key\t\tD=%ld  N=%ld",d,n);
printf("\n\n\nPublic Key\t\tE=%ld  N=%ld",e,n);


getch();

}










⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -