Ejemplo: cifrado c rsa
#include #include #include #include int e, d, n;intgcd(int a,int b)int q, r1, r2, r;if(a > b)
r1 = a;
r2 = b;else
r1 = b;
r2 = a;while(r2 >0)
q = r1 / r2;
r = r1 - q * r2;
r1 = r2;
r2 = r;return r1;intPrimarityTest(int a,int i)intFindT(int a,int m,int n)int r;int y =1;while(m >0)
r = m %2;FastExponention(r, n,&y,&a);
m = m /2;return y;intFastExponention(int bit,int n,int* y,int* a)if(bit ==1)*y =(*y *(*a))% n;*a =(*a)*(*a)% n;intinverse(int a,int b)int inv;int q, r, r1 = a, r2 = b, t, t1 =0, t2 =1;while(r2 >0)
q = r1 / r2;
r = r1 - q * r2;
r1 = r2;
r2 = r;
t = t1 - q * t2;
t1 = t2;
t2 = t;if(r1 ==1)
inv = t1;if(inv <0)
inv = inv + a;return inv;intKeyGeneration()int p, q;int phi_n;dodo
p =rand();while(p %2==0);while(!PrimarityTest(2, p));dodo
q =rand();while(q %2==0);while(!PrimarityTest(2, q));
n = p * q;
phi_n =(p -1)*(q -1);do
e =rand()%(phi_n -2)+2;// 1 < e < phi_nwhile(gcd(e, phi_n)!=1);
d =inverse(phi_n, e);intEncryption(int value, FILE* out)int cipher;
cipher =FindT(value, e, n);fprintf(out,"%d ", cipher);intDecryption(int value, FILE* out)int decipher;
decipher =FindT(value, d, n);fprintf(out,"%c", decipher);intmain(void)
FILE *inp,*out;// destroy contents of these files (from previous runs, if any)
out =fopen("cipher.txt","w+");fclose(out);
out =fopen("decipher.txt","w+");fclose(out);KeyGeneration();
inp =fopen("plain.txt","r+");if(inp ==NULL)printf("Error opening Source File.n");exit(1);
out =fopen("cipher.txt","w+");if(out ==NULL)printf("Error opening Destination File.n");exit(1);// encryption startswhile(1)char ch =getc(inp);if(ch ==-1)break;int value =toascii(ch);Encryption(value, out);fclose(inp);fclose(out);// decryption starts
inp =fopen("cipher.txt","r");if(inp ==NULL)printf("Error opening Cipher Text.n");exit(1);
out =fopen("decipher.txt","w+");if(out ==NULL)printf("Error opening File.n");exit(1);while(1)int cip;if(fscanf(inp,"%d",&cip)==-1)break;Decryption(cip, out);fclose(out);return0;
valoraciones y comentarios
Agradecemos que desees añadir valor a nuestro contenido cooperando tu experiencia en las explicaciones.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)