- 1、本文档共23页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
基于C 的DES加密算法实现
今天偶尔想到数据加密技术,刚才简单讨论了一下MD5和SHA,这里顺便提一下双向加密的标准DES。以前在一个宽带流媒体服务器项目中用到了DES加密,虽然使用环境相对单纯,但算法实现本身是具有普遍意义的。下述程序是很久以前在RSA公司的网站找到的,效率很高,稳定性较好,便一直在实践中使用。首先在头文件中定义一个DES类://des.h#ifndef __DES_H #define __DES_H class DES{public: // Encrypt/decrypt the data in data, according to the key. // Caller is responsible for confirming the buffer size of data // points to is 8*blocks bytes. // The data encrypted/decrypted is stored in data. // The return code is 1:success, other:failed. int encrypt ( unsigned char key[8], unsigned char* data, int blocks = 1 ); int decrypt ( unsigned char key[8], unsigned char* data, int blocks = 1 ); // Encrypt/decrypt any size data,according to a special method. // Before calling yencrypt, copy data to a new buffer with size // calculated by extend. int yencrypt ( unsigned char key[8], unsigned char* data, int size ); int ydecrypt ( unsigned char key[8], unsigned char* in, int blocks, int* size = 0 ); int extend ( int size ) { return (size/8+1)*8; }; private: void des(unsigned char* in, unsigned char* out, int blocks); void des_block(unsigned char* in, unsigned char* out); private: unsigned long KnL[32];enum Mode { ENCRYPT, DECRYPT }; void deskey(unsigned char key[8], Mode md); void usekey(unsigned long *); void cookey(unsigned long *); private: void scrunch(unsigned char *, unsigned long *); void unscrun(unsigned long *, unsigned char *); void desfunc(unsigned long *, unsigned long *); private: static unsigned char Df_Key[24]; static unsigned short bytebit[8]; static unsigned long bigbyte[24]; static unsigned char pc1[56]; static unsigned char totrot[16]; static unsigned char pc2[48]; static unsigned long SP1[64]; static unsigned long SP2[64]; static unsigned long SP3[64]; static unsigned long SP4[64]; static unsigned long SP5[64]; static unsigned long SP6[64]; static unsigned long SP7[64]; static unsigned long SP8[64]; }; #endif 而后,具体实现DES类://des.cpp#include string.h#include Sinodes.hi
文档评论(0)