In one execution of the DES algorithm, 16 rounds of iterative encryption will be performed;
16 round keys k are obtained from the original 56-bit key, and each round key ki is 48 bits; the
round key is also called a sub-key;
Let’s take a look at the sub-key generation process;
The general process is like this; look at the picture below,
This picture comes from 34. "Cryptozoology". Explanation of the principle of DES algorithm_Subkey generation and summary_bilibili_bilibili
For a 64-bit key, 8 check bits are removed, leaving 56 bits;
Replace according to the PC-1 table and shuffle the order; then divide it into two 28-bits; one 28-bit is C0, and the other 28-bit is D0.
Then we need to perform a circular left shift. The circular left shift needs to be shifted by 1 or 2 bits. It can be judged according to which round it is. You can check the circular left shift lookup table in the figure above;
After the shift, Ci and Di are obtained, i is the number of rounds;
Combine Ci and Di to get 56 bits;
The obtained 56 bits are replaced according to the PC-2 table, 8 bits will be removed during the replacement, and the 48-bit subkey ki of this round is obtained;
The PC-1 table is 56 bits and the PC-2 table is 48 bits;
Or look at the picture below for more conciseness;
This picture comes from, DES encryption and decryption algorithm (simple, easy to understand, super detailed)_des encryption algorithm_sunny-ll's blog-CSDN blog
The process is: 64-bit key input, PC1 replacement, divided into two 28-bit keys, circular left shift to obtain a 56-bit key, PC2 replacement, and 48-bit subkey ki;
I took a look at the DES encryption algorithm code; I haven’t fully understood it yet;
The bits in the algorithm description should be binary bits;
The operation in the code does not use bit operations in C language; it should be a binary string such as 11001010, which is used as a string. Each string is taken and operated according to the algorithm; after completion, the binary string is converted into an integer. The ASCII code of the character can be obtained from the integer type; it should be like this;
Its PC1 replacement code is like this,
int pc1Table[56] =
{
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
};
string miyao, miyaoBinary, pc1MiyaoBinary;
//Get 56bit from the 64bit key according to the PC-1 box
for (i = 0; i < 56; i++)
{ pc1MiyaoBinary += miyaoBinary[pc1Table[i] - 1]; }
Among them miyaoBinary and pc1MiyaoBinary are string types;
There are also two functions in its code that implement conversion between binary strings and integers.
int binaryToInt(string s), input a binary string and return an integer;
string intToBinary(int i), input an integer and return a binary string;
Let’s take a look at these two functions separately in MFC; I changed their string type to MFC’s CString;
int binaryToInt(CString );
CString intToBinary(int );
void CInbyView::OnDraw(CDC* pDC)
{
CInbyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString str1;
CString b1 = "0101";
int b2 = binaryToInt(b1);
str1.Format("0101的整数:%d", b2);
pDC->TextOut(50, 50, str1);
CString b3 = "1100101";
int b4 = binaryToInt(b3);
str1.Format("1100101的整数:%d", b4);
pDC->TextOut(50, 80, str1);
str1 = intToBinary(12);
pDC->TextOut(50, 110, "12的二进制:" + str1);
CString str3 = intToBinary(14);
pDC->TextOut(50, 140, "14的二进制:" + str3);
CString str4 = intToBinary(37);
pDC->TextOut(50, 170, "37的二进制:" + str4);
}
//二进制转整型
int binaryToInt(CString s)
{
int i, result = 0, p = 1;
for (i = s.GetLength() - 1; i >= 0; i--)
{
result += ((s[i] - '0') * p); //数字字符转成字符
p *= 2;
}
return result;
}
//整型转二进制
CString intToBinary(int i)
{
int k = 0;
CString result;
while (k < 4) //此处,处理进入S盒后取出的数据转为2进制,此处最多用4bit
{
if (i)
{
result += ((i % 2) + '0');
i /= 2;
}
else result += '0';
k++;
}
result.MakeReverse();
return result;
}
The output is as follows;
Among them, intToBinary() uses up to 4 bits according to its instructions; the binary number of 37 is as follows. When 37 is entered, only the lower 4 bits are returned;
There is time to continue;