-
Notifications
You must be signed in to change notification settings - Fork 0
/
密码串.cpp
76 lines (71 loc) · 1.4 KB
/
密码串.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
#include<stdio.h>
#include<string.h>
#define MaxSize 100
typedef struct
{
char data[MaxSize];
int length;
}HString;
void strAssign(HString &T,char *chars)//生成一个其值等于串常量chars的串T
{
int i=0;
for(;chars[i]!='\0';i++)
{
T.data[i]=chars[i];
}
T.length=i;
}
void strDisplay(HString s)//显示串
{
int i;
if(s.length>0)
{
for(i=0;i<s.length;i++)
printf("%c",s.data[i]);
printf("\n");
}
}
HString strEncrypt(HString A,HString B,HString T)//输入字母映射表和要加密的串
{
int i=0,j;//j为A串中的下标
HString str;//创建新串,为T加密后的串
while(i<T.length)
{
for(j=0;T.data[i]!=A.data[j];j++);
if(j>=A.length)printf("出错了!");
else str.data[i]=B.data[j];
i++;
}
str.length=T.length;
return str;
}
HString strUnencrypt(HString A,HString B,HString T)//输入字母映射表和要解密的串
{
int i=0,j;
HString str;
while(i<T.length)
{
for(j=0;T.data[i]!=B.data[j];j++);
if(j>=A.length)printf("出错了!");
else str.data[i]=A.data[j];
i++;
}
str.length=T.length;
return str;
}
int main()
{
HString A,B,S2,S1,S3;
char str[MaxSize];
strAssign(A,"abcdefghijklmnopqrstuvwxyz");
strAssign(B,"ngzqtcobmuhelkpdawxfyivrsj");
printf("请输入要加密的串>>");
gets(str);
strAssign(S1,str);
S2=strEncrypt(A,B,S1);
printf("加密后>>");
strDisplay(S2);
printf("解密后>>");
S3=strUnencrypt(A,B,S2);
strDisplay(S3);
}