123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924 |
- #include "stdafx.h"
- #include "des.h"
- #include <string.h>
- #include "..\..\common\Base64\Base64.h"
- #include "..\..\common\EncodingConversion\EncodingConversion.h"
- static const unsigned char deskey[9] = "mygz/ndu";//lyfz.net的偶校验结果;
- //static const unsigned char desiv[9] = "WorkbyIT";
- // 32-bit integer manipulation macros (big endian)
- #ifndef GET_ULONG_BE
- #define GET_ULONG_BE(n,b,i) \
- { \
- (n) = ( (unsigned long) (b)[(i)] << 24 ) \
- | ( (unsigned long) (b)[(i) + 1] << 16 ) \
- | ( (unsigned long) (b)[(i) + 2] << 8 ) \
- | ( (unsigned long) (b)[(i) + 3] ); \
- }
- #endif
- #ifndef PUT_ULONG_BE
- #define PUT_ULONG_BE(n,b,i) \
- { \
- (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
- (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
- (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
- (b)[(i) + 3] = (unsigned char) ( (n) ); \
- }
- #endif
- //Expanded DES S-boxes
- static const unsigned long SB1[64] =
- {
- 0x01010400, 0x00000000, 0x00010000, 0x01010404,
- 0x01010004, 0x00010404, 0x00000004, 0x00010000,
- 0x00000400, 0x01010400, 0x01010404, 0x00000400,
- 0x01000404, 0x01010004, 0x01000000, 0x00000004,
- 0x00000404, 0x01000400, 0x01000400, 0x00010400,
- 0x00010400, 0x01010000, 0x01010000, 0x01000404,
- 0x00010004, 0x01000004, 0x01000004, 0x00010004,
- 0x00000000, 0x00000404, 0x00010404, 0x01000000,
- 0x00010000, 0x01010404, 0x00000004, 0x01010000,
- 0x01010400, 0x01000000, 0x01000000, 0x00000400,
- 0x01010004, 0x00010000, 0x00010400, 0x01000004,
- 0x00000400, 0x00000004, 0x01000404, 0x00010404,
- 0x01010404, 0x00010004, 0x01010000, 0x01000404,
- 0x01000004, 0x00000404, 0x00010404, 0x01010400,
- 0x00000404, 0x01000400, 0x01000400, 0x00000000,
- 0x00010004, 0x00010400, 0x00000000, 0x01010004
- };
- static const unsigned long SB2[64] =
- {
- 0x80108020, 0x80008000, 0x00008000, 0x00108020,
- 0x00100000, 0x00000020, 0x80100020, 0x80008020,
- 0x80000020, 0x80108020, 0x80108000, 0x80000000,
- 0x80008000, 0x00100000, 0x00000020, 0x80100020,
- 0x00108000, 0x00100020, 0x80008020, 0x00000000,
- 0x80000000, 0x00008000, 0x00108020, 0x80100000,
- 0x00100020, 0x80000020, 0x00000000, 0x00108000,
- 0x00008020, 0x80108000, 0x80100000, 0x00008020,
- 0x00000000, 0x00108020, 0x80100020, 0x00100000,
- 0x80008020, 0x80100000, 0x80108000, 0x00008000,
- 0x80100000, 0x80008000, 0x00000020, 0x80108020,
- 0x00108020, 0x00000020, 0x00008000, 0x80000000,
- 0x00008020, 0x80108000, 0x00100000, 0x80000020,
- 0x00100020, 0x80008020, 0x80000020, 0x00100020,
- 0x00108000, 0x00000000, 0x80008000, 0x00008020,
- 0x80000000, 0x80100020, 0x80108020, 0x00108000
- };
- static const unsigned long SB3[64] =
- {
- 0x00000208, 0x08020200, 0x00000000, 0x08020008,
- 0x08000200, 0x00000000, 0x00020208, 0x08000200,
- 0x00020008, 0x08000008, 0x08000008, 0x00020000,
- 0x08020208, 0x00020008, 0x08020000, 0x00000208,
- 0x08000000, 0x00000008, 0x08020200, 0x00000200,
- 0x00020200, 0x08020000, 0x08020008, 0x00020208,
- 0x08000208, 0x00020200, 0x00020000, 0x08000208,
- 0x00000008, 0x08020208, 0x00000200, 0x08000000,
- 0x08020200, 0x08000000, 0x00020008, 0x00000208,
- 0x00020000, 0x08020200, 0x08000200, 0x00000000,
- 0x00000200, 0x00020008, 0x08020208, 0x08000200,
- 0x08000008, 0x00000200, 0x00000000, 0x08020008,
- 0x08000208, 0x00020000, 0x08000000, 0x08020208,
- 0x00000008, 0x00020208, 0x00020200, 0x08000008,
- 0x08020000, 0x08000208, 0x00000208, 0x08020000,
- 0x00020208, 0x00000008, 0x08020008, 0x00020200
- };
- static const unsigned long SB4[64] =
- {
- 0x00802001, 0x00002081, 0x00002081, 0x00000080,
- 0x00802080, 0x00800081, 0x00800001, 0x00002001,
- 0x00000000, 0x00802000, 0x00802000, 0x00802081,
- 0x00000081, 0x00000000, 0x00800080, 0x00800001,
- 0x00000001, 0x00002000, 0x00800000, 0x00802001,
- 0x00000080, 0x00800000, 0x00002001, 0x00002080,
- 0x00800081, 0x00000001, 0x00002080, 0x00800080,
- 0x00002000, 0x00802080, 0x00802081, 0x00000081,
- 0x00800080, 0x00800001, 0x00802000, 0x00802081,
- 0x00000081, 0x00000000, 0x00000000, 0x00802000,
- 0x00002080, 0x00800080, 0x00800081, 0x00000001,
- 0x00802001, 0x00002081, 0x00002081, 0x00000080,
- 0x00802081, 0x00000081, 0x00000001, 0x00002000,
- 0x00800001, 0x00002001, 0x00802080, 0x00800081,
- 0x00002001, 0x00002080, 0x00800000, 0x00802001,
- 0x00000080, 0x00800000, 0x00002000, 0x00802080
- };
- static const unsigned long SB5[64] =
- {
- 0x00000100, 0x02080100, 0x02080000, 0x42000100,
- 0x00080000, 0x00000100, 0x40000000, 0x02080000,
- 0x40080100, 0x00080000, 0x02000100, 0x40080100,
- 0x42000100, 0x42080000, 0x00080100, 0x40000000,
- 0x02000000, 0x40080000, 0x40080000, 0x00000000,
- 0x40000100, 0x42080100, 0x42080100, 0x02000100,
- 0x42080000, 0x40000100, 0x00000000, 0x42000000,
- 0x02080100, 0x02000000, 0x42000000, 0x00080100,
- 0x00080000, 0x42000100, 0x00000100, 0x02000000,
- 0x40000000, 0x02080000, 0x42000100, 0x40080100,
- 0x02000100, 0x40000000, 0x42080000, 0x02080100,
- 0x40080100, 0x00000100, 0x02000000, 0x42080000,
- 0x42080100, 0x00080100, 0x42000000, 0x42080100,
- 0x02080000, 0x00000000, 0x40080000, 0x42000000,
- 0x00080100, 0x02000100, 0x40000100, 0x00080000,
- 0x00000000, 0x40080000, 0x02080100, 0x40000100
- };
- static const unsigned long SB6[64] =
- {
- 0x20000010, 0x20400000, 0x00004000, 0x20404010,
- 0x20400000, 0x00000010, 0x20404010, 0x00400000,
- 0x20004000, 0x00404010, 0x00400000, 0x20000010,
- 0x00400010, 0x20004000, 0x20000000, 0x00004010,
- 0x00000000, 0x00400010, 0x20004010, 0x00004000,
- 0x00404000, 0x20004010, 0x00000010, 0x20400010,
- 0x20400010, 0x00000000, 0x00404010, 0x20404000,
- 0x00004010, 0x00404000, 0x20404000, 0x20000000,
- 0x20004000, 0x00000010, 0x20400010, 0x00404000,
- 0x20404010, 0x00400000, 0x00004010, 0x20000010,
- 0x00400000, 0x20004000, 0x20000000, 0x00004010,
- 0x20000010, 0x20404010, 0x00404000, 0x20400000,
- 0x00404010, 0x20404000, 0x00000000, 0x20400010,
- 0x00000010, 0x00004000, 0x20400000, 0x00404010,
- 0x00004000, 0x00400010, 0x20004010, 0x00000000,
- 0x20404000, 0x20000000, 0x00400010, 0x20004010
- };
- static const unsigned long SB7[64] =
- {
- 0x00200000, 0x04200002, 0x04000802, 0x00000000,
- 0x00000800, 0x04000802, 0x00200802, 0x04200800,
- 0x04200802, 0x00200000, 0x00000000, 0x04000002,
- 0x00000002, 0x04000000, 0x04200002, 0x00000802,
- 0x04000800, 0x00200802, 0x00200002, 0x04000800,
- 0x04000002, 0x04200000, 0x04200800, 0x00200002,
- 0x04200000, 0x00000800, 0x00000802, 0x04200802,
- 0x00200800, 0x00000002, 0x04000000, 0x00200800,
- 0x04000000, 0x00200800, 0x00200000, 0x04000802,
- 0x04000802, 0x04200002, 0x04200002, 0x00000002,
- 0x00200002, 0x04000000, 0x04000800, 0x00200000,
- 0x04200800, 0x00000802, 0x00200802, 0x04200800,
- 0x00000802, 0x04000002, 0x04200802, 0x04200000,
- 0x00200800, 0x00000000, 0x00000002, 0x04200802,
- 0x00000000, 0x00200802, 0x04200000, 0x00000800,
- 0x04000002, 0x04000800, 0x00000800, 0x00200002
- };
- static const unsigned long SB8[64] =
- {
- 0x10001040, 0x00001000, 0x00040000, 0x10041040,
- 0x10000000, 0x10001040, 0x00000040, 0x10000000,
- 0x00040040, 0x10040000, 0x10041040, 0x00041000,
- 0x10041000, 0x00041040, 0x00001000, 0x00000040,
- 0x10040000, 0x10000040, 0x10001000, 0x00001040,
- 0x00041000, 0x00040040, 0x10040040, 0x10041000,
- 0x00001040, 0x00000000, 0x00000000, 0x10040040,
- 0x10000040, 0x10001000, 0x00041040, 0x00040000,
- 0x00041040, 0x00040000, 0x10041000, 0x00001000,
- 0x00000040, 0x10040040, 0x00001000, 0x00041040,
- 0x10001000, 0x00000040, 0x10000040, 0x10040000,
- 0x10040040, 0x10000000, 0x00040000, 0x10001040,
- 0x00000000, 0x10041040, 0x00040040, 0x10000040,
- 0x10040000, 0x10001000, 0x10001040, 0x00000000,
- 0x10041040, 0x00041000, 0x00041000, 0x00001040,
- 0x00001040, 0x00040040, 0x10000000, 0x10041000
- };
- //PC1: left and right halves bit-swap
- static const unsigned long LHs[16] =
- {
- 0x00000000, 0x00000001, 0x00000100, 0x00000101,
- 0x00010000, 0x00010001, 0x00010100, 0x00010101,
- 0x01000000, 0x01000001, 0x01000100, 0x01000101,
- 0x01010000, 0x01010001, 0x01010100, 0x01010101
- };
- static const unsigned long RHs[16] =
- {
- 0x00000000, 0x01000000, 0x00010000, 0x01010000,
- 0x00000100, 0x01000100, 0x00010100, 0x01010100,
- 0x00000001, 0x01000001, 0x00010001, 0x01010001,
- 0x00000101, 0x01000101, 0x00010101, 0x01010101,
- };
- static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
- 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
- 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
- 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
- 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
- 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
- 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
- 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
- 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
- 254
- };
- #define WEAK_KEY_COUNT 16
- static const unsigned char weak_key_table[WEAK_KEY_COUNT][DES_KEY_SIZE] =
- {
- { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
- { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
- { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
- { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
- { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
- { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
- { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
- { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
- { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
- { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
- { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
- { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
- { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
- { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
- { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
- { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
- };
- // Initial Permutation macro
- #define DES_IP(X,Y) \
- { \
- T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
- T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
- T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
- T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
- Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
- T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
- X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
- }
- // Final Permutation macro
- #define DES_FP(X,Y) \
- { \
- X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
- T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
- Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
- T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
- T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
- T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
- T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
- }
- // DES round macro
- #define DES_ROUND(X,Y) \
- { \
- T = *SK++ ^ X; \
- Y ^= SB8[ (T) & 0x3F ] ^ \
- SB6[ (T >> 8) & 0x3F ] ^ \
- SB4[ (T >> 16) & 0x3F ] ^ \
- SB2[ (T >> 24) & 0x3F ]; \
- \
- T = *SK++ ^ ((X << 28) | (X >> 4)); \
- Y ^= SB7[ (T) & 0x3F ] ^ \
- SB5[ (T >> 8) & 0x3F ] ^ \
- SB3[ (T >> 16) & 0x3F ] ^ \
- SB1[ (T >> 24) & 0x3F ]; \
- }
- #define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; }
- void des_key_set_parity( unsigned char key[DES_KEY_SIZE] )
- {
- for(int i = 0; i < DES_KEY_SIZE; i++ )
- key[i] = odd_parity_table[key[i] / 2];
- }
- int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] )
- {
- for(int i = 0; i < DES_KEY_SIZE; i++ )
- if( key[i] != odd_parity_table[key[i] / 2] )
- return( 1 );
- return( 0 );
- }
- int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] )
- {
- for(int i = 0; i < WEAK_KEY_COUNT; i++ )
- if( memcmp( weak_key_table[i], key, DES_KEY_SIZE) == 0 )
- return( 1 );
- return( 0 );
- }
- unsigned __int64 des_enc_len(const unsigned __int64& nlen)
- {
- unsigned __int64 npaddinglen = nlen;
- int npad = nlen&7;//nFileLen & 7 <=> nFileLen%8
- if ( npad != 0 )
- {
- npaddinglen += 8 - npad;
- }
- return npaddinglen;
- }
- unsigned __int64 des_dec_len(const unsigned char* input, unsigned __int64 nlen)
- {
- while( input[--nlen] == 0x00 );
- return ++nlen;
- }
- /************************************************************************/
- /* 函数:des_setkey[6/5/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- static void des_setkey( unsigned long SK[32], const unsigned char key[8] )
- {
- int i;
- unsigned long X, Y, T;
- GET_ULONG_BE( X, key, 0 );
- GET_ULONG_BE( Y, key, 4 );
- // Permuted Choice 1
- T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
- T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
- X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
- | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
- | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
- | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
- Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
- | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
- | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
- | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
- X &= 0x0FFFFFFF;
- Y &= 0x0FFFFFFF;
- // calculate subkeys
- for( i = 0; i < 16; i++ )
- {
- if( i < 2 || i == 8 || i == 15 )
- {
- X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
- Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
- }
- else
- {
- X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
- Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
- }
- *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
- | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
- | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
- | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
- | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
- | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
- | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
- | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
- | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
- | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
- | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
- *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
- | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
- | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
- | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
- | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
- | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
- | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
- | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
- | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
- | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
- | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
- }
- }
- /************************************************************************/
- /* 函数:des_setkey_enc[6/5/2016 IT];
- /* 描述:设置DES加密密钥(56bit,加密);
- /* 参数:;
- /* [IN] ctx:要被初始化的DES结构体;
- /* [IN] key:8字节长度的加解密密钥;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void des_setkey_enc( IN des_context *ctx, IN const unsigned char key[8] )
- {
- des_setkey( ctx->sk, key );
- }
- /************************************************************************/
- /* 函数:des_setkey_dec[6/5/2016 IT];
- /* 描述:设置DES解密密钥(56bit,解密);;
- /* 参数:;
- /* [IN] ctx:要被初始化的DES结构体;
- /* [IN] key:8字节长度的加解密密钥;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void des_setkey_dec( IN des_context *ctx, IN const unsigned char key[8] )
- {
- int i;
- des_setkey( ctx->sk, key );
- for( i = 0; i < 16; i += 2 )
- {
- SWAP( ctx->sk[i], ctx->sk[30 - i] );
- SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
- }
- }
- /************************************************************************/
- /* 函数:[6/5/2016 IT];
- /* 描述:DES-ECB模式下加解密;
- /* 参数:;
- /* [IN] ctx:已经初始化过的DES结构体;
- /* [IN] input:64bit的输入缓存块;
- /* [OUT] output:64bit的输出缓存块;
- /* 返回:成功返回0;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int des_crypt_ecb( IN des_context *ctx, IN const unsigned char input[8], OUT unsigned char output[8] )
- {
- int i;
- unsigned long X, Y, T, *SK;
- SK = ctx->sk;
- GET_ULONG_BE( X, input, 0 );
- GET_ULONG_BE( Y, input, 4 );
- DES_IP( X, Y );
- for( i = 0; i < 8; i++ )
- {
- DES_ROUND( Y, X );
- DES_ROUND( X, Y );
- }
- DES_FP( Y, X );
- PUT_ULONG_BE( Y, output, 0 );
- PUT_ULONG_BE( X, output, 4 );
- return( 0 );
- }
- /************************************************************************/
- /* 函数:des_crypt_cbc[6/5/2016 IT];
- /* 描述:DES-CBC模式加解密;
- /* 参数:;
- /* [IN] ctx:已经初始化过的DES结构体;
- /* [IN] mode:加密或解密模式;
- /* [IN] length:input长度;
- /* [IN] iv:初始向量(使用后更新);
- /* [IN] input:要加密或解密的数据;
- /* [OUT] output:加密或解密的数据;
- /* 返回:成功返回0;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int des_crypt_cbc( IN des_context *ctx, IN int mode, IN int length, IN unsigned char iv[8], IN const unsigned char *input, OUT unsigned char *output )
- {
- int i;
- unsigned char temp[8];
- if( length % 8 )
- return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH );
- if( mode == DES_ENCRYPT )
- {
- while( length > 0 )
- {
- for( i = 0; i < 8; i++ )
- output[i] = (unsigned char)( input[i] ^ iv[i] );
- des_crypt_ecb( ctx, output, output );
- memcpy( iv, output, 8 );
- input += 8;
- output += 8;
- length -= 8;
- }
- }
- else /* DES_DECRYPT */
- {
- while( length > 0 )
- {
- memcpy( temp, input, 8 );
- des_crypt_ecb( ctx, input, output );
- for( i = 0; i < 8; i++ )
- output[i] = (unsigned char)( output[i] ^ iv[i] );
- memcpy( iv, temp, 8 );
- input += 8;
- output += 8;
- length -= 8;
- }
- }
- return( 0 );
- }
- /************************************************************************/
- /* 函数:des_crypt_cbc[6/5/2016 IT];
- /* 描述:DES-CBC模式加解密;
- /* 参数:;
- /* [IN] ctx:已经初始化过的DES结构体;
- /* [IN] mode:加密或解密模式;
- /* [IN] length:input长度;
- /* [IN] iv:初始向量(使用后更新);
- /* [IN] input:要加密或解密的数据;
- /* [OUT] output:加密或解密的数据;
- /* 返回:成功返回0;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int des_crypt_cbc(IN int mode, IN int length, IN const unsigned char *input, OUT unsigned char *output, IN const int& outMaxlen )
- {
- if ( length == 0 )
- return -1;
- int i;
- des_context ctx;
- if ( mode == DES_ENCRYPT )
- des_setkey_enc(&ctx, deskey);
- else
- des_setkey_dec(&ctx, deskey);
- int nInputlen = length;
- unsigned char *result = output;
- unsigned char iv[9] = "WorkbyIT";
- unsigned char temp[8];
- int npadlen = length & 7;
- if( npadlen != 0 )
- {
- length += 8 - npadlen;
- nInputlen = length;
- }
- if( mode == DES_ENCRYPT )
- {
- while( length > 0 )
- {
- for( i = 0; i < 8; i++ )
- {
- if ( length == 8 && npadlen != 0)
- {
- if ( i >= npadlen )
- output[i] = (unsigned char)( 0x00 ^ iv[i] );
- else
- output[i] = (unsigned char)( input[i] ^ iv[i] );
- }
- else
- output[i] = (unsigned char)( input[i] ^ iv[i] );
- }
- des_crypt_ecb( &ctx, output, output );
- memcpy( iv, output, 8 );
- input += 8;
- output += 8;
- length -= 8;
- }
- // 将加密结果转换成base64;
- // 1.计算出Base64的长度,CalcBase64Len的参数必须是字节长度,而非字符数;
- INT nBaseLen = CBase64::CalcBase64Len(nInputlen);
- if ( nBaseLen > outMaxlen )
- return -1;
- // 2.创建Base64缓存;
- CHAR *pBase64 = new CHAR[nBaseLen + 1];
- memset(pBase64, 0, nBaseLen + 1);
- // 3.转化出Base64字符;
- CBase64::binToBase64(result, nInputlen, pBase64);
- memset(result, 0, outMaxlen);
- memcpy(result, pBase64, nBaseLen);
- if ( pBase64 )
- delete []pBase64;
- }
- else /* DES_DECRYPT */
- {
- // 将base64转换成密文状态;
- // 1.计算出字节长度;
- INT nByteLen = CBase64::CalcBinLen(length);
- // 2.创建Byte缓存区;
- BYTE *pBytes = new BYTE[nByteLen];
- memset(pBytes, 0, nByteLen);
- // 3.转化成字节;
- length = CBase64::base64ToBin((char*)input, pBytes, nByteLen);
- result = pBytes;
- while ( pBytes[length-1] == 0x00 )
- --length;
- while( length > 0 )
- {
- memcpy( temp, pBytes, 8 );
- des_crypt_ecb( &ctx, pBytes, output );
- for( i = 0; i < 8; i++ )
- output[i] = (unsigned char)( output[i] ^ iv[i] );
- memcpy( iv, temp, 8 );
- pBytes += 8;
- output += 8;
- length -= 8;
- }
- if ( result )
- delete []result;
- }
- return( 0 );
- }
- int des_crypt(IN CString strInput, IN CString& strOutput, IN const int &mode /* = DES_ENCRYPT */)
- {
- if ( strInput.IsEmpty() )
- return -1;
- unsigned char* pInput = NULL;
- int nInputlen = strInput.GetLength();
- #ifdef UNICODE
- pInput = (unsigned char*)EncodingConverion::UNICODE2ASCII(strInput.GetBuffer());
- nInputlen = strlen((char*)pInput);
- #else
- pInput = new unsigned char[nInputlen+1];
- memset(pInput, 0, nInputlen+1);
- memcpy(pInput, strInput, nInputlen);
- #endif
- int nOutputlen = nInputlen;
- if ( mode == DES_ENCRYPT )
- {
- // 计算密文长度,不足8位补齐;
- nOutputlen = des_enc_len(nInputlen);
- // 计算密文转成base64的长度,用于创建输出缓存;
- nOutputlen = CBase64::CalcBase64Len(nOutputlen);
- }
- else
- {
- nOutputlen = CBase64::CalcBinLen(nInputlen);
- }
- unsigned char* pOutput = new unsigned char[nOutputlen+1];
- memset(pOutput, 0, nOutputlen + 1);
- if ( des_crypt_cbc(mode, nInputlen, pInput, pOutput, nOutputlen) == -1)
- return -1;
- #ifdef UNICODE
- WCHAR* pWide = EncodingConverion::ASCII2UNICODE((char*)pOutput);
- strOutput.Format(_T("%s"), pWide);
- if ( pWide )
- delete []pWide;
- #else
- strOutput.Format(_T("%s"), pOutput);
- #endif
- if ( pInput )
- delete []pInput;
- if ( pOutput )
- delete []pOutput;
- return 0;
- }
- /************************************************************************/
- /* 函数:[12/21/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void DES_EncryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpEncryFile)
- {
- if ( lpFile == NULL )
- return;
- if ( lpKey == NULL || lpVI == NULL )
- return;
- BYTE szKey[9] = {0};
- INT nKeyLen = strlen((char*)lpKey);
- if (nKeyLen != 8 )
- return;
- memcpy(szKey, lpKey, 8);
- BYTE szvi[9] = {0};
- INT nVILen = strlen((char*)lpVI);
- if ( nVILen != 8 )
- return;
- memcpy(szvi, lpVI, 8);
- CFile cf;
- BYTE *pFileData = NULL;
- BYTE *pOutData = NULL;
- INT nPadLen = 0;
- UINT64 nFileLen = 0;
- UINT64 nOutLen = 0;
- if ( cf.Open(lpFile, CFile::modeRead) )
- {
- nFileLen = cf.GetLength();
- pFileData = new BYTE[nFileLen];
- memset(pFileData, 0, nFileLen);
- cf.Read(pFileData,nFileLen);
- cf.Close();
- }
- else
- {
- DWORD dwError = GetLastError();
- TRACE("%d", dwError);
- return;
- }
- if ( strncmp((char*)pFileData, "<?>", 3) == 0 )
- {// 已加密的,退出;
- delete []pFileData;
- return;
- }
- // 不足8的倍数,补齐;
- nOutLen = des_enc_len(nFileLen);
- pOutData = new BYTE[nOutLen + 1 ];
- memset(pOutData, 0, nOutLen + 1 );
- des_context des;
- if ( des_key_check_key_parity(szKey) == 1)
- {
- des_key_set_parity(szKey);
- if ( des_key_check_key_parity(szKey) == 0 )
- {
- des_setkey_enc(&des, szKey);
- }
- }
- else
- des_setkey_enc(&des, szKey);
- des_crypt_cbc(&des, DES_ENCRYPT, nOutLen, szvi, pFileData, pOutData);
- CFile cfo;
- if ( cfo.Open(lpEncryFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfo.Write("<?>", 3);
- cfo.Write(&nFileLen, sizeof(UINT64));
- cfo.Write(pOutData, nOutLen);
- cfo.Close();
- }
- if ( pOutData )
- delete []pOutData;
- if ( pFileData)
- delete []pFileData;
- }
- /************************************************************************/
- /* 函数:[12/21/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void DES_DecryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpDecryFile)
- {
- if ( lpFile == NULL )
- return;
- if ( lpKey == NULL || lpVI == NULL )
- return;
- INT nKeyLen = strlen((char*)lpKey);
- if (nKeyLen != 8 )
- return;
- BYTE szvi[9] = {0};
- INT nVILen = strlen((char*)lpVI);
- if ( nVILen != 8 )
- return;
- memcpy(szvi, lpVI, 8);
- CFile cf;
- BYTE *pFileData = NULL;
- BYTE *pOutData = NULL;
- UINT64 nFileLen = 0;
- if ( cf.Open(lpFile, CFile::modeRead) )
- {
- nFileLen = cf.GetLength();
- pFileData = new BYTE[nFileLen];
- memset(pFileData, 0, nFileLen);
- cf.Read(pFileData,nFileLen);
- cf.Close();
- }
- else
- {
- DWORD dwError = GetLastError();
- TRACE("%d", dwError);
- return;
- }
- if ( strncmp((char*)pFileData, "<?>", 3) != 0 )
- {// 不是加密文件;
- delete []pFileData;
- return;
- }
- // 加密的长度,必定是8的倍数;
- UINT64 nOutLen = nFileLen-3-sizeof(UINT64);
- pOutData = new BYTE[nOutLen + 1];
- memset(pOutData, 0, nOutLen + 1);
- des_context des;
- if ( des_key_check_key_parity(lpKey) == 1)
- {
- des_key_set_parity(lpKey);
- if ( des_key_check_key_parity(lpKey) == 0 )
- {
- des_setkey_dec(&des, lpKey);
- }
- }
- else
- des_setkey_dec(&des, lpKey);
- des_crypt_cbc(&des, DES_DECRYPT, nOutLen, szvi, pFileData+3+sizeof(UINT64), pOutData);
- CFile cfo;
- memcpy(&nOutLen, pFileData+3, sizeof(UINT64)); // 获取实际文件长度;
- if ( cfo.Open(lpDecryFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfo.Write(pOutData, nOutLen);
- cfo.Close();
- }
- if ( pOutData )
- delete []pOutData;
- if (pFileData)
- delete []pFileData;
- }
|