moqingsong
论坛版主
论坛版主
  • 注册日期2002-04-07
  • 最后登录2011-02-03
  • 粉丝0
  • 关注0
  • 积分74分
  • 威望71点
  • 贡献值0点
  • 好评度10点
  • 原创分0分
  • 专家分0分
阅读:1366回复:0

DES算法的C源程序

楼主#
更多 发布于:2002-05-29 10:05
〖Encrypt〗
 
上一篇|下一篇|回文章  分类讨论区   全部讨论区   本讨论区  
 
  发信人: blizzard (大炮), 信区: Encrypt
标  题: DES算法的C源程序
发信站: 武汉白云黄鹤站 (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
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, 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
        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


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

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


 
 
 
  
 
上一篇|下一篇|回文章  分类讨论区   全部讨论区   本讨论区  
 
Copyright(c)2000 白云黄鹤BBS站 All Rights Reserved.  
按第一贴的“给分”键,给分。
游客

返回顶部