RSA shifrlash. Faylni shifrlaymiz.
Salom. Bunda oldingiz RSA shifrlash maqolamda faqat matematik yo’llari haqida ko’rsatilgan. Bu safar shu matematikani qanday ishlatish haqida yozmoqchiman. Buning uchun bizga GMPLIB kerak bo’lad.
gmplib.org
Developer files: www.cs.nyu.edu/exact/core/gmp/
Sizga kodini ko’rishdan oldin sizga GMP funksiyalari haqida ozgina yozsam:
mpz_t — butun sonni saqlaydigan o’zgaruvchi
mpz_init(mpz_t a) — a ni ish holatiga tayyorlash
mpz_clear(mpz_t a) — a ni tozalash
mpz_add(r, a, b) — r = a + b ya’ni ikkita sonni qo’shib r ga yozib qo’yadi
mpz_sub(r, a, b) — r = a — b
mpz_mul(r, a, b) — r = a * b
mpz_addmul(r, a, b) — r += a * b
mpz_submul(r, a, b) — r -= a * b
mpz_neg(r, a) — r = -a
mpz_abs(r, a) — r = |a|
mpz_mod(r, a, b) — r = a mod b
mpz_powm(r, a, b, c) — r = a ^ b mod c
mpz_setbit(mpz_t a, int index) — a sonning index dagi bitini 1 qilib qo’yadi
mpz_probab_prime_p(mpz_t a, r) — a sonning tub son ekanligiga tekshiradi. r sanoq sistema. http://ru.wikipedia.org/wiki/Тест_Миллера_—_Рабина
gmp_printf(…) — printf ning gmp dagi analogi. Katta sonlar uchun.
GMP dagi mpz_t ni kor’sangiz
typedef __mpz_struct mpz_t[1];
deb e’lon qilingan. Bundan
typedef struct
{
int _mp_alloc; /* Number of *limbs* allocated and pointed
to by the _mp_d field. */
int _mp_size; /* abs(_mp_size) is the number of limbs the
last field points to. If _mp_size is
negative this is a negative number. */
mp_limb_t *_mp_d; /* Pointer to the limbs. */
} __mpz_struct;
bundan
typedef unsigned long int mp_limb_t;
demak mpz_t dagi katta soni mp_limb_t array ko’rinishida saqlanarkan. Bundan write_to_file funksiyasida foydalanilgan.
Qolgani kodning ichida:
#include
#include
#include
#include
#ifdef _DEBUG
#pragma comment(lib, "c:/gmp/gmpDebug.lib")
#else
#pragma comment(lib, "c:/gmp/gmp.lib")
#endif
/**
* Tasodifiy son tanlab uning tub son ekanligiga tekshiramiz
* agar tub son bo'lsa tasodifiy son tanlashni to'xtatamiz
*/
void select_prime_number(mpz_t *n, int bit_len)
{
for(;;)
{
mpz_set_ui(*n, 0);
for(int i = 0; i = 50)
mpz_setbit(*n, i);
}
if (mpz_probab_prime_p(*n, 10))
break;
}
}
/**
* Fayldan bayt ma bayt o'qib uni mpz_t ga son ko'rinishga o'tkazamiz
*/
void load_from_file(mpz_t *data, const char *file)
{
FILE *f = fopen(file, "rb");
int index = 0;
while(1)
{
unsigned char c = fgetc(f);
if (feof(f)) break;
for(int i = 0; i > 1;
index++;
}
}
fclose(f);
}
/**
* mpz_t sonini faylga char ko'rinishida yozamiz
*/
void write_to_file(mpz_t *data, const char *file)
{
FILE *f = fopen(file, "wb");
for(int i = 0; i > 8;
}
}
fclose(f);
}
void main()
{
time_t timer;
time(&timer);
srand (timer);
//o'zgaruvchilarni e'lon qilish
mpz_t p, q, m, N, phi, e, d, _t1, _t2, data, encoded;
mpz_init(p);
mpz_init(q);
mpz_init(N);
mpz_init(m);
mpz_init(phi);
mpz_init_set_str(e, "65537", 10);
mpz_init(d);
mpz_init(_t1);
mpz_init(_t2);
mpz_init_set_str(data, "0", 10);
mpz_init_set_str(encoded, "0", 10);
//Tasodifiy tub son tanlash
select_prime_number(&p, 1024);
select_prime_number(&q, 1024);
//N ni hisoblash
mpz_mul(N, p, q);
gmp_printf(" N = %Zdn", N);
//p va q ni 1 ga kamaytiramiz
mpz_sub_ui(p, p, 1);
mpz_sub_ui(q, q, 1);
//phi ni hisoblaymiz
mpz_mul(phi, p, q);
gmp_printf("PHI = %Zdn", phi);
gmp_printf(" E = %Zdn", e);
//d ni qidiramiz
for(int k = 0;; k++)
{
//_t = k * phi
mpz_mul_ui(_t1, phi, k);
//_t += 1
mpz_add_ui(_t1, _t1, 1);
//_t mod e
mpz_mod(_t2, _t1, e);
if (mpz_cmp_ui(_t2, 0) == 0)
{
mpz_div(d, _t1, e);
break;
}
}
gmp_printf(" D = %Zdnn", d);
//fayldan o'qimiz
load_from_file(&data, "data.txt");
gmp_printf(" DT = %Zdn", data);
mpz_powm(data, data, e, N);
//shifrlangan ma'lumotni faylga yozamiz
FILE *f = fopen("encoded.bin", "wb");
mpz_out_raw(f, data);
fclose(f);
//deshifrlash
mpz_powm(encoded, data, d, N);
gmp_printf("EDT = %Zdn", encoded);
//deshifrlangan ma'lumotni faylga yozamiz
write_to_file(&encoded, "decoded.txt");
//o'zgaruvchilarni yozalash
mpz_clear(p);
mpz_clear(q);
mpz_clear(m);
mpz_clear(N);
mpz_clear(phi);
mpz_clear(e);
mpz_clear(d);
mpz_clear(_t1);
mpz_clear(_t2);
mpz_clear(data);
mpz_clear(encoded);
}
Shifrlangan fayl encoded.bin da saqlanyapti.
Savollar bo’lsa marhamat.
Texnologiyalar
RSA shifrlash. Faylni shifrlaymiz.