Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //link https://yadi.sk/d/qbiPvZ7pAGif5A
- //RSA шифр
- #include "stdafx.h"
- #include <NTL/ZZ.h>
- #include <fstream>
- #include <iostream>
- #include <math.h>
- #include <cstdlib>
- #include <sstream>
- #include <string>
- NTL_CLIENT
- #pragma comment( lib, "NTL.lib" )
- using namespace NTL;
- int y = 0;
- ZZ* arr;
- int x = 0;
- bool i = false;
- int symbcount(ZZ n){ //подсчет символов для задания массива
- int x;
- int c;
- y = 0;
- x = -1;
- ifstream one("text.txt");
- if (one.is_open()){
- while (!one.eof())
- {
- c = one.get();
- if (c < 0)
- c += 256;
- if (c > n){
- y++;
- }
- if (c / 10 > n){
- y++;
- }
- x++;
- }
- one.close();
- y += x;
- }
- else
- x = -2;
- return x;
- }
- ZZ modinv(ZZ x, ZZ y){
- ZZ b, c, i, j, x1, y1;
- b = y;
- c = x;
- i = 0;
- j = 1;
- while (c != 0){
- x1 = b / c;
- y1 = b%c;
- b = c;
- c = y1;
- y1 = j;
- j = i - j*x1;
- i = y1;
- }
- if (i < 0)
- i += y;
- return i;
- }
- ZZ rec(ZZ ka){
- ZZ k;
- bool b;
- b = false;
- for (k = ka - 1; k > 2; k--){
- if (ka%k == 0)
- b = true;
- }
- if (b == true)
- ka = rec(ka -= 1);
- return ka;
- }
- void TxtInArr(int** ascii, int a, ZZ n){
- ifstream onet("text.txt");
- int cc;
- for (int k = 0; k < a; k++){
- ascii[k] = new int[2];
- cc = onet.get();
- if (cc < 0)
- cc += 256;
- if (cc >= n){
- if (cc / 10 < n){
- ascii[k][0] = cc / 10;
- ascii[k][1] = cc % 10;
- ascii[k][2] = -1;
- }
- else {
- ascii[k][0] = cc / 100;
- ascii[k][1] = (cc % 100) / 10;
- ascii[k][2] = (cc % 100) % 10;
- }
- }
- else
- {
- ascii[k][0] = cc;
- ascii[k][1] = -1;
- ascii[k][2] = -1;
- }
- }
- onet.close();
- }
- void enc(ZZ* zzbuff, int** ascii, int a, ZZ e, ZZ n){
- int yz = -1;
- ZZ f;
- ofstream out2("Chipher_text.txt");
- out2 << a;
- out2 << " ";
- for (int k = 0; k < a; k++){
- yz++;
- if (ascii[k][1] == -1){
- f = ascii[k][0];
- zzbuff[yz] = PowerMod(f, e, n);
- }
- else if (ascii[k][1] != -1 && ascii[k][2] == -1){
- f = ascii[k][0];
- zzbuff[yz] = PowerMod(f, e, n);
- yz++;
- f = ascii[k][1];
- zzbuff[yz] = PowerMod(f, e, n);
- }
- else if (ascii[k][1] != -1 && ascii[k][2] != -1)
- {
- f = ascii[k][0];
- zzbuff[yz] = PowerMod(f, e, n);
- yz++;
- f = ascii[k][1];
- zzbuff[yz] = PowerMod(f, e, n);
- yz++;
- f = ascii[k][2];
- zzbuff[yz] = PowerMod(f, e, n);
- }
- }
- out2 << yz+2;
- out2 << " ";
- out2 << (char)10;
- for (int k = 0; k < yz+2; k++){
- out2 << zzbuff[k];
- out2 << " ";
- }
- out2.close();
- }
- void dec(ZZ* zzbuff, int** ascii,ZZ* unshifr, int a, ZZ d,ZZ n){
- int yz;
- yz = -1;
- ZZ f;
- ofstream out2("DecNumSymb.txt");
- for (int k = 0; k < a; k++){
- yz++;
- if (ascii[k][1] == -1){
- f = zzbuff[yz];
- if (f >= n){
- i = true;
- break;
- }
- unshifr[k] = PowerMod(f, d, n);
- }
- else if (ascii[k][1] != -1 && ascii[k][2] == -1)
- {
- f = zzbuff[yz];
- if (f >= n){
- i = true;
- break;
- }
- unshifr[k] = PowerMod(f, d, n);
- yz++;
- f = zzbuff[yz];
- if (f >= n){
- i = true;
- break;
- }
- unshifr[k] = (unshifr[k] * 10) + PowerMod(f, d, n);
- }
- else if (ascii[k][1]!=-1 && ascii[k][2]!=-1){
- f = zzbuff[yz];
- if (f >= n){
- i = true;
- break;
- }
- unshifr[k] = PowerMod(f, d, n);
- yz++;
- f = zzbuff[yz];
- if (f >= n){
- i = true;
- break;
- }
- unshifr[k] = (unshifr[k] * 10) + PowerMod(f, d, n);
- yz++;
- f = zzbuff[yz];
- if (f >= n){
- i = true;
- break;
- }
- unshifr[k] = (unshifr[k] * 10) + PowerMod(f, d, n);
- }
- out2 << unshifr[k];
- out2 << " ";
- }
- out2.close();
- }
- void CharDecAT(int* num, int a){
- ifstream in("DecNumSymb.txt");
- FILE *out;
- fopen_s(&out, "Deciphered_text.txt", "w");
- for (int k = 0; k < a; k++){
- if (!in.eof()){
- in >> num[k];
- fwrite(&num[k], sizeof(char), 1, out);
- }
- }
- in.close();
- fclose(out);
- }
- ZZ gPrime(char c, ZZ x){
- bool b;
- ZZ p, k;
- b = false;
- cout << "Enter a prime number " << c << " > 2 : ";
- cin >> p;
- if (p < 3)
- b = true;
- else if (p>2)
- for (k = 2; k < p - 1; k++){
- if (p%k == 0)
- b = true;
- }
- if (x == p)
- b = true;
- if (b == true)
- p = gPrime(c,x);
- return p;
- }
- int ret1(){
- int a;
- int y;
- ifstream in("Chipher_text.txt");
- in >> a;
- in >> x;
- in.close();
- return a;
- }
- void ret2(){
- arr = new ZZ[x];
- int kk;
- ifstream in("Chipher_text.txt");
- for (int k = 0; k < x+2; k++){
- if (k<2)
- in >> kk;
- else
- if (k>1)
- in >> arr[k-2];
- }
- in.close();
- }
- int main()
- {
- while (1){
- i = false;
- ZZ p, q, e, d, n, m, one, k;
- ZZ* zzbuff = new ZZ[];
- ZZ* unshifr = new ZZ[];
- int a, cc;
- int* num = new int[];
- int** ascii = new int*[];
- bool b;
- int l = 0;
- cout << "Enter the '1' to ENCRYPT text or '2' to DECIPHER" << endl << "Enter here the number: ";
- cin >> l;
- if (l == 1){
- a = symbcount(n);
- if (a < 1){
- cout << endl << "Error opening file or it is empty" << endl;
- }
- else{
- cout << endl << "Number of characters in the text: " << a << endl << endl;
- b = false;
- one = 1;
- k = 0;
- p = gPrime('p', k);
- q = gPrime('q', p);
- m = ((p - 1)*(q - 1));
- e = (rec(m - 1));
- n = p*q;
- cout << endl << "Public key (e,n): (" << e << "," << n << ")" << endl;
- d = modinv(e, m);
- cout << endl << "Private key (p,q,d): (" << p << "," << q << "," << d << ")" << endl;
- ascii = new int*[a];
- TxtInArr(ascii, a, n);
- zzbuff = new ZZ[y];
- enc(zzbuff, ascii, a, e, n);
- ofstream out("asciiArr.txt");
- for (int k = 0; k < a; k++){
- out << ascii[k][0];
- out << " ";
- out << ascii[k][1];
- out << " ";
- out << ascii[k][2];
- if (k!=a)
- out << " ";
- }
- out.close();
- cout << endl << endl << "Text successfully encrypted" << endl << endl;
- }
- }
- else if (l == 2){
- cout << endl << "Enter private key: " << endl;
- cout << "p: ";
- cin >> p;
- cout << endl << "q: ";
- cin >> q;
- cout << endl << "d: ";
- cin >> d;
- n = p*q;
- a = ret1();
- ret2();
- int** ascii2 = new int*[a];
- ifstream incpwin("asciiArr.txt");
- if (!incpwin.is_open()){
- cout << endl << "Can not open 'asciiArr.txt'";
- }
- else{
- for (int k = 0; k < a; k++){
- ascii2[k] = new int[3];
- incpwin >> ascii2[k][0];
- incpwin >> ascii2[k][1];
- incpwin >> ascii2[k][2];
- }
- cout << endl << endl;
- incpwin.close();
- unshifr = new ZZ[a];
- num = new int[a];
- dec(arr, ascii2, unshifr, a, d, n);
- if (i == true)
- cout << "Invalid key" << endl << endl;
- else{
- CharDecAT(num, a);
- cout << endl << endl << "Text decrypted successfully" << endl << endl;
- }
- }
- }
- }
- cout << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement