-
Notifications
You must be signed in to change notification settings - Fork 1
/
test4.cpp
140 lines (119 loc) · 2.87 KB
/
test4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Created by 李文博 on 2018-12-23.
//
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cstring>
#include "common.h"
using namespace std;
long long p,q,n,deltan,d,e;
char plaintext[2];
int plt,cit;
char code2char[27]={' ','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'};
int char2code(char c){
switch (c){
case ' ':return 0;
case 'A':return 1;
case 'B':return 2;
case 'C':return 3;
case 'D':return 4;
case 'E':return 5;
case 'F':return 6;
case 'G':return 7;
case 'H':return 8;
case 'I':return 9;
case 'J':return 10;
case 'K':return 11;
case 'L':return 12;
case 'M':return 13;
case 'N':return 14;
case 'O':return 15;
case 'P':return 16;
case 'Q':return 17;
case 'R':return 18;
case 'S':return 19;
case 'T':return 20;
case 'U':return 21;
case 'V':return 22;
case 'W':return 23;
case 'X':return 24;
case 'Y':return 25;
case 'Z':return 26;
}
}
void txt2plt(){
plt = char2code(plaintext[0]);
plt *= 100;
plt += char2code(plaintext[1]);
}
void plt2txt(){
plaintext[1] = code2char[plt%100];
plt /= 100;
plaintext[0] = code2char[plt%100];
}
void set_rsa(){
n = p * q;
cout<<"n: "<<n<<endl;
deltan = (p - 1) * (q - 1);
cout<<"(l)n: "<<deltan<<endl;
for(long long i = 2;;i++){
if(is_coprime(i,deltan)){
e = i;
break;
}
}
cout<<"e: "<<e<<endl;
for(d=1;;d++){
if(quick_mod(d*e, 1, deltan) == 1){
break;
}
}
cout<<"d: "<<d<<endl;
}
void encrypt(){
cit = quick_mod(plt , e, n);
}
void decrypt(){
plt = quick_mod(cit, d, n);
}
int main(){
cout<<"7^563mod561: "<<quick_mod(7,563,561)<<endl;
cout<<"请输入p 、q"<<endl;
cin>>p>>q;
set_rsa();
ifstream file;
file.open("data/4/text.txt",ios::in);
ofstream ofile;
ofile.open("data/4/encrypt.txt",ios::out);
while(!file.eof()) {
file.read((char *) &plaintext, sizeof(plaintext));
if(file.eof())break;
txt2plt();
encrypt();
ofile.write((char *) &cit, sizeof(cit));
}
ofile.close();
file.close();
cout<<"加密成功!"<<endl;
//解密
file.open("data/4/encrypt.txt",ios::in);
ofile.open("data/4/answer.txt",ios::out);
while(!file.eof()) {
file.read((char *) &cit, sizeof(cit));
if(file.eof())break;
decrypt();
plt2txt();
ofile.write((char *) &plaintext, sizeof(plaintext));
}
ofile.close();
file.close();
cout<<"解密成功!"<<endl;
return 0;
}