Example #1
0
int main()
{
	int p, q, e, d, m, n, t, c, r;
	char s;
	printf("Please input the p, q: ");
	scanf("%d%d", &p, &q);

	n = p * q;
	printf("the n is %3d\n", n);
	t = (p - 1) * (q - 1);
	printf("the t is %3d\n", t);

	printf("Please input the e: ");
	scanf("%d", &e);

	if(e < 1 || e > t)
	{
		printf("e is error. Please input again");
		scanf("%d", &e);
	}

	d = 1;
	while(((e * d) % t) != 1)
		d++;
	printf("then calculate out that the d is %d\n", d);
	printf("the cipher please input 1\n");
	printf("the plain please input 2\n");
	scanf("%d", &r);
	switch(r)
	{
		case 1: printf("input the m: "); //输入要加密的明文数字
		scanf("%d", &m);
		c = candp(m, e, n);
		printf("the cipher is %d\n", c);
		break;

		case 2: printf("input the c: ");//输入要解密的密文数字
		scanf("%d", &c);
		m = candp(c, d ,n);
		printf("the plain is %d\n", m);
		break;
	}
	getch();
	return 0;
}
Example #2
0
int main ( void ) { 
  int i, p=53, q=61;
  int n = p*q;
  int f = (p-1)*(q-1), e = getE(f), d = getD(e, f);
  int data = 123, encry, decry;

  printf("p = %d, q = %d, f = %d, e = %d, d = %d\ndata = %d\n", p, q, f, e, d, data );  
  
  encry = candp(data, e, f);
  if ( encry == data ) {
    printf("%s\n","cao");
  }
  printf("the encry is %d\n", encry);
  
  decry = candp(encry, d, f);
  printf("the decry is %d\n", decry);
  
}