This is a previous article, only to find out that the content was not put in before today. It's so dizzy. Luckily I looked at the comments and someone pointed it out. I am really sorry. Now put the code up; statement, not my own completely original. Also from the internet to find someone else's things, and then changed. #ifndef cdes_h_caesar__def #define CDES_H_CAESAR__DEF/*! /brief CDEs Class Description * * This class is des and 3DES algorithm class * * CDEs {public:cdes (); virtual ~cdes ();//Encrypt decrypt enum {ENCRYPT = 0,//! encryption decry PT,//! decryption}; Des algorithm schema enum {ECB = 0,//! ECB mode CBC//! CBC mode}; typedef BOOL (*PSUBKEY) [16][48]; Pad-filled pattern enum {pad_iso_1 = 0,//! Iso_1 padding: A multiple of less than 8 bits of data, 0x00 to complement, if a multiple of 8 bits, make up 8 0x00 pad_iso_2,//! Iso_2 padding: A multiple of less than 8 bits of data, to 0x80,0x00. Make up, if a multiple of 8 bits, fill 0x80,0x00. 0x00 pad_pkcs_7//! PKCS7 padding: Data length In addition to 8 number of N, to (8-n) to make up a multiple of 8}; /*/brief to complement 8-bit data * * Description: Fill the data before encryption according to the protocol * @param ntype: Type: Pad type * @param in: Data string pointer * @param out: Fill output string pointer * @par AM Datalen: Data length * @param padlen:(in,out) output buffer length, length after filling * @return true--succeeded; false--failed; */static bool Runpad (int Ntyp E,const unsigned char* in, unsigned datalen,unsigned char*, out,unsigned& Padlen); /*/brief execution des algorithm for text encryption * Description: Perform des algorithm to decrypt text with * @param btype: Type:Encryption encrypt, decrypting decrypt * @param bmode: Mode: ECB,CBC * @param in: To be encrypted string pointer * @param out: The output string pointer * @param datalen: The length of the string to be encrypted, while out of the slow The flushing area size should be greater than or equal to Datalen * @param key: Key (8-bit, 16-bit, 24-bit) supports 3 key * @param keylen: Key length, extra 24 bits will be automatically cut * @return true--success; false--failure; * * static bool Rundes (bool Btype,bool bmode,const unsigned char* in, unsigned char* out,unsigned datalen,const-unsigned ch ar* key,unsigned Keylen); Protected://Compute and populate the subkey to the static void Setsubkey in the subkey data (Psubkey Psubkey, const unsigned char key[8]); Des unit operations static void DES (unsigned char out[8], const unsigned char in[8], const psubkey Psubkey, bool Type); }; #endif//cdes_h_caesar__def
The implementation code is as follows://DES.cpp:implementation of the CDEs class. #include "DES.h" #include "memory.h" # Include <iostream> 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 , 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 , 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 , 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, 1 7, 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, 5 9, 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 , 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} ; Number left rotations of PC1 const char loop_table[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1}; The (in) famous s-boxes const CHAR S_box[8][4][16] = {//S1 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, S2 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 , one, 4, 1, 5, 8, 6, 9, 3, 2, 8, 1, 3, 4, one, 2, 6, one, 7, 0, 5,///S3 9, 10, 0, 9, 14, 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, 1, 2, 5, 7, 1, 0, 6, 9, 8, 7, 4, 3, 5,,///S4 2, 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 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 , 3, 0, 6, 1, 8, 9, 4, 5, S5, 7, 2, 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 , 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, 4, 5, 3,//S6 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 , 9, 5, 2, 8, 3, 7, 0, 1, 6, 4, 3, 2, 9,,, 5, 4, 1, 7, 6, 0,//S7 8, 4,//// 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, 7, 6, 8, 0, 5, 9, 2, 6, one, 8, 1, 4,, 7, 9, 5, 0,,, 2, 3,,//S8 13, 2, 8, 4, 6, 15, 11, 1, 1 0, 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 5, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}; Cdes::cdes () {} cdes::~cdes () {}/*******************************************************************//* Function name: ByteT OBit function Description: Convert byte to bit flow parameter description: out: bit stream output [in][out] in: byte stream input [in] bitS:bit flow length [in] return value description: Void Author: Shand Update: 2003.12.19/**************************************************************** /static void Bytetobit (bool *out, const unsigned char *in, int bits) {for (int i=0; i<bits; ++i) out[i] = (in[i> >3]>> (7-i&7)) & 1; /*******************************************************************//* Function Name: bittobyte function Description: Convert bit to byte stream parameter Ming: Out: Output byte stream [In][out] in: Input bit stream [in] bits:bit length of the stream [in] return value description: Void Author: Shand Update: 2003.12.19/******************* /static void Bittobyte (unsigned char *out, const bool *in, int bits) {Me Mset (out, 0, bits>>3); for (int i=0; i<bits; ++i) out[i>>3] |= in[i]<< (7-i&7); /*******************************************************************//* Function Name: Rotatel function Description: The bit stream bitwise Left Iteration parameter description: In: the input bit stream [in] len:bit the length of the stream [in] loop: The length return value of the left iteration description: Void Author: Shand Update: 2003.12.19/******************************** ***/static void Rotatel (bool *in, int len, int loop) {bool tmp[256]; memcpy (TMP, in, loop); memcpy (in, In+loop, Len-loop); memcpy (In+len-loop, Tmp, loop); /*******************************************************************//* Function Name: Xor function Description: Two bit streams are different or parameter description: InA: bit stream input [in][out] InB: Input bit stream [in] loop:bit stream length return value description: Void Author: Shand Update: 2003.12.19/****************************** /static void Xor (bool *ina, const BOOL *INB, int len) {for (int i=0; i<len; ++i) Ina[i] ^= inb[i]; /*******************************************************************//* Function Name: Transform function Description: Convert two bit streams to table for bit conversion parameters Description: Out: bit stream of output [out] in: bit stream input [in] Table: Transformation Required table pointer len: Conversion table Length return value description: Void Author: Shand Update: 2003.12.19/************ /static void Transform (bool *out, bool *in, const char *table, int len) {bool tmp[256]; for (int i=0; i<len; ++i) tmp[i] = in[TABLE[I]-1]; memcpy (out, TMP, Len); /*******************************************************************//* Function Name: S_func function Description: Implement Data encryption S box module parameter description: Out: Output 32BIT[OUT] in: input 48bit[in] return value description: Void Author: Shand Update: 2003.12.19/****************************************** /static void S_func (BOOL out[32], const BOOL IN[48]) {for (char i=0,j,k; i<8; ++i,in+=6,out+= 4) {j = (in[0]<<1) + in[5]; k = (in[1]<<3) + (IN[2]<<2) + (in[3]<<1) + in[4];//Organization SID subscript for (int l = 0; l<4; ++L)///4bit assigned value out[l] = (s_box[i][j][k]>> (3-l)) & 1; }/*******************************************************************//* Function Name: F_func function Description: Implementing data encryption to output p parameter description: Out: Output 32BIT[OUT] in: input 48bit[in] return value description: Void Author: Shand Update: 2003.12.19/******************************************* /static void F_func (BOOL in[32], const BOOL KI[48]) {bool mr[48]; Transform (MR, in, E_table, 48); Xor (MR, Ki, 48); S_func (In,MR); Transform (in, in, P_table, 32); BOOL Cdes::rundes (bool Btype,bool bmode,const unsigned char* in, unsigned char* out,unsigned datalen,const-unsigned char * key,unsigned Keylen) {//Judge input legality if (!) ( In && out && Key && datalen && keylen>=8)-return false; unsigned char Inbuf[datalen + 8]; memset (Inbuf, 0x00, sizeof (INBUF)); memcpy (Inbuf, in, Datalen); unsigned char* tempbuf; unsigned padlen = Datalen; Handle only 8 integer times, less than the length of their fill if (Datalen & 0x00000007) {if (! Runpad (Pad_pkcs_7, in, Datalen, Inbuf, Padlen)) return false; Tempbuf = Inbuf; else {tempbuf = inbuf;} bool m_subkey[3][16][48]; Secret key//construct and generate subkeys unsigned char Nkey = (keylen>>3) >=3? 3: (keylen>>3); for (int i=0;i<nkey;i++) {Setsubkey (&m_subkey[i],&key[i<<3]);} if (Bmode = = ECB)//ECB mode {if (Nkey = 1) Single key {int j = padlen>>3; for (int i=0,j = padlen>>3;i<j;++i,out+=8,tempbuf+=8) {DES (out,tempbuf,&m_ Subkey[0],btype); } else if (Nkey = 2)//3des 2Key {for (int i=0,j = padlen>>3;i<j;++i,out+=8,tempbuf+=8) {DES (Out,tempbuf,&m_subkey[0],btype) ; DES (Out,out,&m_subkey[1],!btype); DES (Out,out,&m_subkey[0],btype); ' Else//3des 3Key {for (int i=0,j=padlen>>3;i<j;++i,out+=8,tempbuf+=8) {DES (out,tempbuf,&m_subkey[ Btype? 2:0],btype); DES (Out,out,&m_subkey[1],!btype); DES (Out,out,&m_subkey[btype 0:2],btype); }} else//CBC mode {unsigned char cvec[8] = ""; Torsion vector unsigned char cvin[8] = ""; Intermediate variable if (nkey = 1)//single key {for (int i=0,j=padlen>>3;i<j;++i,out+=8,tempbuf+=8) {if (btype = = Cdes::encrypt) {f or (int j=0;j<8;++j)//will be entered with the torsion variable or {cvin[j] = Tempbuf[j] ^ cvec[j];} else {memcpy (cvin,tempbuf,8);} DES (Out,cvin,&m_subkey[0],btype); if (btype = = Cdes::encrypt) {memcpy (cvec,out,8);////Set Output to torsion variable} else {for (int j=0;j<8;++j)///will output to reverse variable or {out[j] = out [j] ^ cvec[j]; } memcpy (cvec,cvin,8); Set input to twist variable}} else if (Nkey = 2)//3des CBC 2Key {for (int i=0,j=padlen>>3;i<j;++i,out+=8,tempbuf+=8) {if (btype = = Cdes::encrypt) {for (int j=0;j<8;++j)///will be entered with a twist variable or {Cvin [j] = Tempbuf[j] ^ cvec[j]; } else {memcpy (cvin,tempbuf,8);} DES (Out,cvin,&m_subkey[0],btype); DES (Out,out,&m_subkey[1],!btype); DES (Out,out,&m_subkey[0],btype); if (btype = = Cdes::encrypt) {memcpy (cvec,out,8);////Set Output to torsion variable} else {for (int j=0;j<8;++j)///will output to reverse variable or {out[j] = out [j] ^ cvec[j]; } memcpy (cvec,cvin,8); Set input to twist variable}} else//3des CBC 3Key {for (int i=0,j=padlen >>3;i<j;++i,out+=8,tempbuf+=8) {if (btype = = CDEs: : ENCRYPT) {for (int j=0;j<8;++j)//will enter and reverse variable or {cvin[j] = Tempbuf[j] ^ cvec[j];}} else {memcpy (cvin,tempbuf,8);} DES (Out,cvin,&m_subkey[btype 2:0],btype); DES (Out,out,&m_subkey[1],!btype); DES (Out,out,&m_subkey[btype 0:2],btype); if (btype = = Cdes::encrypt) {memcpy (cvec,out,8);////Set Output to torsion variable} else {for (int j=0;j<8;++j)///will output to reverse variable or {out[j] = out [j] ^ cvec[j]; } memcpy (cvec,cvin,8); WillInput set to reverse variable}}} return true; /*******************************************************************//* Function Name: Runpad function Description: According to the protocol to the encryption data before the filling parameters Ming: btype: Type: Pad type in: Data string pointer out: Fill output string pointer datalen: Length of data Padlen:(in,out) Output buffer length, fill length return value Description: bool: Whether to fill the successful author: Shand Modify History: Update Date: 2003.12.19/*******************************************************************/bool CDES::RunPad (int n Type, const unsigned char* in, unsigned datalen, unsigned char* out, unsigned& padlen) {if (Ntype < Pad_iso_1 | | n Type > Pad_pkcs_7) return false; if (in = = NULL | | Datalen < 0 | | out = = NULL) return false; int res = (Datalen & 0x00000007); if (res = = 0) {Padlen = Datalen; memcpy (out, in, Datalen); return true;} Padlen = (datalen+8-res); memcpy (Out,in,datalen); if (ntype = = pad_iso_1) {memset (out+datalen,0x00,8-res);} else if (Ntype = pad_iso_2) {memset (out+datalen,0x80,1); Memse T (out+datalen,0x00,7-res); else if (ntype = = pad_pkcs_7) {memset (out+datalen,0x00,8-res);} else {return false;} return true; Calculate and populate the subkey to subkey data in void Cdes::setsubkey (Psubkey psubkey, const unsigned char key[8]) {bool k[64], *kl=&k[0], *kr= &K[28]; Bytetobit (K, Key, 64); Transform (k, K, pc1_table, 56); for (int i=0; i<16; ++i) {Rotatel (KL, loop_table[i]); Rotatel (KR, loop_table[i]); Transform ((*psubkey) [i], K, pc2_table, 48); }//des cell operator void CDEs::D es (unsigned char out[8], const unsigned char in[8], const psubkey Psubkey, BOOL Type) {bool M[6 4], tmp[32], *li=&m[0], *ri=&m[32]; Bytetobit (M, in, 64); Transform (M, M, Ip_table, 64); if (Type = = ENCRYPT) {for (int i=0; i<16; ++i) {memcpy (tmp, RI, L);//ri[i-1] Save F_func (ri, (*psubkey) [i]);//ri[i-1 The transformed and Sbox output is P Xor (Ri, Li, 32); Ri[i] = P XOR li[i-1] memcpy (Li, TMP, 32); Li[i] = Ri[i-1]} else {for (int i=15; i>=0;-i) {memcpy (tmp, RI, L);//ri[i-1] Save F_func (ri, (*psubkey) [i]);// Ri[i-1] Transformed and sbox output to P Xor (Ri, Li, 32); Ri[i] = P XOR li[i-1] memcpy (Li, TMP, 32); Li[i] = ri[i-1]} rotatel (m,64,32); Ri and Li transposition recombination m Transform (M, M, Ipr_table, 64); The final results are transformed into bittobyte (out, M, 64); Organize into characters}