DES实验要求
实验目的
实现电子本模式(ECB)和密码分组链接模式(CBC)的DES算法。
实验要求
- 指定明文文件、密钥文件、初始化向量文件的位置和名称,加密的操作模式以及加密完成后密文文件的位置和名称。
- 加密时先分别从指定的明文文件、密钥文件和初始化向量文件中读取有关信息,然后按ECB和CBC操作模式分别进行加密,最后将密文(用16进制表示)写入指定的密文文件。解密类似。
- 统一将文件名命名如下:
明文文件名:des_messages.txt
密钥文件名:des_key.txt
密文文件名:des_secret.txt
解密结果文件名:des_decrypted.txt
初始化向量文件:des_iv.txt - 如果一个分组不足64bit,低位补0。
代码
DES.h
#ifndef DESH
#define DESH
#include <string>
using namespace std;
/*DES.h: declaration of the TDES、TBase64、TBase64DES class. */
/* TDES类说明:该类是DES和3DES算法类 */
class CDES {
public:
CDES();
virtual ~CDES();
static char* HexToStr(string s);
static string StrToHex(char* bytes, int bytelength);
static bool EnCode(); //des加密
static bool DeCode(); //des解密
protected:
typedef bool(*PSubKey)[16][48];
//计算并填充子密钥到SubKey数据中
static void SetSubKey(PSubKey pSubKey, const unsigned char Key[8]);
//DES单元运算
static void DES(unsigned char Out[8], const unsigned char In[8], const PSubKey pSubKey, bool Type);
/* 补足8位数据
* Description: 根据协议对加密前的数据进行填充
* @param nType: 类型:PAD类型
* @param In: 数据串指针
* @param Out: 填充输出串指针
* @param datalen: 数据的长度
* @param padlen: (in,out)输出buffer的长度,填充后的长度
* @return true--成功;false--失败;
*/
static bool RunPad(bool bType, int nType, const unsigned char* In, unsigned datalen, unsigned char* Out, unsigned& padlen);
/* 执行DES算法对文本加解密
* Description : 执行DES算法对文本加解密
* @param bType : 类型:加密ENCRYPT,解密DECRYPT
* @param bMode : 模式:ECB,CBC
* @param In : 待加密串指针
* @param Out : 待输出串指针
* @param datalen : 待加密串的长度,同时Out的缓冲区大小应大于或者等于datalen
* @param Key : 密钥(可为8位,16位,24位)支持3密钥
* @param keylen : 密钥长度,多出24位部分将被自动裁减
* @return true--成功;false--失败;
*/
static bool RunDES(bool bType, bool bMode, int PaddingMode, const unsigned char* IV, const unsigned char* In,
unsigned char* Out, unsigned datalen, const unsigned char* Key, unsigned keylen);
private:
/*static int hexCharToInt(char c);*/
enum {
ENCRYPT = 0, // 加密
DECRYPT, // 解密
};
enum {
ECB = 0, // ECB模式
CBC // CBC模式
};
enum {
PAD_ISO_1 = 0, // ISO_1填充:数据长度不足8比特的倍数,以0x00补足,如果为8比特的倍数,补8个0x00
};
};
#endif
DES.cpp
#include "DES.h"
#include "memory.h"
#include <iostream>
#include<fstream>
using namespace std;
/* initial permutation IP */
const char IP_Table[64] = {
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
};
/* final permutation IP^-1 */
const char IPR_Table[64] = {
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
};
/* expansion operation matrix */
const char E_Table[48] = {
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
};
/* 32-bit permutation function P used on the output of the S-boxes */
const char P_Table[32] = {
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
};
/* permuted choice table (key) */
const char PC1_Table[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
};
/* permuted choice key (table) */
const char PC2_Table[48] = {
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
};
/* number left rotations of pc1 */
const char LOOP_Table[