荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Lg (创造人生的传奇), 信区: Linux
标  题: DES算法的C源程序(fwd)
发信站: BBS 荔园晨风站 (Fri Jan 21 22:33:37 2000), 站内信件

【 以下文字转载自 Hacker 讨论区 】
【 原文由 bstone 所发表 】
发信人: blizzard (大炮), 信区: Encrypt
标  题: DES算法的C源程序(fwd)
发信站: 武汉白云黄鹤站 (Thu Jan  6 12:35:19 2000), 站内信件

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
#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)cont
;
        inline void Decrypt(SBitBlock& destination, const SBitBlock& source)cont
;
};
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*)encrp
        desDES((byte const*)(&source),(byte*)(&destination),(word32 const*)encrp
t_
k);
};
inline void CDES_Encoder::Decrypt(SBitBlock& destination, const SBitBlock& s
ource)const{
        desDES((byte const*)(&source),(byte*)(&destination),(word32 const*)decrp
t_
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;
        inline void GetSeed(unsigned long& lo,unsigned long& hi)const;
        int Encrypt(unsigned char destination[],const unsigned char source[], us
ig
ned sourcesize)const;
        int Decrypt(unsigned char destination[],const unsigned char source[], us
ig
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


--
I'm Sailing;  I'm Flying
       Through The Dark Night;  Far Away
ig
I'm Dying;    Forever Crying
          To Be With You;     To Be Free!


※ 来源:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: 203.95.7.153]

--
☆ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: bbs@192.168.28.28]
--
※ 转载:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 210.39.3.97]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店