-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcyptertext.cpp
More file actions
70 lines (70 loc) · 950 Bytes
/
cyptertext.cpp
File metadata and controls
70 lines (70 loc) · 950 Bytes
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
#include <iostream>
using namespace std;
int mod(int u,int n);
void main()
{
int a,b,i,t;
int n=26;
char m[10000];
cout<<"please input ciphertext as m:";
gets(m);
cout<<"please input a :";
cin>>a;
cout<<"please input b:";
cin>>b;
cout<<"密文:";
for(i=0;i<strlen(m);i++)
{
cout<<m[i];
}
cout<<endl;
t=mod(a,n);
for(i=0;i<strlen(m);i++)
{
if(m[i]<='z'&&m[i]>='a')
{
m[i]=m[i]-'a';
m[i]=(t*(m[i]-b+n))%n;
m[i]=m[i]+'a';
}
else if(m[i]<='Z'&&m[i]>='A')
{
m[i]=m[i]-'A';
m[i]=(t*(m[i]-b+n))%n;
m[i]=m[i]+'A';
}
else
{
m[i]=m[i];
}
}
cout<<"解密后明文:";
for(i=0;i<strlen(m);i++)
{
cout<<m[i];
}
cout<<endl;
}
int mod(int u,int n)
{
int n1,n2,b1,b2,t,k,r,w;
n1=n,n2=u,b1=0,b2=1;
k=n1/n2;
r=n1-k*n2;
while(r!=0)
{
n1=n2;
n2=r;
t=b2;
b2=b1-k*b2;
b1=t;
k=n1/n2;
r=n1-k*n2;
}
if(n2==1)
{
w=(n+b2)%n;
return w;
}
return 0;
}