DES的源程序

 DES的源程序应该不少见,但是公开的大多错误百出,包括一些出版的
书上也是很多错误。
这个C程序应该还算正确的,我测试了几百组数据,没出现错误,
解密的程序也可以返回,希望能对大家有些帮助。
首先是这个Encrypt.h文件的原代码:
//Encryption definition file
//To define all encryption constants and functions
#ifndef _ENCRYPT_H
#define _ENCRYPT_H
typedef union{
unsigned char byte[8];
struct{
unsigned long low;
unsigned long high;
}dword;
}SBitBlock;
// Declare Some "C" function, Doing the Main Part of DES encoding
#define word32 unsigned long
#define byte unsigned char
static void deskey (byte const *key, int decryptf, word32 *outbuf);
static void desDES (byte const inblock[8], byte outblock[8], word32 const *k
eys);
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:
// Functions:SetKey,Encrypt,Decrypt
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;
};
inline void CDES_Encoder::SetKey(const SBitBlock& key){
deskey((byte const*)(&key),0,(word32*)encrypt_k);
deskey((byte const*)(&key),1,(word32*)decrypt_k);
};
inline void CDES_Encoder::Encrypt(SBitBlock& destination, const SBitBlock& s
ource)const{
desDES((byte const*)(&source),(byte*)(&destination),(word32 const*)encrypt_
k);
};
inline void CDES_Encoder::Decrypt(SBitBlock& destination, const SBitBlock& s
ource)const{
desDES((byte const*)(&source),(byte*)(&destination),(word32 const*)decrypt_
k);
};
#undef word32
#undef byte
// 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:
// Functions: SetKey,SetSeed,GetSeed,Encrypt,Decrypt
void SetKey(const char* password);
inline void SetSeed(unsigned long lo,unsigned long hi);
inline void GetSeed(unsigned long& lo,unsigned long& hi)const;
int Encrypt(unsigned char destination[],const unsigned char source[], unsig
ned sourcesize)const;
int Decrypt(unsigned char destination[],const unsigned char source[], unsig
ned sourcesize);
};
inline void CPackage_Encoder::SetSeed(unsigned long lo,unsigned long hi){
seed.dword.low=lo;
seed.dword.high=hi;
};
inline void CPackage_Encoder::GetSeed(unsigned long& lo,unsigned long& hi)co
nst{
lo=seed.dword.low;
hi=seed.dword.high;
};
#endif //_ENCRYPT_H
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值