本文內容:
1. 對稱加密
2. 數據加密標準
3. 用c++程序實現DES加密和解密
4. 實驗效果
一、對稱加密
對稱加密也稱爲常規加密、私鑰或單鑰加密。
一個對稱加密由5部分組成:
- 明文(plaintext):這是原始信息或數據,作爲算法的輸入。
- 加密算法(encryption algorithm):加密算法對明文進行各種替換和轉換。
- 密鑰(secret key):密鑰也是算法的輸入。算法進行的具體替換和轉換取決於密鑰。
- 密文(ciphertext):這是產生的已被打亂的消息輸出。
- 解密算法(decryption algorithm):本質上是加密算法的反向執行。它使用密文和同一密鑰產生產生原始明文。
下圖解釋了這一過程:
二、數據加密標準
數據加密標準(Data Encryption Standard, DES)於1977年被美國國家標準局(National Bureau of Standard, NBS),即現在國家標準和技術協會(National Institute of Standards and Technology,NIST)採納爲聯邦46(FIPS PUB 46)。這個算法本身指的是數據加密算法(Data Encryption Algorithm)。DES採用了64位的分組長度和56位的密鑰長度。它將64位的經過一系列的變換得到64位的輸出,解密則使用了相同的步驟和相同的密鑰。
三、用c++程序實現DES加密和解密
下圖是DES加密算法的整體流程圖:
64位輸入明文先進行初始置換IP,然後進行16輪相同的函數作用,最後在進行一個逆初始置換輸出64位的密文。上圖的右半部分是密鑰的產生和使用過程,首先64位密鑰壓縮置換後變爲56位,然後進行循環左移和置換選擇得到子密鑰和每輪的輸入。
初始置換(IP):
// 初始置換表
int IP[] = {58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
// 逆置換表
int IP_1[] = {40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
每輪的具體操作流程:
每輪的變換過程可以記爲下面的公式:
明文輸入的64位,被分成兩部分L和R,R又作爲下一輪迭代的L,R經過F函數的作用輸出32位的數據,再與本輪的L異或產生下一輪的R。
下圖解釋了S盒在F函數中的使用方法:
一共有8個S盒,每個S盒都輸入6位,輸出4位。盒Si輸入的第一位和最後一位組成一個二進制數,用來選擇S盒4行代替值中的一行,中間4位組成的二進制數用來選擇16列中的一列。選中的是十進制數轉換成二進制數後得到輸出的4位二進制數。S盒的定義如下:
// S盒,每個S盒是4x16的置換表,6位 -> 4位
int S_BOX[8][4][16] = {
{
{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}
},
{
{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}
},
{
{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}
},
{
{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}
},
{
{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}
},
{
{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}
},
{
{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}
},
{
{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
}
};
F函數中用到的擴展置換表E和置換表P定義如下:
// 擴展置換表,將 32位 擴展至 48位
int E[] = {32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};
// P置換,32位 -> 32位
int P[] = {16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};
注意:
代碼中位的操作使用c++中的模板類bitset,
對於bitset<64> N(5),N[0]=1,N[1]=0,N[2]=1,N[3]=0。
3.1 函數F的代碼實現
// 實現 F 函數
bitset<32> f_function(const bitset<32> &R, const bitset<48> &K) {
bitset<48> expandedR;
// 1.擴展置換
for(int i = 0; i < 48; i++) {
expandedR[47 - i] = R[32 - E[i]];
}
// 2.expandedR 與 K 執行XOR運算
expandedR = expandedR ^ K;
// 3. S盒置換
bitset<32> output;
for(int i = 0, j = 0; i < 48; i += 6, j += 4) {
int row = expandedR[47 - i] * 2 + expandedR[47 - i - 5];
int column = expandedR[47 - i - 1] * 8 + expandedR[47 - i - 2] * 4 + expandedR[47 - i - 3] * 2 + expandedR[47 - i - 4];
bitset<4> bitTemp(S_BOX[i / 6][row][column]);
output[31 - j] = bitTemp[3];
output[31 - j - 1] = bitTemp[2];
output[31 - j - 2] = bitTemp[1];
output[31 - j - 3] = bitTemp[0];
}
// 4. P置換
bitset<32> tmp = output;
for(int i = 0; i < 32; i++) {
output[31 - i] = tmp[32 - P[i]];
}
return output;
}
3.2 密鑰的操作
因爲16個子密鑰在加密和解密過程中都要使用,故編寫一個函數getSubKey用來生成16個子密碼。
密鑰生成過程中用到的置換表:
// 密鑰置換表,將64位密鑰變成56位
int PC_1[] = {57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
// 壓縮置換,將56位密鑰壓縮成48位子密鑰
int PC_2[] = {14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
// 每輪左移的位數
int shiftBitCounts[] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
生成16個子密碼的函數:
// 用函數實現循環左移
bitset<28> leftShift(bitset<28> bits, int count) {
bitset<28> bittemp;
for(int i = 0; i < 28; i++)
bittemp[i] = bits[27 - i];
bittemp = (bittemp >> count) | (bittemp << (28 - count));
for(int i = 0; i < 28; i++)
bits[27 - i] = bittemp[i];
return bits;
}
// 生成子密鑰
void getSubKey(const bitset<64> &key) {
// 1. 獲得C和D
bitset<56> initKeyReplace;
for(int i = 0; i < 56; i++) {
initKeyReplace[55 - i] = key[64 - PC_1[i]];
}
bitset<28> C;
bitset<28> D;
for(int i = 0; i < 56; i++) {
if(i < 28)
C[27 - i] = initKeyReplace[55 - i];
else
D[55 - i] = initKeyReplace[55 - i];
}
// 16 rounds
for(int iround = 0; iround < 16; iround++) {
C = leftShift(C, shiftBitCounts[iround]);
D = leftShift(D, shiftBitCounts[iround]);
bitset<56> con;
for(int i = 0; i < 56; i++) {
if(i < 28)
con[55 - i] = C[27 - i];
else
con[55 - i] = D[55 - i];
}
bitset<48> res;
for(int i = 0; i < 48; i++) {
res[47 - i] = con[56 - PC_2[i]];
}
subKey[iround] = res;
}
}
3.3 加密和解密過程
// DES加密
bitset<64> encrypt(const bitset<64> &text) {
// 1. 初始置換IP
bitset<64> initReplace;
for(int i = 0; i < 64; i++) {
initReplace[63 - i] = text[64 - IP[i]];
}
// 2. 16輪
bitset<32> currentL;
bitset<32> currentR;
bitset<32> nextL;
// 2.1 獲得L和R
for(int i = 0; i < 64; i++) {
if(i < 32)
currentL[31 - i] = initReplace[63 - i];
else
currentR[63 - i] = initReplace[63 - i];
}
// 2.2 16 round
for(int round = 0; round < 16; round++) {
nextL = currentR;
currentR = f_function(currentR, subKey[round]) ^ currentL;
currentL = nextL;
}
// 3. 32位互換
bitset<64> exchange;
for(int i = 0; i < 64; i++) {
if(i < 32)
exchange[63 - i] = currentR[31 - i];
else
exchange[63 - i] = currentL[63 - i];
}
// 4. 結尾IP_1置換
bitset<64> result;
for(int i = 0; i < 64; i++) {
result[63 - i] = exchange[64 - IP_1[i]];
}
return result;
}
解密過程和加密過程一樣,只是使用子密鑰的順序是相反的。
四、實驗效果
int main(int argc, char *argv[])
{
string s_key = "qiaoting";
bitset<64> key = charToBitset(s_key.c_str());
getSubKey(key);
fstream fileIn, fileOut;
fileIn.open("plain_in.txt", ios::binary | ios::in);
fileOut.open("cipher.txt", ios::binary | ios::out);
bitset<64> plain;
while(fileIn.read((char *)&plain, sizeof(plain)))
{
bitset<64> cipher = encrypt(plain);
fileOut.write((char*)&cipher, sizeof(cipher));
plain.reset();
}
fileIn.close();
fileOut.close();
fileIn.open("cipher.txt", ios::binary | ios::in) ;
fileOut.open("plain_out.txt", ios::binary | ios::out);
while(fileIn.read((char*)&plain, sizeof(plain)))
{
bitset<64> temp = decrypt(plain);
fileOut.write((char*)&temp, sizeof(temp));
plain.reset();
}
fileIn.close();
fileOut.close();
return 0;
}
其中subKey是一個全局變量(當然也可以不這麼做)。
bitset<48> subKey[16];
函數charToBitset是用來將C類型的字符串轉換成位(bitset)。
bitset<64> charToBitset(const char *s)
{
bitset<64> res;
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
res[i * 8 + j] = ((s[i] >> j) & 0x01);
return res;
}
主函數中實現的是將plain_in.txt這個文件加密位cipher.txt,然後以cipher.txt作爲輸入文件,進行解密,解密後的文件爲plain_out.txt。
效果:
五、代碼
本文爲對稱加密算法DES的學習筆記,參考《密碼學與網絡安全》第五版