生活随笔
收集整理的這篇文章主要介紹了
35. 通过实现一个序列加密的功能,熟悉对二维空间与一维空间的操作。
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
按行讀的話,肯定可以讀出數(shù)據(jù),如果按列來讀的話,則會(huì)出再亂碼的現(xiàn)像。正
是這種現(xiàn)像可作為一種加密手段,稱為序列加密。
hello everyone? 原始序列
可以看成
hello_
everyo
ne****
按列提取
hen
eve
le*
lr*
oy*
_o*
henevele*lr*oy*_o*? ?此時(shí)就為加密后的序列
現(xiàn)在實(shí)現(xiàn)加密與解密的功能:
1 #include <stdio.h>
2 #include <
string.h>
3 #include <stdlib.h>
4 char* encode(
char* str,
int col)
5 {
6 int i,j;
7 int strLen =
strlen(str);
8 int bufLen = strLen + col - strLen%
col;
9
10 char* strBuf =
malloc(bufLen+
1);
//線性一維空間,存儲(chǔ)完原始數(shù)據(jù)后之后當(dāng)成二維空間使用
11 strcpy(strBuf,str);
12 // char* pbuf = buf;
13 for(i = strLen;i < bufLen;i++
)
14 {
15 *(strBuf+i) =
'*';
//strBuf[i] = '*';
16 }
17 *(strBuf+i) =
'\0';
18
19
20 //將原始字符串行列置反存儲(chǔ)到新空間
21 char* newStrBuf =
malloc(bufLen+
1);
22 char* pNewStrBuf =
newStrBuf;
23 char (*pStrBuf)[col] = strBuf;
//二維方式訪問strBuf,
24 for(j =
0;j < col;j++)
//按順序訪問原始數(shù)據(jù)中每列上的所有行
25 {
26 for(i =
0;i<bufLen/col;i++
)
27 {
28 *pNewStrBuf++ =
pStrBuf[i][j];
29 }
30 }
31 *pNewStrBuf =
'\0';
32
33
34 free(strBuf);
35 return newStrBuf;
36 }
37
38 char* encode2(
char* str,
int col)
39 {
40 int i,j;
41 int strLen =
strlen(str);
42 int bufLen = strLen + col - strLen%
col;
43
44 char* strBuf =
malloc(bufLen);
//線性一維空間,存儲(chǔ)完原始數(shù)據(jù)后之后當(dāng)成二維空間使用
45 strcpy(strBuf,str);
46 // char* pbuf = buf;
47 for(i = strLen;i < bufLen;i++
)
48 {
49 *(strBuf+i) =
'*';
//strBuf[i] = '*';
50 }
51 // *(strBuf+i) = '\0';
52
53
54 //將原始字符串行列置反存儲(chǔ)到新空間
55 char* newStrBuf =
malloc(bufLen+
1);
56 char* pNewStrBuf =
newStrBuf;
57 char (*pStrBuf)[col] = strBuf;
//二維方式訪問strBuf,
58 for(j =
0;j < col;j++)
//按順序訪問原始數(shù)據(jù)中每列上的所有行
59 {
60 for(i =
0;i<bufLen/col;i++
)
61 {
62 *pNewStrBuf++ =
pStrBuf[i][j];
63 }
64 }
65 *pNewStrBuf =
'\0';
66
67
68 free(strBuf);
69 return newStrBuf;
70 }
71
72
73 char* decode(
char *str,
int row)
74 {
75 int bufLen =
strlen(str);
76 char* buf =
malloc(bufLen+
1);
77
78
79 int i,j;
80 int col = (bufLen)/row;
//計(jì)算出新的原始數(shù)據(jù)的列
81
82 char (*pStr)[col] =
str;
83 char *pBuf =
buf;
84 for(j =
0;j < col ;j++
)
85 {
86 for(i =
0;i < row;i++
)
87 {
88 *pBuf++ =
pStr[i][j];
89 }
90 }
91 *pBuf =
'\0';
//這句可有可無
92
93 //去*操作
94 while(*(--pBuf) ==
'*');
95 *(++pBuf) =
'\0';
96
97
98
99 return buf;
100
101 }
102
103 int main(
void)
104 {
105 // int code = 6;
106 char str[] =
"hello everyone";
107
108 char* pe = encode(str,
6);
109 printf(
"%s\n",pe);
110 char* pd = decode(pe,
6);
111 printf(
"%s\n",pd);
112
113 free(pe);
114 free(pd);
115
116
117 return 0;
118 }
?
| ?h | ?e | ?l | ?l | ?o | ? | ?e | ?v | ?e | ?r | ?y | ?o | ?n | ?e | ?0 | ? | ? | ? |
| ?0? | ?1? | ?2? | ?3? | ?4? | ?5? | ?6? | ?7? | ?8? | ?9? | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| ?h? | ?e? | ?l? | ?l? | ?o? | ? | e | ?v | ?e | ?r | ?y | ?o | ?n | ?e | ?0 | ?*? | ?* | ?*? | ?0 |
| ?0 | ?1 | ?2? | ?3? | ?4 | ?5? | ?6? | ?7? | ?8? | ?9? | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
?
| ?h? | ?e? | ?l? | ?l? | ?o? | ? ? |
| ?e | ?v | ?e? | ?r? | ?y | ?o |
| ?n | ?e | ?* | ?* | ?* | ?* |
? 0? ? ?
轉(zhuǎn)載于:https://www.cnblogs.com/ZhuLuoJiGongYuan/p/9494860.html
總結(jié)
以上是生活随笔為你收集整理的35. 通过实现一个序列加密的功能,熟悉对二维空间与一维空间的操作。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。