📄 des.h
字号:
/***************************************************************************
* Des.h
*
* Wed Jan 11 15:05:14 2006
* Copyright 2006 User
* Email
****************************************************************************/
#ifndef _DES_H
#define _DES_H
#include <string.h>
#include <stdio.h>
typedef struct _tagWORD32
{
public:
unsigned char byte[4];
_tagWORD32()
{
memset(byte,0,sizeof(byte));
}
_tagWORD32(const unsigned long input )
{
unsigned long test=1;
unsigned char * ptest=(unsigned char *)&test;
unsigned char *pinput=(unsigned char *)&input;
if(*ptest)
{
memcpy(byte,pinput,4);
}
else
{
memcpy(&byte[3],pinput,1);
pinput++;
memcpy(&byte[2],pinput,1);
pinput++;
memcpy(&byte[1],pinput,1);
pinput++;
memcpy(&byte[0],pinput,1);
}
}
_tagWORD32(const unsigned char * buffer,const long lsize )
{
memset(byte,0,sizeof(byte));
if(lsize>4)
memcpy(byte,buffer,4);
else
memcpy(byte,buffer,lsize);
}
_tagWORD32(unsigned char byte1,unsigned char byte2,unsigned char byte3,unsigned char byte4 )
{
byte[0]=byte1;
byte[1]=byte2;
byte[2]=byte3;
byte[3]=byte4;
}
operator char*()
{
return (char *)byte;
}
operator unsigned char*()
{
return (unsigned char *)byte;
}
/*
operator unsigned char()
{
return (unsigned char )byte[0];
}
operator WORD32()
{
return *this;
}
operator unsigned long()
{
return getlongvalue();
}
*/
public:
unsigned long getactualvalue()
{
return byte[0]+(byte[1]<<8)+(byte[2]<<16)+(byte[3]<<24);
}
static unsigned long getactualvalue(_tagWORD32 input)
{
return input.byte[0]+(input.byte[1]<<8)+(input.byte[2]<<16)+(input.byte[3]<<24);
}
unsigned long getosvalue()
{
unsigned long test=1;
unsigned char * ptest=(unsigned char *)&test;
if(*ptest)
{
return byte[0]+(byte[1]<<8)+(byte[2]<<16)+(byte[3]<<24);
}
else
{
return byte[3]+(byte[2]<<8)+(byte[1]<<16)+(byte[0]<<24);
}
}
_tagWORD32 & operator =(const unsigned long input )
{
unsigned long test=1;
unsigned char * ptest=(unsigned char *)&test;
unsigned char *pinput=(unsigned char *)&input;
if(*ptest)
{
memcpy(byte,pinput,4);
}
else
{
memcpy(&byte[3],pinput,1);
pinput++;
memcpy(&byte[2],pinput,1);
pinput++;
memcpy(&byte[1],pinput,1);
pinput++;
memcpy(&byte[0],pinput,1);
}
return *this;
}
_tagWORD32 & operator =(_tagWORD32 word32 )
{
byte[3]=word32.byte[3];
byte[2]=word32.byte[2];
byte[1]=word32.byte[1];
byte[0]=word32.byte[0];
return *this;
}
_tagWORD32 operator +(const unsigned long input )
{
unsigned long ltemp=input+getactualvalue();
return _tagWORD32(ltemp);
}
_tagWORD32 operator +( _tagWORD32 input )
{
unsigned long ltemp=input.getactualvalue()+getactualvalue();
return _tagWORD32(ltemp);
}
_tagWORD32 operator -(const unsigned long input )
{
unsigned long ltemp=getactualvalue()-input;
return _tagWORD32(ltemp);
}
_tagWORD32 operator -(_tagWORD32 input )
{
unsigned long ltemp=getactualvalue()-input.getactualvalue();
return _tagWORD32(ltemp);
}
_tagWORD32 operator &(const unsigned long input )
{
_tagWORD32 word32(input);
return _tagWORD32 (word32.byte[0] & byte[0], word32.byte[1] & byte[1],word32.byte[2] & byte[2],word32.byte[3] & byte[3]);
}
_tagWORD32 operator &(_tagWORD32 word32 )
{
return _tagWORD32 (word32.byte[0] & byte[0], word32.byte[1] & byte[1],word32.byte[2] & byte[2],word32.byte[3] & byte[3]);
}
_tagWORD32 operator | (const unsigned long input )
{
_tagWORD32 word32(input);
return _tagWORD32 (word32.byte[0] | byte[0], word32.byte[1] | byte[1],word32.byte[2] | byte[2],word32.byte[3] | byte[3]);
}
_tagWORD32 operator | (_tagWORD32 word32 )
{
return _tagWORD32 (word32.byte[0] | byte[0], word32.byte[1] | byte[1],word32.byte[2] | byte[2],word32.byte[3] | byte[3]);
}
_tagWORD32 operator <<(const long input )
{
unsigned long ltemp=getactualvalue();
ltemp=ltemp<<input;
return _tagWORD32 (ltemp);
}
_tagWORD32 operator >>(const long input )
{
unsigned long ltemp=getactualvalue();
ltemp=ltemp>>input;
return _tagWORD32 (ltemp);
}
_tagWORD32 operator ^(const long input )
{
_tagWORD32 word32(input);
return _tagWORD32 (word32.byte[0]^byte[0],word32.byte[1]^byte[1],word32.byte[2]^byte[2],word32.byte[3]^byte[3]);
}
_tagWORD32 operator ^(const _tagWORD32 input )
{
return _tagWORD32 (input.byte[0]^byte[0],input.byte[1]^byte[1],input.byte[2]^byte[2],input.byte[3]^byte[3]);
}
_tagWORD32 & operator ^=(const long input)
{
_tagWORD32 word32(input);
byte[0]^=word32.byte[0];
byte[1]^=word32.byte[1];
byte[2]^=word32.byte[2];
byte[3]^=word32.byte[3];
return *this;
}
_tagWORD32 & operator ^=(const _tagWORD32 input)
{
byte[0]^=input.byte[0];
byte[1]^=input.byte[1];
byte[2]^=input.byte[2];
byte[3]^=input.byte[3];
return *this;
}
_tagWORD32 & operator +=(const unsigned long input )
{
const unsigned long ltemp=input+getactualvalue();
_tagWORD32 word32(ltemp);
byte[0]=word32.byte[0];
byte[1]=word32.byte[1];
byte[2]=word32.byte[2];
byte[3]=word32.byte[3];
return *this;
}
_tagWORD32 & operator -=(const unsigned long input )
{
const unsigned long ltemp=getactualvalue()-input;
_tagWORD32 word32(ltemp);
byte[0]=word32.byte[0];
byte[1]=word32.byte[1];
byte[2]=word32.byte[2];
byte[3]=word32.byte[3];
return * this;
}
_tagWORD32 & operator |=(const unsigned long input )
{
_tagWORD32 word32(input);
byte[3]|=word32.byte[3];
byte[2]|=word32.byte[2];
byte[1]|=word32.byte[1];
byte[0]|=word32.byte[0];
return *this;
}
_tagWORD32 & operator |=(const _tagWORD32 word32 )
{
byte[3]|=word32.byte[3];
byte[2]|=word32.byte[2];
byte[1]|=word32.byte[1];
byte[0]|=word32.byte[0];
return *this;
}
const _tagWORD32 & operator &=(const unsigned long input )
{
_tagWORD32 word32(input);
byte[0]=word32.byte[0] & byte[0];
byte[1]=word32.byte[1] & byte[1];
byte[2]=word32.byte[2] & byte[2];
byte[3]=word32.byte[3] & byte[3];
return *this;
}
const _tagWORD32 & operator &=(_tagWORD32 word32 )
{
byte[0]=word32.byte[0] & byte[0];
byte[1]=word32.byte[1] & byte[1];
byte[2]=word32.byte[2] & byte[2];
byte[3]=word32.byte[3] & byte[3];
return *this;
}
_tagWORD32 & operator <<= (const unsigned long input )
{
unsigned long ltemp=getactualvalue();
ltemp=ltemp<<input;
unsigned long test=1;
unsigned char * ptest=(unsigned char *)&test;
unsigned char *pinput=(unsigned char *)<emp;
if(*ptest)
{
memcpy(byte,pinput,4);
}
else
{
memcpy(&byte[3],pinput,1);
pinput++;
memcpy(&byte[2],pinput,1);
pinput++;
memcpy(&byte[1],pinput,1);
pinput++;
memcpy(&byte[0],pinput,1);
}
return *this;
}
_tagWORD32 & operator >>= (const unsigned long input )
{
unsigned long ltemp=getactualvalue();
ltemp=ltemp>>input;
unsigned long test=1;
unsigned char * ptest=(unsigned char *)&test;
unsigned char *pinput=(unsigned char *)<emp;
if(*ptest)
{
memcpy(byte,pinput,4);
}
else
{
memcpy(&byte[3],pinput,1);
pinput++;
memcpy(&byte[2],pinput,1);
pinput++;
memcpy(&byte[1],pinput,1);
pinput++;
memcpy(&byte[0],pinput,1);
}
return *this;
}
}WORD32;
typedef struct _tagDWORD64
{
WORD32 low;
WORD32 high;
}DWORD64;
typedef struct _tagSBitBlock
{
DWORD64 dword;
// unsigned char byte[8];
unsigned char & operator [](unsigned lindex)
{
if((lindex>=0)&&(lindex<=3))
return dword.low.byte[lindex];
if((lindex>=4)&&(lindex<=7))
return dword.high.byte[lindex-4];
return dword.low.byte[0];
}
public:
operator WORD32*()
{
return &dword.low;
}
}SBitBlock;
extern void deskey (unsigned char const *key, int decryptf, WORD32 *outbuf);
extern void desDES (unsigned char const inblock[8], unsigned char outblock[8], WORD32 const *keys);
class CDES_Encoder
{
private:
// Data:16 48-bit k[i], calculated from "key", use low 6-bit in each byte
SBitBlock encrypt_k[16],decrypt_k[16];
public:
inline void SetKey(const SBitBlock& key); //calculate k[i]
inline void Encrypt(SBitBlock& destination, const SBitBlock& source)const;
inline void Decrypt(SBitBlock& destination, const SBitBlock& source)const;
};
// Below defines the package encryption and decryption class
class CPackage_Encoder
{
private:
// Data: a DES coder and package seed
CDES_Encoder theDEScoder;
SBitBlock seed;
public:
void SetKey(const unsigned char* password);
//edit on 2004-2-23
int encode(unsigned char* scr,int nslen,unsigned char**des,int* ndlen );
int decode(unsigned char* scr,int nslen,unsigned char**des,int* ndlen );
private:
int Encrypt(unsigned char destination[],const unsigned char source[], unsigned sourcesize)const;
int Decrypt(unsigned char destination[],const unsigned char source[], unsigned sourcesize);
inline void SetSeed(unsigned long lo,unsigned long hi);
inline void GetSeed(unsigned long& lo,unsigned long& hi) const;
// CString SEncrypt(CString);
// CString SDecrypt(CString);
};
#endif /* _DES_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -