当前位置:网站首页>Des File Encryptor based on MFC framework
Des File Encryptor based on MFC framework
2022-06-12 13:57:00 【HyperCall】
project GitHub Address :https://github.com/joliph/DES_File
TIPS:
1. choice project attribute , Character set where to choose to use multi byte character set , Do not use UNICODE Character set
2. The project in Windows 10 + VS2017 + X64 +Release The next compilation test passed , Not X64 Version part API The interface may be different
3. There must be BUG, I will repair it when I have a chance , Not yet
4.DES There are some changes in the algorithm interface and the algorithm part of the previous article
5.CBC The pattern is an external implementation , namely DES The algorithm part can only do ECB Pattern , It's kind of hard , But it works
6. The code style of the message response function is a bit messy …
7. Encrypted and decrypted files MD5 Values may change but do not affect usage , Because I just added a few more 0 Make it 8 Byte alignment
8.CBC The initial secret key of the mode is 64 Bit 1
The interface is a bit ugly …

Algorithm testing :
Secret key :“security”
Plaintext :“computercomputer”
ECB Encrypted password :“3B6C72B2710EB5133B6C72B2710EB513”
CBC Encrypted password :“3B6C72B2710EB51339283610A256F03E”
Efficiency analysis of encryption algorithm :
test 1:23 Megabyte file :ECB:4.484 second CBC:4.971 second
test 2:230 Megabyte file :ECB:22.271 second CBC:24.718 second
ECB Efficiency is higher than CBC, because CBC There are additional XOR operations , And you can multithread later ECB Mode encryption , because ECB Can be multi-threaded synchronous operation , and CBC no way
Function and brief description of key functions
1. DES_Key Class constructor design
Receiving parameters :char Char_Key[8]
effect : Generate 16 Round keys
step :
1. call CharToBit take 8 A key of bytes is converted into 64 position
2. call Transform Make a displacement selection
3. Group left and right
4.16 The wheel cycles to the left
5. Each round of replacement selection is performed to generate 16 Round keys
DES_Key::DES_Key(char Char_Key[8]) {
CharToBit(Bit_Key, Char_Key);
Transform(Bit_Key, Bit_Key, PC1_Table, 56);
bool *C0 = &Bit_Key[0];
bool *D0 = &Bit_Key[28];
for (int i = 0; i < 16; i++) {
RotateL(C0, LOOP_Table[i]);
RotateL(D0, LOOP_Table[i]);
Transform(Round_Key[i], Bit_Key, PC2_Table, 48);
}
}
********************************************************************************
2. Xor Matrix XOR function design :
Receiving parameters : Bit matrix A , Bit matrix B, Number of matrix bits
effect : take A Matrix and B Bitwise exclusive or of a matrix , Results update A matrix
void Xor(bool *InputA, const bool *InputB, int len) {
for (int i = 0; i < len; i++) {
InputA[i] = InputA[i] ^ InputB[i];
}
}
********************************************************************************
3. CharToBit Character transposition matrix function design
Receiving parameters : Bit matrix pointer , String pointer
effect : take 8 Bit string converted to 64 Bit matrix
The key : Bit test operation
void CharToBit(bool *Output, const char *Input) {
for (int i = 0; i<64; i++) {
Output[i] = (Input[i / 8] << (i % 8)) & 0x80;
}
}
********************************************************************************
4. Transform Permutation selection function design
Accept parameters : Output bit matrix pointer , Input bit matrix pointer , Replacement table , Number of permutations
effect :DES Substitution selection in encryption
void Transform(bool *Output, bool *Input, const int *Table, int Tablelen) {
bool temp[256];
for (int i = 0; i < Tablelen; i++) {
temp[i] = Input[Table[i] - 1];
}
memcpy(Output, temp, Tablelen);
}
********************************************************************************
5. RotateL Left shift function design
Accept parameters : Input bit matrix , Shift left length
effect :DES Left shift operation in round key generation , Move the bit matrix to the left circularly len length
void RotateL(bool *Input, int len) {
bool temp[28];
memcpy(temp, &Input[len], 28 - len);
memcpy(&temp[28 - len], Input, len);
memcpy(Input, temp, 28);
}
********************************************************************************
6. S_func S Box substitution function design
Accept parameters : Output bit matrix , Input bit matrix
effect :DES Security in encryption S Function implementation of box replacement , take 48 The bit matrix is compressed into 32 position
void S_func(bool *Output, bool *Input) {
for (int i = 0; i < 8; i++) {
int row = 2 * Input[6 * i] + 1 * Input[6 * i + 5];
int col = 8 * Input[6 * i + 1] + 4 * Input[6 * i + 2] + 2 * Input[6 * i + 3] + 1 * Input[6 * i + 4];
int s_result = S_Box[i][row][col];
for (int j = 0; j < 4; j++) {
Output[4 * i + j] = (s_result << j) & 0x8;
}
}
}
********************************************************************************
7. F_func F Function design
Accept parameters : Input R0 matrix , Enter the key bit matrix
effect :DES Security in encryption F Function implementation , The key and ciphertext are mixed for encryption
step :
1. call Transform Replacement selection
2. call Xor Function to XOR matrix
3. call S The box function performs S Box replacement
4. call Transform Replacement selection
void F_func(bool *R, bool *Key) {
bool Ex_R[48];
Transform(Ex_R, R, E_Table, 48);
Xor(Ex_R, Key, 48);
S_func(R, Ex_R);
Transform(R, R, P_Table, 32);
}
********************************************************************************
8. Des_Run DES Encryption function design
Accept parameters : Input plaintext bit matrix , Enter the key instance , Enter the encrypted ciphertext storage pointer
effect : Enter clear text for DES encryption , The result is written into the ciphertext storage matrix
step :
1. call Transform Replacement selection
2. Left and right plaintext separation
3. 16 Round encryption cycle
1. F Function mixed key encryption
2. Matrix XOR
3. S Function substitution compression
4. call Transform Replacement selection
void Des_Run(bool *Bit_Mingwen, DES_Key DES_Key,char *pNewFileBuf) {
Transform(Bit_Mingwen, Bit_Mingwen, IP_Table, 64);
bool *L0 = &Bit_Mingwen[0];
bool *R0 = &Bit_Mingwen[32];
for (int i = 0; i < 16; i++) {
bool temp[32];
memcpy(temp, R0, 32);
F_func(R0, DES_Key.Round_Key[i]);
Xor(R0, L0, 32);
memcpy(L0, temp, 32);
}
bool temp[32];
memcpy(temp, R0, 32);
memcpy(R0, L0, 32);
memcpy(L0, temp, 32);
Transform(Bit_Mingwen, Bit_Mingwen, IPR_Table, 64);
ShowResult(Bit_Mingwen, pNewFileBuf);
}
********************************************************************************
9. Re_Des_Run DES Decryption function design
Accept parameters : Input ciphertext bit matrix , Enter the key instance , Enter the encrypted plaintext storage pointer
effect : Enter clear text for DES Decrypt , The result is written into the plaintext storage matrix
step :
5. call Transform Replacement selection
6. Left and right plaintext separation
7. 16 Round decryption cycle
4. F Function mixed key decryption
5. Matrix XOR
6. S Function substitution compression
8. call Transform Replacement selection
void Re_Des_Run(bool *Bit_Mingwen, DES_Key DES_Key, char *pNewFileBuf) {
Transform(Bit_Mingwen, Bit_Mingwen, IP_Table, 64);
bool *L0 = &Bit_Mingwen[0];
bool *R0 = &Bit_Mingwen[32];
for (int i = 0; i < 16; i++) {
bool temp[32];
memcpy(temp, R0, 32);
F_func(R0, DES_Key.Round_Key[15-i]);
Xor(R0, L0, 32);
memcpy(L0, temp, 32);
}
bool temp[32];
memcpy(temp, R0, 32);
memcpy(R0, L0, 32);
memcpy(L0, temp, 32);
Transform(Bit_Mingwen, Bit_Mingwen, IPR_Table, 64);
ShowResult(Bit_Mingwen, pNewFileBuf);
}
********************************************************************************
10. ShowResult The result shows that the function design
Accept parameters : Input 64 Bit ciphertext bit matrix , Enter the encrypted plaintext storage pointer
effect : take 64 The bit matrix is converted to 16 Hexadecimal is stored in the memory pointed to by the pointer
void ShowResult(const bool Input[64],char *pNewFileBuf) {
int Character[8];
for (int i = 0; i<8; i++) {
pNewFileBuf[i] = char(128 * Input[0 + i * 8] + 64 * Input[1 + i * 8] + 32 * Input[2 + i * 8] + 16 * Input[3 + i * 8] +8 * Input[4 + i * 8] + 4 * Input[5 + i * 8] + 2 * Input[6 + i * 8] + 1 * Input[7 + i * 8]);
}
}
********************************************************************************
11. OnBnClickedChose File select message response button
effect : Response file selection button , Read files into memory , Make the file press 8 Bit alignment
void CDESFileDlg::OnBnClickedChose(){
CFileDialog dlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
(LPCTSTR)_TEXT("All Files (*.*)|*.*||"), NULL);
if (dlg.DoModal() == IDOK)
FilePath = dlg.GetPathName();
else
return;
UpdateData(FALSE);
File_Handle = CreateFile(FilePath,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (File_Handle == INVALID_HANDLE_VALUE) {
MessageBoxA(" Failed to load file !","Error", MB_OK);
File_Handle = NULL;
return;
}
FileSize = GetFileSize(File_Handle, NULL);
int need_add = 0;
if(FileSize % 8 != 0){
need_add = 8 - (FileSize % 8);
}
pFileBuf = new char[FileSize+ need_add];
DWORD ReadSize = 0;
ReadFile(File_Handle, pFileBuf, FileSize, &ReadSize, NULL);
memset(pFileBuf + FileSize, 0, need_add * sizeof(char));
FileSize += need_add;
CloseHandle(File_Handle);
File_Handle = NULL;
}
********************************************************************************
12. OnBnClickedEncrypt Encrypted message response button
effect : Respond to the encryption button , call DES The encryption function encrypts the file , Write new file to disk
Branch :ECB encryption +CBC encryption
void CDESFileDlg::OnBnClickedEncrypt(){
UpdateData(TRUE);
if (!Check()) {
return;
}
char *Char_Key= (char*)CString_Key.GetBuffer(0);
DES_Key Input_Key(Char_Key);
int count = FileSize / 8;
pNewFileBuf = new char[FileSize];
for(int i=0;i<count;i++){
bool Bit_Mingwen[64];
CharToBit(Bit_Mingwen, pFileBuf);
if (Ls_CBC && i != 0) {
CBC_Xor(Bit_Mingwen,pFileBuf, pNewFileBuf-8);
}
Des_Run(Bit_Mingwen, Input_Key, pNewFileBuf);
pFileBuf += 8;
pNewFileBuf += 8;
}
pNewFileBuf -= count * 8;
int n=FilePath.ReverseFind('.');
CString NewFilePath = FilePath.Left(n+1);
NewFilePath+="encrypted";
HANDLE NewFile_Handle=CreateFile(NewFilePath,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD WriteSize = 0;
WriteFile(NewFile_Handle, pNewFileBuf, FileSize, &WriteSize, NULL);
CloseHandle(NewFile_Handle);
NewFile_Handle = NULL;
MessageBoxA(" Encryption complete ", "Nice", NULL);
}
********************************************************************************
13. OnBnClickedDecrypt Decrypt message response button
effect : Respond to the decryption button , call DES Decryption function decrypts the file , Write new file to disk
Branch :ECB Decrypt +CBC Decrypt
void CDESFileDlg::OnBnClickedDecrypt(){
UpdateData(TRUE);
if (!Check()) {
return;
}
char *Char_Key = (char*)CString_Key.GetBuffer(0);
DES_Key Input_Key(Char_Key);
int count = FileSize / 8;
pNewFileBuf = new char[FileSize];
for (int i = 0; i<count; i++) {
bool Bit_Mingwen[64];
CharToBit(Bit_Mingwen, pFileBuf);
Re_Des_Run(Bit_Mingwen, Input_Key, pNewFileBuf);
if (Ls_CBC && i != 0) {
CBC_Xor(Bit_Mingwen, pNewFileBuf, pFileBuf-8);
ShowResult(Bit_Mingwen, pNewFileBuf);
}
pFileBuf += 8;
pNewFileBuf += 8;
}
pNewFileBuf -= count * 8;
int n = FilePath.ReverseFind('.');
CString NewFilePath = FilePath.Left(n + 1);
NewFilePath += "decrypted";
HANDLE NewFile_Handle = CreateFile(NewFilePath,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD WriteSize = 0;
WriteFile(NewFile_Handle, pNewFileBuf, FileSize, &WriteSize, NULL);
CloseHandle(NewFile_Handle);
NewFile_Handle = NULL;
MessageBoxA(" finished decrypting ", "Nice", NULL);
}
********************************************************************************
14. Check Information integrity detection button
effect : Respond to the decryption button , Check whether the information is complete
BOOL CDESFileDlg::Check() {
if (CString_Key.GetLength() != 8) {
MessageBoxA(" The key should be 8 position ","Error",MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
边栏推荐
- Implementation of Ackermann function with simulated recursion
- Alicloud development board vscode development environment setup
- 如果要打造品牌知名度,可以选择什么出价策略?
- Web3.0, the era of "stimulating creativity"
- 阿里云开发板HaaS510将串口获取数据发送到物联网平台
- Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode
- Cmake basic tutorial - 02 b-hello-cmake
- [WUSTCTF2020]颜值成绩查询-1
- Code debugging - print log output to file
- Real time software source code of COVID-19
猜你喜欢

数据类型转换和条件控制语句

A method of quickly creating test window

Alibaba cloud development board haas510 submission device attributes

Encryptor and client authenticate with each other

Talk about the top 10 classic MySQL errors

阿里云开发板HaaS510解析串口JSON数据并发送属性

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

动态搜索广告智能查找匹配关键字

Data type conversion and conditional control statements

After reading the question, you will point to offer 16 Integer power of numeric value
随机推荐
如何使用android studio制作一个阿里云物联网APP
280 weeks /2171 Take out the least number of magic beans
Codeforces 1638 B. odd swap sort - tree array, no, simple thinking
Data type conversion and conditional control statements
当字节跳动在美国输出中国式 996
Wait function in SystemC
SystemC simulation scheduling mechanism
Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
Backtracking: Prime Rings
Mold and remainder
[WUSTCTF2020]颜值成绩查询-1
SystemC:SC_ Thread and SC_ METHOD
To SystemC Beginners: the first program
Dismantle and modify the advertising machine - Amateur decompression
[wustctf2020] selfie score query -1
Tree reconstruction (pre order + middle order or post order + middle order)
Alibaba Cloud Development Board haas510 submission Device Properties
Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation
Codeforces 1637 B. mex and array - reading, violence
简述CGI与FASTCGI区别