求C或C++的DES加密算法

求C或C++的DES加密算法,第1张

/ d3desh -

Headers and defines for d3desc

Graven Imagery, 1992

Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge

(GEnie : OUTER; CIS : [71755,204])

/

#define D2_DES / include double-length support /

#define D3_DES / include triple-length support /

#ifdef D3_DES

#ifndef D2_DES

#define D2_DES / D2_DES is needed for D3_DES /

#endif

#endif

#define EN0 0 / MODE == encrypt /

#define DE1 1 / MODE == decrypt /

/ A useful alias on 68000-ish machines, but NOT USED HERE /

typedef union {

unsigned long blok[2];

unsigned short word[4];

unsigned char byte[8];

} M68K;

extern void deskey(unsigned char , short);

/ hexkey[8] MODE

Sets the internal key register according to the hexadecimal

key contained in the 8 bytes of hexkey, according to the DES,

for encryption or decryption according to MODE

/

extern void usekey(unsigned long );

/ cookedkey[32]

Loads the internal key register with the data in cookedkey

/

extern void cpkey(unsigned long );

/ cookedkey[32]

Copies the contents of the internal key register into the storage

located at &cookedkey[0]

/

extern void des(unsigned char , unsigned char );

/ from[8] to[8]

Encrypts/Decrypts (according to the key currently loaded in the

internal key register) one block of eight bytes at address 'from'

into the block at address 'to' They can be the same

/

#ifdef D2_DES

#define desDkey(a,b) des2key((a),(b))

extern void des2key(unsigned char , short);

/ hexkey[16] MODE

Sets the internal key registerS according to the hexadecimal

keyS contained in the 16 bytes of hexkey, according to the DES,

for DOUBLE encryption or decryption according to MODE

NOTE: this clobbers all three key registers!

/

extern void Ddes(unsigned char , unsigned char );

/ from[8] to[8]

Encrypts/Decrypts (according to the keyS currently loaded in the

internal key registerS) one block of eight bytes at address 'from'

into the block at address 'to' They can be the same

/

extern void D2des(unsigned char , unsigned char );

/ from[16] to[16]

Encrypts/Decrypts (according to the keyS currently loaded in the

internal key registerS) one block of SIXTEEN bytes at address 'from'

into the block at address 'to' They can be the same

/

extern void makekey(char , unsigned char );

/ password, single-length key[8]

With a double-length default key, this routine hashes a NULL-terminated

string into an eight-byte random-looking key, suitable for use with the

deskey() routine

/

#define makeDkey(a,b) make2key((a),(b))

extern void make2key(char , unsigned char );

/ password, double-length key[16]

With a double-length default key, this routine hashes a NULL-terminated

string into a sixteen-byte random-looking key, suitable for use with the

des2key() routine

/

#ifndef D3_DES / D2_DES only /

#define useDkey(a) use2key((a))

#define cpDkey(a) cp2key((a))

extern void use2key(unsigned long );

/ cookedkey[64]

Loads the internal key registerS with the data in cookedkey

NOTE: this clobbers all three key registers!

/

extern void cp2key(unsigned long );

/ cookedkey[64]

Copies the contents of the internal key registerS into the storage

located at &cookedkey[0]

/

#else / D3_DES too /

#define useDkey(a) use3key((a))

#define cpDkey(a) cp3key((a))

extern void des3key(unsigned char , short);

/ hexkey[24] MODE

Sets the internal key registerS according to the hexadecimal

keyS contained in the 24 bytes of hexkey, according to the DES,

for DOUBLE encryption or decryption according to MODE

/

extern void use3key(unsigned long );

/ cookedkey[96]

Loads the 3 internal key registerS with the data in cookedkey

/

extern void cp3key(unsigned long );

/ cookedkey[96]

Copies the contents of the 3 internal key registerS into the storage

located at &cookedkey[0]

/

extern void make3key(char , unsigned char );

/ password, triple-length key[24]

With a triple-length default key, this routine hashes a NULL-terminated

string into a twenty-four-byte random-looking key, suitable for use with

the des3key() routine

/

#endif / D3_DES /

#endif / D2_DES /

package des;

import javaio;

import javanio;

import javaniochannelsFileChannel;

public class FileDES{

private static final boolean enc=true; //加密

private static final boolean dec=false; //解密

private String srcFileName;

private String destFileName;

private String inKey;

private boolean actionType;

private File srcFile;

private File destFile;

private Des des;

private void analyzePath(){

String dirName;

int pos=srcFileNamelastIndexOf("/");

dirName=srcFileNamesubstring(0,pos);

File dir=new File(dirName);

if (!direxists()){

Systemerrprintln(dirName+" is not exist");

Systemexit(1);

}else if(!dirisDirectory()){

Systemerrprintln(dirName+" is not a directory");

Systemexit(1);

}

pos=destFileNamelastIndexOf("/");

dirName=destFileNamesubstring(0,pos);

dir=new File(dirName);

if (!direxists()){

if(!dirmkdirs()){

Systemoutprintln ("can not creat directory:"+dirName);

Systemexit(1);

}

}else if(!dirisDirectory()){

Systemerrprintln(dirName+" is not a directory");

Systemexit(1);

}

}

private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{

long byteLeft=channelsize()-channelposition();

if(byteLeft==0L)

return -1;

bufposition(0);

buflimit(bufposition()+(byteLeft<8 (int)byteLeft :8));

return channelread(buf);

}

private void file_operate(boolean flag){

des=new Des(inKey);

FileOutputStream outputFile=null;

try {

outputFile=new FileOutputStream(srcFile,true);

}catch (javaioFileNotFoundException e) {

eprintStackTrace(Systemerr);

}

FileChannel outChannel=outputFilegetChannel();

try{

if(outChannelsize()%2!=0){

ByteBuffer bufTemp=ByteBufferallocate(1);

bufTempput((byte)32);

bufTempflip();

outChannelposition(outChannelsize());

outChannelwrite(bufTemp);

bufTempclear();

}

}catch(Exception ex){

exprintStackTrace(Systemerr);

Systemexit(1);

}

FileInputStream inFile=null;

try{

inFile=new FileInputStream(srcFile);

}catch(javaioFileNotFoundException e){

eprintStackTrace(Systemerr);

//Systemexit(1);

}

outputFile=null;

try {

outputFile=new FileOutputStream(destFile,true);

}catch (javaioFileNotFoundException e) {

eprintStackTrace(Systemerr);

}

FileChannel inChannel=inFilegetChannel();

outChannel=outputFilegetChannel();

ByteBuffer inBuf=ByteBufferallocate(8);

ByteBuffer outBuf=ByteBufferallocate(8);

try{

String srcStr;

String destStr;

while(true){

if (replenish(inChannel,inBuf)==-1) break;

srcStr=((ByteBuffer)(inBufflip()))asCharBuffer()toString();

inBufclear();

if (flag)

destStr=desenc(srcStr,srcStrlength());

else

destStr=desdec(srcStr,srcStrlength());

outBufclear();

if (destStrlength()==4){

for (int i = 0; i<4; i++) {

outBufputChar(destStrcharAt(i));

}

outBufflip();

}else{

outBufposition(0);

outBuflimit(2destStrlength());

for (int i = 0; i<destStrlength(); i++) {

outBufputChar(destStrcharAt(i));

}

outBufflip();

}

try {

outChannelwrite(outBuf);

outBufclear();

}catch (javaioIOException ex) {

exprintStackTrace(Systemerr);

}

}

Systemoutprintln (inChannelsize());

Systemoutprintln (outChannelsize());

Systemoutprintln ("EoF reached");

inFileclose();

outputFileclose();

}catch(javaioIOException e){

eprintStackTrace(Systemerr);

Systemexit(1);

}

}

public FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){

thissrcFileName=srcFileName;

thisdestFileName=destFileName;

thisactionType=actionType;

analyzePath();

srcFile=new File(srcFileName);

destFile=new File(destFileName);

thisinKey=inKey;

if (actionType==enc)

file_operate(enc);

else

file_operate(dec);

}

public static void main(String[] args){

String file1=SystemgetProperty("userdir")+"/111doc";

String file2=SystemgetProperty("userdir")+"/222doc";

String file3=SystemgetProperty("userdir")+"/333doc";

String passWord="1234ABCD";

FileDES fileDes=new FileDES(file1,file2,passWord,true);

FileDES fileDes1=new FileDES(file2,file3,passWord,false);

}

package des;

import javaio;

import javanio;

import javaniochannelsFileChannel;

public class FileDES{

private static final boolean enc=true; //加密

private static final boolean dec=false; //解密

private String srcFileName;

private String destFileName;

private String inKey;

private boolean actionType;

private File srcFile;

private File destFile;

private Des des;

private void analyzePath(){

String dirName;

int pos=srcFileNamelastIndexOf("/");

dirName=srcFileNamesubstring(0,pos);

File dir=new File(dirName);

if (!direxists()){

Systemerrprintln(dirName+" is not exist");

Systemexit(1);

}else if(!dirisDirectory()){

Systemerrprintln(dirName+" is not a directory");

Systemexit(1);

}

pos=destFileNamelastIndexOf("/");

dirName=destFileNamesubstring(0,pos);

dir=new File(dirName);

if (!direxists()){

if(!dirmkdirs()){

Systemoutprintln ("can not creat directory:"+dirName);

Systemexit(1);

}

}else if(!dirisDirectory()){

Systemerrprintln(dirName+" is not a directory");

Systemexit(1);

}

}

private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{

long byteLeft=channelsize()-channelposition();

if(byteLeft==0L)

return -1;

bufposition(0);

buflimit(bufposition()+(byteLeft<8 (int)byteLeft :8));

return channelread(buf);

}

private void file_operate(boolean flag){

des=new Des(inKey);

FileOutputStream outputFile=null;

try {

outputFile=new FileOutputStream(srcFile,true);

}catch (javaioFileNotFoundException e) {

eprintStackTrace(Systemerr);

}

FileChannel outChannel=outputFilegetChannel();

try{

if(outChannelsize()%2!=0){

ByteBuffer bufTemp=ByteBufferallocate(1);

bufTempput((byte)32);

bufTempflip();

outChannelposition(outChannelsize());

outChannelwrite(bufTemp);

bufTempclear();

}

}catch(Exception ex){

exprintStackTrace(Systemerr);

Systemexit(1);

}

FileInputStream inFile=null;

try{

inFile=new FileInputStream(srcFile);

}catch(javaioFileNotFoundException e){

eprintStackTrace(Systemerr);

//Systemexit(1);

}

outputFile=null;

try {

outputFile=new FileOutputStream(destFile,true);

}catch (javaioFileNotFoundException e) {

eprintStackTrace(Systemerr);

}

FileChannel inChannel=inFilegetChannel();

outChannel=outputFilegetChannel();

ByteBuffer inBuf=ByteBufferallocate(8);

ByteBuffer outBuf=ByteBufferallocate(8);

try{

String srcStr;

String destStr;

while(true){

if (replenish(inChannel,inBuf)==-1) break;

srcStr=((ByteBuffer)(inBufflip()))asCharBuffer()toString();

inBufclear();

if (flag)

destStr=desenc(srcStr,srcStrlength());

else

destStr=desdec(srcStr,srcStrlength());

outBufclear();

if (destStrlength()==4){

for (int i = 0; i<4; i++) {

outBufputChar(destStrcharAt(i));

}

outBufflip();

}else{

outBufposition(0);

outBuflimit(2destStrlength());

for (int i = 0; i<destStrlength(); i++) {

outBufputChar(destStrcharAt(i));

}

outBufflip();

}

try {

outChannelwrite(outBuf);

outBufclear();

}catch (javaioIOException ex) {

exprintStackTrace(Systemerr);

}

}

Systemoutprintln (inChannelsize());

Systemoutprintln (outChannelsize());

Systemoutprintln ("EoF reached");

inFileclose();

outputFileclose();

}catch(javaioIOException e){

eprintStackTrace(Systemerr);

Systemexit(1);

}

}

public FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){

thissrcFileName=srcFileName;

thisdestFileName=destFileName;

thisactionType=actionType;

analyzePath();

srcFile=new File(srcFileName);

destFile=new File(destFileName);

thisinKey=inKey;

if (actionType==enc)

file_operate(enc);

else

file_operate(dec);

}

public static void main(String[] args){

String file1=SystemgetProperty("userdir")+"/111doc";

String file2=SystemgetProperty("userdir")+"/222doc";

String file3=SystemgetProperty("userdir")+"/333doc";

String passWord="1234ABCD";

FileDES fileDes=new FileDES(file1,file2,passWord,true);

FileDES fileDes1=new FileDES(file2,file3,passWord,false);

}

DES(Data Encryption Standard)满足了国家标准局欲达到的4个目的:提供高质量的数据保护,防止数据未经授权的泄露和未被察觉的修改;具有相当高的复杂性,使得破译的开销超过可能获得的利益,同时又要便于理解和掌握;

DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位,首先,DES把输入的64位数据块按位重新组合,并把输出分为L0、R0两部分,每部分各长32位,并进行前后置换(输入的第58位换到第一位,第50位换到第2位,依此类推,最后一位是原来的第7位),最终由L0输出左32位,R0输出右32位,根据这个法则经过16次迭代运算后,得到L16、R16,将此作为输入,进行与初始置换相反的逆置换,即得到密文输出。

DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密,如果Mode为加密,则用Key去把数据Data进行加密,生成Data的密码形式作为DES的输出结果;如Mode为解密,则用Key去把密码形式的数据Data解密,还原为Data的明码形式作为DES的输出结果。在使用DES时,双方预先约定使用的”密码”即Key,然后用Key去加密数据;接收方得到密文后使用同样的Key解密得到原数据,这样便实现了安全性较高的数据传输。

//

/-文件名:desh /

/- /

/-功能: 实现DES加密算法的加密解密功能 /

//

typedef int INT32;

typedef char INT8;

typedef unsigned char ULONG8;

typedef unsigned short ULONG16;

typedef unsigned long ULONG32;

/如果采用c++编译器的话采用如下宏定义

#define DllExport extern "C" __declspec(dllexport)

/

#define DllExport __declspec(dllexport)

/加密接口函数/

DllExport INT32 DdesN(ULONG8 data, ULONG8 key, ULONG32 n_key,ULONG32 readlen);

DllExport INT32 desN(ULONG8 data, ULONG8 key, ULONG32 n_key,ULONG32 readlen);

DllExport INT32 des3(ULONG8 data, ULONG8 key,ULONG32 n ,ULONG32 readlen);

DllExport INT32 Ddes3(ULONG8 data,ULONG8 key,ULONG32 n ,ULONG32 readlen);

DllExport INT32 des(ULONG8 data, ULONG8 key,INT32 readlen);

DllExport INT32 Ddes(ULONG8 data,ULONG8 key,INT32 readlen);

/

/-文件名:desc /

/- /

/-功能: 实现DES加密算法的加密解密功能 /

///

#include <stdlibh>

#include <stdioh>

#include <stringh>

#include <memoryh>

#include <malloch>

#include "desh"

#define SUCCESS 0

#define FAIL -1

#define READFILESIZE 512

#define WZ_COMMEND_NUM 4

#define WZUSEHELPNUM 19

#define DESONE 1

#define DESTHREE 2

#define DESMULTI 3

INT8 WZ_Commend_Help[] =

{

"基于DES的加密解密工具v10 ",/0/

"追求卓越,勇于创新 ",

"----著者 : 吴真--- ",

" "

};

INT8 WZ_USE_HELP[]={

"输入5+n个参数:",

"\t1可执行文件名 exe",

"\t2 *** 作类型 1:一层加密;2:一层解密;",

"\t\t13:N层单密钥加密;23:N层单密钥解密;",

"\t\t39:N层多密钥加密;49:N层多密钥解密",

"\t3读出数据的文件名txt",

"\t4写入数据的文件名txt",

"\t5密钥(8字节例如:wuzhen12)",

"\t[6]N层单密钥的层数或者二层加密|解密密钥",

"\t[7]三层加密|解密密钥",

"\t[8] ",

"\t[N]N层加密|解密密钥",

"\t 例1: des 1 1txt 2txt 12345678",

"\t : des 2 2txt 3txt 12345678",

"\t 例2: des 13 1txt 2txt tiantian 5",

"\t : des 23 2txt 3txt tiantian 5",

"\t 例3: des 39 1txt 2txt 12345678 tiantian gaoxinma",

"\t : des 49 2txt 3txt 12345678 tiantian gaoxinma",

""

};

INT32 hextofile( ULONG8 buf ,FILE writefile, ULONG32 length);/以16进制写入文件/

INT32 encodehex(ULONG8 tobuf,ULONG8 frombuf,ULONG32 len);/16进制解码/

INT32 file_enc(FILE readfile,FILE writefile,

ULONG8 key,ULONG32 keynum,

ULONG8 superkey,ULONG32 n_superkey,

ULONG8 flag);

INT32 file_dec(FILE readfile,FILE writefile,

ULONG8 key,ULONG32 keynum,

ULONG8 superkey,ULONG32 n_superkey,

ULONG8 flag);

void wz_print_help();

INT32 main(INT32 argc,INT8 argv[])

{

INT8 FILENAME1,FILENAME2;

FILE fp, fp2;

ULONG8 key ;

ULONG8 superkey ;/n层加密解密密钥/

ULONG8 n_superkey ;

ULONG32 num;

if ( argc >= 5 && (atoi(argv[1]) == 39 || atoi(argv[1]) == 49 ) )

{

n_superkey = argc - 4 ;

superkey = ( INT8 )calloc(1, n_superkeysizeof( void ) ) ;

for ( num = 0 ; num < n_superkey ; num++)

{

superkey[num] = argv[4+num] ;

}

}

else if ( argc == 6 && (atoi(argv[1]) == 13 || atoi(argv[1]) == 23 ) && (atoi(argv[5])) > 0)

{

}

else if ( argc == 5 && ( atoi(argv[1]) == 1 || atoi(argv[1]) == 2 ))

{

}

else

{

wz_print_help();

return FAIL;

}

FILENAME1 = argv[2];

FILENAME2 = argv[3];

if ((fp= fopen(FILENAME1,"rb")) == NULL || (fp2 = fopen(FILENAME2,"wb"))==NULL)

{

printf("Can't open file\n");

return FAIL;

}

key = argv[4] ;

switch( atoi(argv[1] ))

{

case 1: /加密/

file_enc(fp,fp2,key,0, NULL,0, DESONE);

printf("\n \tDES 一层加密完毕,密文存于%s文件\n",FILENAME2);

break;

case 2:

file_dec(fp,fp2,key,0, NULL, 0,DESONE);

printf("\n \tDES 一层解密完毕,密文存于%s文件\n",FILENAME2);

break;

case 13:

file_enc(fp,fp2,key,atoi(argv[5]),NULL,0,DESTHREE);

printf("\n \tDES %u层单密钥加密完毕,密文存于%s文件\n",atoi(argv[5]),FILENAME2);

break;

case 23:

file_dec(fp,fp2,key,atoi(argv[5]),NULL,0,DESTHREE);

printf("\n \tDES %u层单密钥解密完毕,密文存于%s文件\n",atoi(argv[5]),FILENAME2);

break;

case 39:

file_enc(fp,fp2,NULL,0,superkey,n_superkey,DESMULTI);

printf("\n \tDES 多密钥加密完毕,密文存于%s文件\n",FILENAME2);

free(superkey);

superkey = NULL;

break;

case 49:

file_dec(fp,fp2,NULL,0,superkey,n_superkey,DESMULTI);

printf("\n \tDES 多密钥加密完毕,密文存于%s文件\n",FILENAME2);

free(superkey);

superkey = NULL;

break;

default:

printf("请选择是加密|解密 plese choose encrypt|deencrypt\n");

break;

}

fclose(fp);

fclose(fp2);

return SUCCESS;

}

void wz_print_help()

{

INT32 i ;

printf("\t");

for ( i = 0 ; i < 22 ; i++)

{

printf("%c ",5);

}

printf("\n");

for( i = 0 ; i < WZ_COMMEND_NUM ; i++)

{

printf("\t%c\t%s %c\n",5,WZ_Commend_Help[i],5);

}

printf("\t");

for ( i = 0 ; i < 22 ; i++)

{

printf("%c ",5);

}

printf("\n");

for( i = 0 ; i < WZUSEHELPNUM ; i++)

{

printf("\t%s\n",WZ_USE_HELP[i]);

}

return ;

}

INT32 file_enc(FILE readfile,FILE writefile,

ULONG8 key,ULONG32 keynum,

ULONG8 superkey,ULONG32 n_superkey,

ULONG8 flag)

{

INT32 filelen = 0,readlen = 0,writelen = 0;

ULONG32 totalfilelen = 0 ;/统计实际的文件的长度/

ULONG8 readbuf[READFILESIZE] = { 0 };

filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );

while( filelen == READFILESIZE )

{

totalfilelen += READFILESIZE;

switch(flag)

{

case DESONE:

des( readbuf,key,READFILESIZE);

break;

case DESTHREE:

des3( readbuf, key ,keynum,READFILESIZE);

break;

case DESMULTI:

desN( readbuf, superkey ,n_superkey,READFILESIZE);

break;

}

hextofile( readbuf, writefile, READFILESIZE );/以16进制形式写入文件/

memset(readbuf,0,READFILESIZE);

filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );

}

/这是从文件中读出的最后一批数据,长度可能会等于0,所以要先判断/

if ( filelen > 0 )

{

/如果从文件中读出的长度不等于0,那么肯定有8个字节以上的空间

文件长度存在最后8个字节中/

totalfilelen += filelen;

memcpy( &readbuf[READFILESIZE-8], (ULONG8)&totalfilelen,4);

switch(flag)

{

case DESONE:

des( readbuf,key,READFILESIZE);

break;

case DESTHREE:

des3( readbuf, key ,keynum,READFILESIZE);

break;

case DESMULTI:

desN( readbuf, superkey ,n_superkey,READFILESIZE);

break;

}

hextofile( readbuf, writefile,READFILESIZE );/以16进制形式写入文件/

memset(readbuf,0 ,READFILESIZE);

}

else /filelen == 0/

{

memcpy( &readbuf[0], (ULONG8)&totalfilelen,4);

switch(flag)

{

case DESONE:

des( readbuf,key,8);

break;

case DESTHREE:

des3( readbuf, key ,keynum,8);

break;

case DESMULTI:

desN( readbuf, superkey ,n_superkey,8);

break;

}

hextofile( readbuf, writefile, 8);/以16进制形式写入文件/

}

return SUCCESS;

}

INT32 file_dec(FILE readfile,FILE writefile,

ULONG8 key,ULONG32 keynum,

ULONG8 superkey,ULONG32 n_superkey,

ULONG8 flag)

{

INT32 filelen = 0,readlen = 0,writelen = 0;

ULONG32 totalfilelen = 0 ;/统计实际的文件的长度/

INT32 num = 0;

ULONG8 readbuf[READFILESIZE] = { 0 };

ULONG8 sendbuf[READFILESIZE2] = { 0 };

fseek(readfile,-16,SEEK_END);/最后16个字节的表示文件长度的空间/

filelen = fread( sendbuf, sizeof( INT8 ), 16, readfile );

encodehex( readbuf,sendbuf,8);

switch(flag)

{

case DESONE:

Ddes( readbuf,key,8);

break;

case DESTHREE:

Ddes3( readbuf, key ,keynum,8);

break;

case DESMULTI:

DdesN( readbuf, superkey ,n_superkey,8);

break;

}

/解密/

memcpy((ULONG8)&totalfilelen, &readbuf[0],4);/得到文件总长/

memset(readbuf,0 ,8);

memset(sendbuf,0 ,16);

num = totalfilelen/READFILESIZE;/有几个READFILESIZE组/

totalfilelen %= READFILESIZE;

fseek(readfile,0,SEEK_SET);/跳到文件头/

while(num--)

{

filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE2, readfile );

encodehex( readbuf,sendbuf,READFILESIZE);

switch(flag)

{

case DESONE:

Ddes( readbuf,key,READFILESIZE);

break;

case DESTHREE:

Ddes3( readbuf, key ,keynum,READFILESIZE);

break;

case DESMULTI:

DdesN( readbuf, superkey ,n_superkey,READFILESIZE);

break;

}

writelen = fwrite(readbuf, sizeof( INT8 ), READFILESIZE, writefile);

memset(readbuf,0 ,READFILESIZE);

memset(sendbuf,0 ,READFILESIZE2);

}

if ( totalfilelen > 0 )/最后一块有多余的元素/

{

filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE2, readfile );

encodehex( readbuf,sendbuf,READFILESIZE);

switch(flag)

{

case DESONE:

Ddes( readbuf,key,READFILESIZE);

break;

case DESTHREE:

Ddes3( readbuf, key ,keynum,READFILESIZE);

break;

case DESMULTI:

DdesN( readbuf, superkey ,n_superkey,READFILESIZE);

break;

}

writelen = fwrite(readbuf, sizeof( INT8 ), totalfilelen, writefile);

memset(readbuf,0 ,READFILESIZE);

memset(sendbuf,0 ,READFILESIZE2);

}

return SUCCESS;

}

INT32 hextofile( ULONG8 buf ,FILE writefile, ULONG32 length)

{

ULONG32 writelen = 0 ;

/以16进制形式写入文件/

while( writelen < length)

{

if(buf[writelen] == 0)

{

fprintf( writefile, "%x", 0 );

fprintf( writefile, "%x", 0 );

}

else if (buf[writelen] < 0x10)

{

fprintf( writefile, "%x", 0 );

fprintf( writefile, "%x", buf[writelen] );

}

else

{

fprintf( writefile, "%x", buf[writelen] );

}

writelen++;

}

return SUCCESS;

}

INT32 encodehex(ULONG8 tobuf,ULONG8 frombuf,ULONG32 len)

{

ULONG8 readfirst = frombuf ;

ULONG8 readend = &frombuf[1] ;

INT8 s;

ULONG8 y[2] ;

ULONG32 i;

for ( i = 0 ; i < len ; i++)

{

y[0] = readfirst ;

y[1] = readend ;

readfirst += 2 ;

readend += 2 ;

tobuf[i] = (ULONG8)strtol((INT8)y, &s, 16);

}

return SUCCESS;

}

以上就是关于求C或C++的DES加密算法全部的内容,包括:求C或C++的DES加密算法、用java实现des算法、求用JAVA实现的DES算法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9277918.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存