#if !defined _TRYTO_YDESH_ #define _TRYTO_YDESH_ #include "stdafx.h" #define TEXT_MAX 1024 typedef bool (*PSubKey)[16][48]; enum {ENCRYPT,DECRYPT}; static bool SubKey[2][16][48];// 16圈子密钥 static bool Is3DES;// 3次DES标志 static BYTE Tmp[256], deskey[16]; class yDES { public: static void DES(BYTE Out[8],const BYTE *In, const PSubKey pSubKey, bool Type);//标准DES加/解密 static void SetKey(const BYTE* Key, int len);// 设置密钥 // Type—ENCRYPT:加密,DECRYPT:解密 // 输出缓冲区(Out)的长度 >= ((datalen+7)/8)*8,即比datalen大的且是8的倍数的最小正整数 // In 可以= Out,此时加/解密后将覆盖输入缓冲区(In)的内容 // 当keylen>8时系统自动使用3次DES加/解密,否则使用标准DES加/解密.超过16字节后只取前16字节 bool DES_Act(BYTE *Out,const BYTE *In,size_t datalen,const BYTE *Key,int keylen,bool Type = ENCRYPT); private: static void SetSubKey(PSubKey pSubKey, const BYTE Key[8]);// 设置子密钥 static void F_func(bool In[32], const bool Ki[48]);// f 函数 static void S_func(bool Out[32], const bool In[48]);// S 盒代替 static void Transform(bool *Out, bool *In, const BYTE *Table, int len);// 变换 static void Xor(bool *InA, const bool *InB, int len);// 异或 static void RotateL(bool *In, int len, int loop);// 循环左移 static void ByteToBit(bool *Out, const BYTE *In, int bits);// 字节组转换成位组 static void BitToByte(BYTE *Out, const bool *In, int bits);// 位组转换成字节组 }; #endif