Tоshkеnt aхbоrоt tехnоlоgiyalari univеrsitеti kriptografiya 2 fanidan Loyiha ishi Bajardi


Download 183.1 Kb.
bet4/4
Sana16.06.2023
Hajmi183.1 Kb.
#1514676
1   2   3   4
Bog'liq
loyiha ishi

Faraz qilaylik, q= 8191 va p= 8111 ikkita tub son va ochiq kalit e= 2047. Maxfiy kalitlarni hisoblang. Nima uchun ochiq kalitni tanlash e= 2048 ishlamayotganligini tushuntiring.



p=8111, q=8191 va e= 2047 bo’lgan holda d maxfiy kalit topildi.










Dasturning c++ tilidagi kodi:


#include
#include
#include
#include
using namespace std;
typedef struct {
int d;
int x;
int y;
} EE;
EE extended_euclid(int a, int b) {
EE ee1, ee2, ee3;
if (b == 0) {
ee1.d = a;
ee1.x = 1;
ee1.y = 0;
return ee1;
} else {
ee2 = extended_euclid(b, a % b);
ee3.d = ee2.d;
ee3.x = ee2.y;
ee3.y = ee2.x - floor(a / b) * ee2.y;
return ee3;
}
}
///////////////////////////////
int gcd(int fi, int e)
{
// Find Minimum of a and b
int result = min(fi, e);
while (result > 0) {
if (fi % result == 0 && e % result == 0) {
break;
}
result--;
}


// Return gcd of a and b
return result;
}
//////////////////////////////////////////////
int modulo(int x, int N){
return (x % N + N) % N;
}
void decimal_to_binary(int op1, int aOp[]){
int result, i = 0;
do{
result = op1 % 2;
op1 /= 2;
aOp[i] = result;
i++;
}while(op1 > 0);
}
int modular_exponentiation(int a, int b, int n){
int *bb;
int count = 0, c = 0, d = 1, i;
//bning ikkilik o'lchamini topish
count = (int) (log(b)/log(2)) + 1;
bb = (int *) malloc(sizeof(int*) * count);
decimal_to_binary(b, bb);
for (i = count - 1; i >= 0; i--) {
c = 2 * c;
d = (d*d) % n;
if (bb[i] == 1) {
c = c + 1;
d = (d*a) % n;
}
}
return d;
}
int get_d(int e, int fi){
EE ee;
ee = extended_euclid(e, fi);
return modulo(ee.x, fi);
}
int main(int argc, char* argv[]) {
int p, q, fi, n, e, d;
printf("p ning qiymatini kiriting: ");
scanf("%d", &p);
printf("q ning qiymatini kiriting: ");
scanf("%d", &q);
n = p*q;
fi = (p - 1) * (q - 1);
printf("e ning qiymatini kiriting: ");
scanf("%d", &e);
printf("fi(N): %d\n", fi);
if(gcd(fi, e) == 1){
d = get_d(e, fi);
printf("Maxfiy kalit: (d = %d)\n", d);
}
else{
printf("fi(N) va e o'zaro tub emas !!!");
}
return 0;
}
Download 183.1 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling