Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <fstream>
- #include <string>
- #include <iostream>
- #include <cmath>
- using namespace std;
- struct KluczeRSA
- {
- // (e,n) - klucz publiczny
- // (d,n) - klucz prywatny
- int e;
- int n;
- int d;
- };
- string szyfruj(string, string);
- string deszyfruj(string, string);
- string toUpper(string);
- string autoHaslo(string);
- bool czyPierwsza(int);
- int euler(int, int);
- int znajdzWzgledniePierwsza(int);
- KluczeRSA generujKlucze(int&, int&);
- const char ASCII[36] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
- 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- int main()
- {
- int pos;
- int p, q, n; // p,q - liczby pierwsze wczytane od uzytkownika; n = p*q
- string str, inData, upperData, wynik, haslo, upperHaslo;
- ifstream inFile;
- ifstream inPass;
- ofstream outFile;
- const string path = "in.txt";
- const string passPath = "pass.txt";
- const string outFilePath = "out.txt";
- inFile.open(path, ios::in);
- inPass.open(passPath, ios::in);
- outFile.open(outFilePath, ios::out);
- cout << "Dane wczytywane sa z pliku: \"" << path << "\"" << endl;
- while (getline(inFile, str))
- {
- inData += str;
- }
- cout << "Wczytano: " << inData << endl;
- cout << "---" << endl;
- cout << "Podaj liczbe pierwsza: ";
- cin >> p;
- cout << endl;
- if (!czyPierwsza(p))
- {
- cout << "Liczba " << p << " nie jest pierwsza. Program konczy prace." << endl;
- system("pause");
- inFile.close();
- inPass.close();
- outFile.close();
- return 1;
- }
- cout << "podaj kolejna liczbe pierwsza: ";
- cin >> q;
- cout << endl;
- if (!czyPierwsza(q))
- {
- cout << "Liczba " << q << " nie jest pierwsza. Program konczy prace." << endl;
- system("pause");
- inFile.close();
- inPass.close();
- outFile.close();
- return 1;
- }
- cout << "Wczytano liczby: " << p << ", " << q << ". Na ich podstawie wygenerowane zostana klucze prywatny i publiczny." << endl << endl;
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- //DOPISAC - TUTAJ PRZERWANO
- // e - liczba wzglednie pierwsza z eu, 1 < e < eu
- //int e =
- KluczeRSA keys = generujKlucze(p, q);
- /*cout << "---" << endl;
- cout << "Dane zapisywane sa do pliku: \"" << outFilePath << "\" w formacie: \nHaslo\nZaszyfrowana wiadomosc\nOdszyfrowana wiadomosc" << endl;
- cout << "---" << endl;*/
- /*wynik = szyfruj(upperData, upperHaslo);
- cout << wynik << endl;
- outFile << wynik << endl;*/
- //wynik = deszyfruj(wynik, upperHaslo);
- //cout << wynik << endl;
- //outFile << wynik << endl;
- system("pause");
- inFile.close();
- inPass.close();
- outFile.close();
- return 0;
- }
- // Te 4 zostaly z poprzedniej wersji
- string toUpper(string data)
- {
- int znak;
- string out = data;
- for (unsigned int i = 0; i < data.length(); ++i)
- {
- znak = (int)data[i];
- if ((znak >= 97) && (znak <= 122))
- {
- znak -= 32;
- out[i] = (char)znak;
- }
- }
- return out;
- }
- string szyfruj(string data, string haslo)
- {
- int znak;
- int pos;
- string out = data;
- cout << "Szyfrowanie" << endl;
- for (unsigned int i = 0; i < data.length(); ++i)
- {
- znak = (int)data[i];
- pos = (int)haslo[i];
- if ((pos <= 90) && (pos >= 65))
- {
- pos = pos - 65;
- }
- else if ((pos <= 57) && (pos >= 48))
- {
- pos = pos - 48 + 26;
- }
- if ((znak <= 90) && (znak >= 65))
- {
- znak = znak - 65;
- }
- else if ((znak <= 57) && (znak >= 48))
- {
- znak = znak - 48 + 26;
- }
- else continue;
- znak += pos;
- znak = znak % 36; // 36 - szerokosc tablicy ze znakami
- out[i] = ASCII[znak];
- }
- return out;
- }
- string deszyfruj(string data, string haslo)
- {
- int znak;
- int pos;
- string out = data;
- cout << "Deszyfrowanie" << endl;
- for (unsigned int i = 0; i < data.length(); ++i)
- {
- znak = (int)data[i];
- pos = (int)haslo[i];
- if ((pos <= 90) && (pos >= 65))
- {
- pos = pos - 65;
- }
- else if ((pos <= 57) && (pos >= 48))
- {
- pos = pos - 48 + 26;
- }
- if ((znak <= 90) && (znak >= 65))
- {
- znak = znak - 65;
- }
- else if ((znak <= 57) && (znak >= 48))
- {
- znak = znak - 48 + 26;
- }
- else continue;
- znak -= pos;
- while (znak < 0)
- {
- znak += 36;
- }
- znak = znak % 36; // 36 - szerokosc tablicy ze znakami
- out[i] = ASCII[znak];
- }
- return out;
- }
- string autoHaslo(string data)
- {
- char pierwszyZnak;
- cout << "Podaj pierwsza literke: ";
- cin >> pierwszyZnak;
- int znak;
- string out = data;
- out[0] = pierwszyZnak;
- for (unsigned int i = 0; i < data.length(); ++i)
- {
- out[i + 1] = data[i];
- }
- return out;
- }
- // /\ Koniec z poprzedniej wersji
- bool czyPierwsza(int num)
- {
- if (num <= 1)
- return false;
- else if (num == 2)
- return true;
- else if (num % 2 == 0)
- return false;
- else
- {
- bool prime = true;
- int divisor = 3;
- double num_d = static_cast<double>(num);
- int upperLimit = static_cast<int>(sqrt(num_d) + 1);
- while (divisor <= upperLimit)
- {
- if (num % divisor == 0)
- prime = false;
- divisor += 2;
- }
- return prime;
- }
- }
- int nwd(int a, int b)
- {
- int t;
- while (b != 0)
- {
- t = b;
- b = a % b;
- a = t;
- };
- return a;
- }
- int znajdzWzgledniePierwsza(int eu)
- {
- int e = 3;
- for (; nwd(e, eu) != 1; e += 2);
- return e;
- }
- int odwrMod(int e, int eu)
- {
- int a0, n0, p0, p1, q, r, t;
- p0 = 0; p1 = 1; a0 = e; n0 = eu;
- q = n0 / a0;
- r = n0 % a0;
- while (r > 0)
- {
- t = p0 - q * p1;
- if (t >= 0)
- t = t % eu;
- else
- t = eu - ((-t) % eu);
- p0 = p1; p1 = t;
- n0 = a0; a0 = r;
- q = n0 / a0;
- r = n0 % a0;
- };
- return p1;
- }
- KluczeRSA generujKlucze(int& p, int& q)
- {
- KluczeRSA keys;
- keys.n = p*q;
- // eu - wartosc funkcji eulera dla n
- int eu = (p - 1)*(q - 1);
- // e - liczba wzglednie pierwsza do eu
- keys.e = znajdzWzgledniePierwsza(eu);
- // d - odwrotnosc modulu
- keys.d = odwrMod(keys.e, eu);
- // tutaj przerwano
- //----------------------------------------
- // Klucz publiczny to para liczb (e, n), |
- // prywatny - (d, n) |
- //----------------------------------------
- cout << "KLUCZ PUBLICZNY:" << endl
- << "wykladnik eu = " << eu << endl
- << " modul n = " << keys.n << endl
- << endl
- << "KLUCZ PRYWATNY:" << endl
- << "wykladnik d = " << keys.d << endl
- << " modul n = " << keys.n << endl
- << "Aby kontynuowac nacisnij dowolny klawisz." << endl << endl;
- cin.get();
- return keys;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement