@@ -10,6 +10,7 @@ class DES {
1010public:
1111 string plaintext = " " ;
1212 string ciphertext = " " ;
13+
1314 DES (string keyword) {
1415 if (keyword.size () < 8 )
1516 {
@@ -22,10 +23,10 @@ class DES {
2223 ciphertext = " " ;
2324 char plainchar[12 ];
2425 char cipherchar[12 ] = " " ;
25- for (unsigned int i = 0 ; i < plaintext.size (); i += 8 ) {
26- strcpy (plainchar, plaintext.substr (i, 8 ).c_str ());
27- memset (cipherchar, 0 , sizeof (plainchar));
28- Run (cipherchar, plainchar, true ); // 因为是对称加密
26+ for (unsigned int i = 0 ; i < plaintext.size (); i += 8 ) { // 把明文按每8个字节分割
27+ strcpy (plainchar, plaintext.substr (i, 8 ).c_str ());
28+ memset (cipherchar, 0 , sizeof (plainchar)); // 把cipherchar每一位赋值0
29+ Run (cipherchar, plainchar, true ); // 获取对应密文
2930 ciphertext += cipherchar;
3031 }
3132 return ciphertext;
@@ -36,9 +37,9 @@ class DES {
3637 char plainchar[12 ];
3738 char cipherchar[12 ];
3839 for (unsigned int i = 0 ; i < ciphertext.size (); i += 8 ) {
39- strcpy (cipherchar, ciphertext.substr (i, 8 ).c_str ());
40+ strcpy (cipherchar, ciphertext.substr (i, 8 ).c_str ()); // 把密文按每8个字节分割
4041 memset (plainchar, 0 , sizeof (cipherchar));
41- Run (plainchar, cipherchar, false ); // 因为是对称加密
42+ Run (plainchar, cipherchar, false ); // 获取对应明文
4243 plaintext += plainchar;
4344 }
4445 return plaintext;
@@ -51,14 +52,14 @@ class DES {
5152 {
5253 Key[i] = keyword[i];
5354 }
54- static bool K[64 ], *KL = &K[0 ], *KR = &K[28 ];
55- charToByte (K, Key, 64 ); // 从char转换到二进制数组
56- Transform (K, K, PC_1Table, 56 ); // 从64位化为56位
55+ static bool K[64 ], *KL = &K[0 ], *KR = &K[28 ]; // kl,kr分别为左侧和右侧子密码段
56+ charToByte (K, Key, 64 ); // 从char转换到二进制数组
57+ Transform (K, K, PC_1Table, 56 ); // 从64位化为56位
5758 for (int i = 0 ; i < 16 ; i++)
5859 {
59- shiftLeft (KL, 28 , shiftTable[i]);
60- shiftLeft (KR, 28 , shiftTable[i]);
61- Transform (SubKey[i], K, PC_2Table, 48 );
60+ shiftLeft (KL, 28 , shiftTable[i]); // 左移
61+ shiftLeft (KR, 28 , shiftTable[i]);
62+ Transform (SubKey[i], K, PC_2Table, 48 ); // 据PC2表做转换
6263 }
6364 }
6465
0 commit comments