Упражнение 10. Цифров подпис: Difference between revisions

From Ilianko
No edit summary
No edit summary
Line 1: Line 1:
'''Задача 1.''' Да се намери остатъкът на числото 3^256 разделено на 4 ( 3^256%4=?):
'''Задача 2.''' Да се намери остатъкът на числото 85^256 разделено на 83 ( 3^256%4=?):
'''Задача 3.''' Да се тества програмата. Допълнете програмата, така че да генерира и избира автоматично стойност за експонентата на публичния ключ
<code><pre>
<code><pre>
/* C program for the Implementation Of RSA Algorithm */
/*********************************************************************\
* Title: RSA
* Description: Генериране на ключове, криптиране и декриптиране с RSA
* Edited by: ilianko
* Idea:  http://cppgm.blogspot.com/2008/01/rsa-algorithm.html
*
\*********************************************************************/


#include <stdio.h>
#include <stdio.h>
//#include <conio.h>


int phi,M,n,e,d,C,FLAG;
int r; // pRivate key exponent
int u; // pUblic key exponent
int phi; // phi(s) = (v-1)*(t-1) - Euler's totient function
int M; // Message
int C; // encrypted message
int s; // modulus for both the public and private keys, модул (делител) на ключовете
 


int check()
int check()
{
{
   int i;
   int i, iMax;
   for(i=3;e%i==0 && phi%i==0; i+2)
  iMax = ( u > phi) ? u : phi;
   for( i=3; i < iMax ; i = i+2)
   {
   {
     FLAG = 1;
     if(u%i==0 && phi%i==0) return 1;
    return 0;
   }
   }
  FLAG = 0;
   return 0;
   return 0;
};
};
Line 22: Line 38:
{
{
   int i;
   int i;
 
   C = 1;
   C = 1;
   for(i=0;i< e;i++)
   for(i=0; i < u; i++)
  C=C*M%n;
    C = C*M%s; //  modular arithmetic
   C = C%n;
 
   printf("\n\tEncrypted keyword : %d",C);
   C = C%s;
   printf( "\n\tEncrypted keyword : %d", C );
}
}


Line 33: Line 51:
   int i;
   int i;
   M = 1;
   M = 1;
   for(i=0;i< d;i++)
   for(i=0; i < r;i++)
  M=M*C%n;
    M=M*C%s;
   M = M%n;
   M = M%s;
   printf("\n\tDecrypted keyword : %d",M);
   printf("\n\tDecrypted keyword : %d",M);
}
}
Line 41: Line 59:
int main()
int main()
{
{
   int p,q,s;
   int v,t; /* Изходящи праметри, избират се две произволни прости числа,
   //clrscr();
            * колкото по-големи, толкова "по-изчислителноемко" е разбиването. */
   printf("Enter Two Relatively Prime Numbers\t: ");
   int temp = 0;
   scanf("%d%d",&p,&q);
   printf("Enter Two Prime Numbers\t: ");
   n = p*q;
   scanf("%d%d",&v,&t);
   phi=(p-1)*(q-1);
    
   printf("\n\tF(n)\t= %d",phi);
  s = v*t; // модул на ключовете
   phi=(v-1)*(t-1); // Euler's totient function
 
   printf("\n\t Phi(s)\t= %i", phi);
 
   do
   do
   {
   {
     printf("\n\nEnter e\t: ");
     printf("\n\n Enter public key exponent\t: ");
     scanf("%d",&e);
     scanf("%d",&u);
    check();
   }while( check() ); //Проверка дали избраната експонента е относително проста
   }
 
  while(FLAG==1);
  d = 1;
    
    
   do
   r = 1;
  while(temp != 1 )
   {
   {
     s = (d*e)%phi;
     temp = (r*u)%phi;
     d++;
     r++;
   }while(s!=1);
   };
  r--;
 
  printf("\n\tPublic Key\t: {%d,%d}",u,s);
  printf("\n\tPrivate Key\t: {%d,%d}",r,s);
    
    
  d = d-1;
  printf("\n\tPublic Key\t: {%d,%d}",e,n);
  printf("\n\tPrivate Key\t: {%d,%d}",d,n);
   printf("\n\nEnter The Plain Text\t: ");
   printf("\n\nEnter The Plain Text\t: ");
   scanf("%d",&M);
   scanf("%d",&M);
   encrypt();
   encrypt();
 
   printf("\n\nEnter the Cipher text\t: ");
   printf("\n\nEnter the Cipher text\t: ");
   scanf("%d",&C);
   scanf("%d",&C);
   decrypt();
   decrypt();
   //getch();
    
   return 0;
   return 0;
}
}
</pre></code>
</code></pre>


== References ==
Идеята на програмата е взаимствана от [http://cppgm.blogspot.com/2008/01/rsa-algorithm.html RSA C programmin example]
[[Category:Компютърна периферия]]
[[Category:Компютърна периферия]]

Revision as of 09:38, 15 May 2011

Задача 1. Да се намери остатъкът на числото 3^256 разделено на 4 ( 3^256%4=?): Задача 2. Да се намери остатъкът на числото 85^256 разделено на 83 ( 3^256%4=?):


Задача 3. Да се тества програмата. Допълнете програмата, така че да генерира и избира автоматично стойност за експонентата на публичния ключ

/*********************************************************************\
 * Title: RSA
 * Description: Генериране на ключове, криптиране и декриптиране с RSA 
 * Edited by: ilianko
 * Idea:  http://cppgm.blogspot.com/2008/01/rsa-algorithm.html
 * 
\*********************************************************************/

#include <stdio.h>

int r; // pRivate key exponent
int u; // pUblic key exponent
int phi; // phi(s) = (v-1)*(t-1) - Euler's totient function
int M; // Message
int C; // encrypted message
int s; // modulus for both the public and private keys, модул (делител) на ключовете


int check()
{
  int i, iMax;
  iMax = ( u > phi) ? u : phi;
  for( i=3; i < iMax ; i = i+2)
  {
    if(u%i==0 && phi%i==0) return 1;
  }
  return 0;
};

void encrypt()
{
  int i;
  
  C = 1;
  for(i=0; i < u; i++)
    C = C*M%s; //  modular arithmetic
  
  C = C%s;
  printf( "\n\tEncrypted keyword : %d", C );
}

void decrypt()
{
  int i;
  M = 1;
  for(i=0; i < r;i++)
    M=M*C%s;
  M = M%s;
  printf("\n\tDecrypted keyword : %d",M);
}

int main()
{
  int v,t; /* Изходящи праметри, избират се две произволни прости числа,
            * колкото по-големи, толкова "по-изчислителноемко" е разбиването. */
  int temp = 0;
  printf("Enter Two Prime Numbers\t: ");
  scanf("%d%d",&v,&t);
  
  s = v*t; // модул на ключовете
  phi=(v-1)*(t-1); // Euler's totient function
  
  printf("\n\t Phi(s)\t= %i", phi);
  
  do
  {
    printf("\n\n Enter public key exponent\t: ");
    scanf("%d",&u);
  }while( check() ); //Проверка дали избраната експонента е относително проста

  
  r = 1;
  while(temp != 1 )
  {
    temp = (r*u)%phi;
    r++;
  };
  r--;
  
  printf("\n\tPublic Key\t: {%d,%d}",u,s);
  printf("\n\tPrivate Key\t: {%d,%d}",r,s);
  
  printf("\n\nEnter The Plain Text\t: ");
  scanf("%d",&M);
  encrypt();
  
  printf("\n\nEnter the Cipher text\t: ");
  scanf("%d",&C);
  decrypt();
  
  return 0;
}
</code>