置换加密算法
1 #include "stdio.h"
2 #include "stdlib.h"
3 #define column 3
4 typedef char DataType;
5
6
7 /************************************************************************/
8 /* 置換加密算法。
9 content為需要加密的內容,index為置換的密匙 */
10 /************************************************************************/
11 char * secertIn(DataType * content, int * index)
12 {
13 int getLength(DataType *);
14 char * returnChar = NULL,**putIn = NULL;
15 int count = 0,getIndex = 0,i = 0,j = 0,length = 0,row = 0; //將內容分割后的行數
16 length = getLength(content);
17 // printf("明文內容長度: %d\n", length);
18 //計算切割內容后的行數
19 row = length / column;
20 i = length % column;
21 if(i > 0)
22 row = row + 1;
23 // printf("明文行數為:%d\n", row);
24
25 //分配二維數組存放切割后的內容
26 putIn = (char **) malloc(sizeof(char *) * row);
27 for(i = 0; i < row; i++)
28 {
29 putIn[i] = (char * )malloc(sizeof(char) * column);
30 }
31
32 returnChar = (char *) malloc(sizeof(char) * row * column + 1);
33
34 //將需要加密的內容存放進二維數組里
35 for(i = 0; i < row; i++)
36 {
37 for(j = 0; j < column; j++)
38 {
39 if(i * column + j < length)
40 putIn[i][j] = content[i * column + j];
41 else
42 putIn[i][j] = ' ';
43 }
44 }
45
46 //對內容進行加密
47 for(i = 0; i <column; i++)
48 {
49 getIndex = index[i];
50 for(j = 0; j < row; j++)
51 {
52 returnChar[count] = putIn[j][getIndex];
53 count++;
54 }
55 }
56 returnChar[count] = '\0';
57
58 return returnChar;
59 }
60
61 /************************************************************************/
62 /* 置換加密算法的解密方法
63 content為需要解密密的內容,index為置換的密匙 */
64 /************************************************************************/
65 char * secretOut(DataType * content, int * index)
66 {
67 int getLength(DataType *);
68 char * returnChar = NULL,**putIn = NULL,**buffer =NULL;
69 int count = 0,getIndex = 0,i = 0,j = 0,length = 0,row = 0; //將內容分割后的行數
70 length = getLength(content);
71 // printf("密文內容長度: %d\n", length);
72 //計算切割內容后的行數
73 row = length / column;
74 i = length % column;
75 if(i > 0)
76 row = row + 1;
77 // printf("密文文行數為:%d\n", row);
78
79 //分配二維數組存放切割后的內容
80 putIn = (char **) malloc(sizeof(char *) * row);
81 for(i = 0; i < row; i++)
82 {
83 putIn[i] = (char * )malloc(sizeof(char) * column);
84 }
85
86 returnChar = (char *) malloc(sizeof(char) * row * column + 1);
87
88 //分配二維數組空間,用來存放解密后的內容
89 buffer = (char **) malloc(sizeof(char *) * row);
90 for(i = 0; i < row; i++)
91 {
92 buffer[i] = (char * )malloc(sizeof(char) * column);
93 }
94
95 //將需要解密的內容存放進二維數組里
96 for(i = 0; i < row; i++)
97 {
98 for(j = 0; j < column; j++)
99 {
100 if(i * column + j < length)
101 putIn[i][j] = content[i * column + j];
102 else
103 putIn[i][j] = ' ';
104 }
105 }
106
107 //將加密的內容按照密匙對行重新排列
108 for(i = 0; i < row; i++)
109 {
110 getIndex = index[i];
111 buffer[getIndex] = putIn[i];
112 }
113 for(i = 0; i < column; i++)
114 {
115 for(j = 0; j < row; j++)
116 {
117 returnChar[count] = buffer[j][i];
118 count++;
119 }
120 }
121
122 returnChar[count] = '\0';
123
124 return returnChar;
125 }
126
127
128 /************************************************************************/
129 /* 計算數組長度 */
130 /************************************************************************/
131 int getLength(DataType * content)
132 {
133 int count = 0;
134 while(*content != '\0')
135 {
136 content++;
137 count++;
138 }
139 return count;
140 }
141
142 int main()
143 {
144
145 char *a = "abcdefghi"; //明文
146 char *get;
147 int index[] = {2,1,0}; //密匙,長度和列數相同
148
149 printf("明文:%s\n", a);
150 get = secertIn(a, index); //加密
151 printf("密文 :%s\n", get);
152 get = secretOut(get, index); //將上面加密后的密文進行解密
153 printf("解密后的得到明文: %s\n\n", get);
154
155 }
?
轉載于:https://www.cnblogs.com/hanyuan/archive/2012/09/20/2695813.html
總結
- 上一篇: 如何添加地图控件到Windows Pho
- 下一篇: nginx:工作原理