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>
#include <string.h>
using
namespace
std;
int
main()
{
void
encrytion_decrypt(
char
input[],
int
len);
char
input[10];
cout<<
"Please input password:"
;
cin>>input;
int
len;
len=
strlen
(input);
cout<<
"\n"
<<
"password is:"
<<input<<endl;
encrytion_decrypt(input,len);
return
0;
}
void
encrytion_decrypt(
char
input[],
int
len)
//加密_解密函数
{
// 仿射加密公式 : f(x)=3x+2;
int
i;
char
string[10];
for
(i=0;i<len;i++)
{
int
a;
a=(input[i]-97)*3+2;
//加密
if
(a>25)
//对照为0-25 mod为26
{
do
//保证不超出域
{
a=a-26;
}
while
(a>25);
a=a+97;
//加密后得到的ASC码;
string[i]=(
char
)a;
}
else
{
a=a+97;
string[i]=(
char
)a;
}
}
cout<<
"encrytion password is:"
;
for
(i=0;i<len;i++)
{
printf
(
"%c"
,string[i]);
}
cout<<endl;
char
res[10];
for
(i=0;i<len;i++)
//解密过程
{
int
b;
b=(string[i]-97)-2;
/* if(b==0)
{
res[i]=(char)(b+97);
continue;
} */
while
(b%3!=0)
{
b=b+26;
}
b=b/3;
res[i]=(
char
)(b+97);
}
cout<<
"keyword is:"
;
for
(i=0;i<len;i++)
printf
(
"%c"
,res[i]);
cout<<endl;
}
|
本文转自 nw01f 51CTO博客,原文链接:http://blog.51cto.com/dearch/1748067,如需转载请自行联系原作者