69,369
社区成员
发帖
与我相关
我的任务
分享
void des3key(unsigned char *, short);
void use3key(unsigned long *);
cp3key(unsigned long *);
void make3key(char *, unsigned char *);
extern void deskey(unsigned char *, short);
/* hexkey[8] MODE
* Sets the internal key register according to the hexadecimal
* key contained in the 8 bytes of hexkey, according to the DES,
* for encryption or decryption according to MODE.
*/
extern void usekey(unsigned long *);
/* cookedkey[32]
* Loads the internal key register with the data in cookedkey.
*/
extern void cpkey(unsigned long *);
/* cookedkey[32]
* Copies the contents of the internal key register into the storage
* located at &cookedkey[0].
*/
extern void des(unsigned char *, unsigned char *);
/* from[8] to[8]
* Encrypts/Decrypts (according to the key currently loaded in the
* internal key register) one block of eight bytes at address 'from'
* into the block at address 'to'. They can be the same.
*/
#else /* D3_DES too */
#define useDkey(a) use3key((a))
#define cpDkey(a) cp3key((a))
extern void des3key(unsigned char *, short);
/* hexkey[24] MODE
* Sets the internal key registerS according to the hexadecimal
* keyS contained in the 24 bytes of hexkey, according to the DES,
* for DOUBLE encryption` or decryption according to MODE.
*/
extern void use3key(unsigned long *);
/* cookedkey[96]
* Loads the 3 internal key registerS with the data in cookedkey.
*/
extern void cp3key(unsigned long *);
/* cookedkey[96]
* Copies the contents of the 3 internal key registerS into the storage
* located at &cookedkey[0].
*/
extern void make3key(char *, unsigned char *);
/* *password, triple-length key[24]
* With a triple-length default key, this routine hashes a NULL-terminated
* string into a twenty-four-byte random-looking key, suitable for use with
* the des3key() routine.
*/
#endif /* D3_DES */
//
BOOL DesEncrypt(LPCTSTR szKey, LPBYTE lpbySrc, LPBYTE lpbyDest, int iLength)
{
BYTE byKey[8] = {0};
CHAR szTempKey[20];
strcpy(szTempKey, szKey);
makekey(szTempKey, byKey);
deskey(byKey, EN0);
for (int i = 0; i < iLength; i += 8)
des(lpbySrc + i, lpbyDest + i);
return TRUE;
}
BOOL DesDecrypt(LPCTSTR szKey, LPBYTE lpbySrc, LPBYTE lpbyDest, int iLength)
{
BYTE byKey[8] = {0};
CHAR szTempKey[20];
strcpy(szTempKey, szKey);
makekey(szTempKey, byKey);
deskey(byKey, DE1);
for (int i = 0; i < iLength; i += 8)
des(lpbySrc + i, lpbyDest + i);
return TRUE;
}
所谓的des3就是把调用加密或解密的同一个函数三次
//
我也用的这个,下面是我的代码
//密钥赋值
key[0] = 0x5D;
key[1] = 0x68;
key[2] = 0xCE;
key[3] = 0xC2;
key[4] = 0x61;
key[5] = 0x86;
key[6] = 0x02;
key[7] = 0xF1;
deskey(key, DE1);
//解密
des(pwdin, pwdout);
//解出来的pwdout密码明文后面的字符会被一些0x01、0x02之类的控制字符填充,删之
for (int i=0; i<strlen(pwdout); i++)
{
if (pwdout[i] < 32)
pwdout[i] = 0x00;
}
另外,我只用了解密部分。加密部分好像不能得到正确的值。
//
des对数据进行8位加密
d2des和d3des是对数据进行16位加密和24位加密
和3重des无关
3重des是指用2个的key对数据进行加密/解密/加密3次操作
//
make3key( hexkey, make );
des3key( make, 0 );
//cp3key(el);
//use3key(el);
D3des( hi, date );