0% found this document useful (0 votes)
23 views15 pages

Dgu 3

The document describes a software program that automates the encryption and decryption of text data using the RSA algorithm, implemented in C++. It generates public and private keys based on two prime numbers provided by the user, and allows for the reading of plaintext from an input file, encryption, and writing the encrypted text to an output file. The program also includes functionalities for modular exponentiation, calculating the GCD, and finding modular inverses, creating a practical environment for learning and applying the RSA algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views15 pages

Dgu 3

The document describes a software program that automates the encryption and decryption of text data using the RSA algorithm, implemented in C++. It generates public and private keys based on two prime numbers provided by the user, and allows for the reading of plaintext from an input file, encryption, and writing the encrypted text to an output file. The program also includes functionalities for modular exponentiation, calculating the GCD, and finding modular inverses, creating a practical environment for learning and applying the RSA algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DGU uchun dastur

IDS va IPS tizimlari: ishlash prinsipi va qo‘llanilishi

Mualliflar: Ramazonova Madina Shavkatovna, Najmiddinov Samandar Fayzulla


o`g`li, Abdumutalov Behruzbek Ma’rufjonovich, Saidov Yaxshimurodbek
Umidjonovich, Akbarov Alimurod Aziz o'g'li, Ergasheva Farida Yunus qizi, ,
Zoirov Diyorbek Dilshod o’g’li, Tursunov Samandarbek Raxmatilla o’g’li, Jalilov
Muhammad, Maxamatjonov Muxammadqodir Rashidbek o’g’li.

Toshkent - 2025
Annotatsiya: Ushbu dasturiy ta'minot RSA algoritmiga asoslangan matnli
ma’lumotlarni shifrlash va deshifrlash jarayonlarini avtomatlashtiradi. Dastur C++
tilida yozilgan bo‘lib, foydalanuvchidan ikkita tub son qabul qilinadi va ular
asosida ochiq (public) va maxfiy (private) kalitlar generatsiya qilinadi. Kalitlar
keys.txt fayliga saqlanadi.
Shifrlash bosqichida foydalanuvchi matni input.txt faylidan o‘qiladi va
RSA algoritmi yordamida kodlanib, encrypted.txt fayliga yoziladi.
Deshifrlash bosqichida esa encrypted.txt dan o‘qilgan shifrlangan
ma’lumotlar ochiladi va decrypted.txt fayliga tik holatda qayta yoziladi.
Dastur modulli darajaga ko‘tarish (modular exponentiation), tub sonlar
uchun Euler funksiyasi (phi), GCD va modulyar teskari element (modular inverse)
kabi kriptografik asosiy amallarni o‘z ichiga oladi. Bu yondashuv RSA algoritmini
o‘rganish va amaliyotda qo‘llash uchun qulay muhit yaratadi.

Dastur kodi
#include <iostream>

#include <cmath>

#include <vector>

using namespace std;

// GCD (eng katta umumiy bo'luvchi)

int gcd(int a, int b) {

return b == 0 ? a : gcd(b, a % b);

// Modular exponentiation (a^b mod n)

long long mod_pow(long long base, long long exp, long long mod) {

long long result = 1;


base = base % mod;

while (exp > 0) {

if (exp % 2 == 1)

result = (result * base) % mod;

exp = exp >> 1; // exp /= 2

base = (base * base) % mod;

return result;

// Euler phi funksiyasi

int phi(int n) {

int result = n;

for (int p = 2; p * p <= n; ++p) {

if (n % p == 0) {

while (n % p == 0)

n /= p;

result -= result / p;

if (n > 1)

result -= result / n;
return result;

// Invers elementni topish (e^-1 mod phi_n)

int mod_inverse(int e, int phi_n) {

int t = 0, newt = 1;

int r = phi_n, newr = e;

while (newr != 0) {

int quotient = r / newr;

int temp = newt;

newt = t - quotient * newt;

t = temp;

temp = newr;

newr = r - quotient * newr;

r = temp;

if (r > 1) return -1; // Invers yo'q

if (t < 0) t += phi_n;

return t;

}
// Kalit generatsiyasi

void generate_keys(int& n, int& e, int& d) {

int p, q;

cout << "2 ta tub son kiriting (masalan, 61 va 53): ";

cin >> p >> q;

n = p * q;

int phi_n = (p - 1) * (q - 1);

// e tanlash

e = 2;

while (e < phi_n) {

if (gcd(e, phi_n) == 1) break;

++e;

d = mod_inverse(e, phi_n);

if (d == -1) {

cout << "xatolik: e uchun mod invers topilmadi.\n";

exit(1);

cout << "🔐 Public key: (" << e << ", " << n << ")\n";
cout << "🔒 Private key: (" << d << ", " << n << ")\n";

// Matnni RSA orqali shifrlash

vector<long long> encrypt(const string& msg, int e, int n) {

vector<long long> cipher;

for (char c : msg) {

int m = (int)c;

long long ciph = mod_pow(m, e, n);

cipher.push_back(ciph);

return cipher;

// RSA orqali deshifrlash

string decrypt(const vector<long long>& cipher, int d, int n) {

string result;

for (long long ciph : cipher) {

char m = (char)mod_pow(ciph, d, n);

result += m;

return result;
}

int main() {

int n, e, d;

generate_keys(n, e, d);

cin.ignore();

string message;

cout << "\nMatn kiriting: ";

getline(cin, message);

vector<long long> encrypted = encrypt(message, e, n);

cout << "\n🔐 Shifrlangan (RSA): ";

for (auto c : encrypted) cout << c << " ";

cout << endl;

string decrypted = decrypt(encrypted, d, n);

cout << "🔓 Ochilgan matn: " << decrypted << endl;

return 0;

#include <iostream>
#include <fstream>

#include <vector>

#include <string>

#include <cmath>

using namespace std;

// GCD

int gcd(int a, int b) {

return b == 0 ? a : gcd(b, a % b);

// Modular exponentiation

long long mod_pow(long long base, long long exp, long long mod) {

long long result = 1;

base %= mod;

while (exp > 0) {

if (exp % 2) result = (result * base) % mod;

base = (base * base) % mod;

exp >>= 1;

return result;

}
// Euler phi

int phi(int n) {

int result = n;

for (int p = 2; p * p <= n; p++) {

if (n % p == 0) {

while (n % p == 0)

n /= p;

result -= result / p;

if (n > 1)

result -= result / n;

return result;

// Modular inverse

int mod_inverse(int e, int phi_n) {

int t = 0, newt = 1;

int r = phi_n, newr = e;

while (newr != 0) {

int q = r / newr;
int temp = newt;

newt = t - q * newt;

t = temp;

temp = newr;

newr = r - q * newr;

r = temp;

if (r > 1) return -1;

if (t < 0) t += phi_n;

return t;

void generate_keys(int& n, int& e, int& d) {

int p, q;

cout << "🔢 Iltimos, 2 ta tub son kiriting (masalan 61 va 53): ";

cin >> p >> q;

n = p * q;

int phi_n = (p - 1) * (q - 1);

e = 3;

while (gcd(e, phi_n) != 1) e++;


d = mod_inverse(e, phi_n);

ofstream fout("keys.txt");

fout << e << " " << d << " " << n;

fout.close();

cout << "✅ Kalitlar 'keys.txt' faylga yozildi: Public(" << e << ", " << n << "),
Private(" << d << ", " << n << ")\n";

string read_file(const string& filename) {

ifstream file(filename);

string content((istreambuf_iterator<char>(file)),
istreambuf_iterator<char>());

file.close();

return content;

void write_file(const string& filename, const string& data) {

ofstream file(filename);

file << data;

file.close();
}

void encrypt_text() {

int e, d, n;

ifstream fin("keys.txt");

if (!(fin >> e >> d >> n)) {

cout << "❌ Kalit fayli topilmadi yoki noto‘g‘ri.\n";

return;

fin.close();

string msg = read_file("input.txt");

vector<long long> cipher;

for (char c : msg)

cipher.push_back(mod_pow((int)c, e, n));

ofstream out("encrypted.txt");

for (auto val : cipher)

out << val << " ";

out.close();
cout << "🔐 Matn 'input.txt'dan o‘qildi va 'encrypted.txt' ga shifrlab
yozildi.\n";

void decrypt_text() {

int e, d, n;

ifstream fin("keys.txt");

if (!(fin >> e >> d >> n)) {

cout << "❌ Kalit fayli topilmadi yoki noto‘g‘ri.\n";

return;

fin.close();

ifstream in("encrypted.txt");

vector<long long> cipher;

long long val;

while (in >> val)

cipher.push_back(val);

in.close();

string result;

for (auto c : cipher)


result += (char)mod_pow(c, d, n);

write_file("decrypted.txt", result);

cout << "🔓 Matn ochildi va 'decrypted.txt' ga yozildi.\n";

int main() {

int choice;

do {

cout << "\n📦 RSA Kengaytirilgan Dasturi\n";

cout << "1. Kalit generatsiya qilish\n";

cout << "2. Matnni shifrlash (input.txt dan)\n";

cout << "3. Matnni ochish (encrypted.txt dan)\n";

cout << "0. Chiqish\n";

cout << "Tanlovingiz: ";

cin >> choice;

switch (choice) {

case 1: generate_keys(*(new int), *(new int), *(new int)); break;

case 2: encrypt_text(); break;

case 3: decrypt_text(); break;

case 0: cout << "Dastur tugadi.\n"; break;


default: cout << "❗ Noto‘g‘ri tanlov.\n";

} while (choice != 0);

return 0;

You might also like