Упражнение 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> | ||
/* | /*********************************************************************\ | ||
* Title: RSA | |||
* Description: Генериране на ключове, криптиране и декриптиране с RSA | |||
* Edited by: ilianko | |||
* Idea: http://cppgm.blogspot.com/2008/01/rsa-algorithm.html | |||
* | |||
\*********************************************************************/ | |||
#include <stdio.h> | #include <stdio.h> | ||
int phi | 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; | iMax = ( u > phi) ? u : phi; | ||
for( i=3; i < iMax ; i = i+2) | |||
{ | { | ||
if(u%i==0 && phi%i==0) return 1; | |||
} | } | ||
return 0; | return 0; | ||
}; | }; | ||
| Line 22: | Line 38: | ||
{ | { | ||
int i; | int i; | ||
C = 1; | C = 1; | ||
for(i=0;i< | for(i=0; i < u; i++) | ||
C = C*M%s; // modular arithmetic | |||
C = C% | |||
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< | for(i=0; i < r;i++) | ||
M=M*C%s; | |||
M = M% | M = M%s; | ||
printf("\n\tDecrypted keyword : %d",M); | printf("\n\tDecrypted keyword : %d",M); | ||
} | } | ||
| Line 41: | Line 59: | ||
int main() | int main() | ||
{ | { | ||
int | int v,t; /* Изходящи праметри, избират се две произволни прости числа, | ||
* колкото по-големи, толкова "по-изчислителноемко" е разбиването. */ | |||
printf("Enter Two | int temp = 0; | ||
scanf("%d%d",& | printf("Enter Two Prime Numbers\t: "); | ||
scanf("%d%d",&v,&t); | |||
phi=( | |||
printf("\n\ | s = v*t; // модул на ключовете | ||
phi=(v-1)*(t-1); // Euler's totient function | |||
printf("\n\t Phi(s)\t= %i", phi); | |||
do | do | ||
{ | { | ||
printf("\n\ | printf("\n\n Enter public key exponent\t: "); | ||
scanf("%d",& | 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: "); | 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(); | ||
return 0; | return 0; | ||
} | } | ||
</ | </code></pre> | ||
[[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>