热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C加密解密

**********************************************************des.h*用户使用des算法头文件**************

/********************************************************** des.h* 用户使用des算法头文件* *********************************************************/
#ifndef _OPENDESS_H_
#define _OPENDESS_H_#ifdef __cplusplus
extern "C" {
#endif//ab\0defg//用户使用的函数
int DesEnc(unsigned char *pInData,//明文int nInDataLen,unsigned char *pOutData,//密文int *pOutDataLen);//用户使用函数des解密
int DesDec(unsigned char *pInData,//密文int nInDataLen,unsigned char *pOutData,//明文int *pOutDataLen);#ifdef __cplusplus
}
#endif#endif

/******************************************************** des.c* common des......*******************************************************/#include
#include
<string.h>
#include

#include
"des.h"/*********************************************************data type definition for Des;
*********************************************************
*/
#define EN0 0
#define DE1 1#define DES_KEYBYTES 128
#define DES_KEYLONGS 32
#define DES_BLOCKLEN 8typedef struct {unsigned char ek[DES_KEYBYTES];int ekLen;unsigned char dk[DES_KEYBYTES];int dkLen;unsigned char CbcCtx[DES_BLOCKLEN];
} DES_CTX;typedef
struct {unsigned char ek1[DES_KEYBYTES];int ek1Len;unsigned char dk1[DES_KEYBYTES];int dk1Len;unsigned char ek2[DES_KEYBYTES];int ek2Len;unsigned char dk2[DES_KEYBYTES];int dk2Len;unsigned char CbcCtx[DES_BLOCKLEN];//int IsFirstBlock;
} DES3_CTX;static unsigned char pc1[56] &#61; {56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 };static unsigned char pc2[48] &#61; {13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };static unsigned short bytebit[8] &#61; {0200,0100,040,020,010,04,02,01 };
static unsigned char totrot[16] &#61; {1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28};
static unsigned long bigbyte[24] &#61; {0x800000L, 0x400000L, 0x200000L, 0x100000L,0x80000L, 0x40000L, 0x20000L, 0x10000L,0x8000L, 0x4000L, 0x2000L, 0x1000L,0x800L, 0x400L, 0x200L, 0x100L,0x80L, 0x40L, 0x20L, 0x10L,0x8L, 0x4L, 0x2L, 0x1L };//insert digits
static unsigned long SP1[64] &#61;{0x01010400l,0x00000000l,0x00010000l,0x01010404l,0x01010004l,0x00010404l,0x00000004l,0x00010000l,0x00000400l,0x01010400l,0x01010404l,0x00000400l,0x01000404l,0x01010004l,0x01000000l,0x00000004l,0x00000404l,0x01000400l,0x01000400l,0x00010400l,0x00010400l,0x01010000l,0x01010000l,0x01000404l,0x00010004l,0x01000004l,0x01000004l,0x00010004l,0x00000000l,0x00000404l,0x00010404l,0x01000000l,0x00010000l,0x01010404l,0x00000004l,0x01010000l,0x01010400l,0x01000000l,0x01000000l,0x00000400l,0x01010004l,0x00010000l,0x00010400l,0x01000004l,0x00000400l,0x00000004l,0x01000404l,0x00010404l,0x01010404l,0x00010004l,0x01010000l,0x01000404l,0x01000004l,0x00000404l,0x00010404l,0x01010400l,0x00000404l,0x01000400l,0x01000400l,0x00000000l,0x00010004l,0x00010400l,0x00000000l,0x01010004l };static unsigned long SP2[64]&#61;{0x80108020l,0x80008000l,0x00008000l,0x00108020l,0x00100000l,0x00000020l,0x80100020l,0x80008020l,0x80000020l,0x80108020l,0x80108000l,0x80000000l,0x80008000l,0x00100000l,0x00000020l,0x80100020l,0x00108000l,0x00100020l,0x80008020l,0x00000000l,0x80000000l,0x00008000l,0x00108020l,0x80100000l,0x00100020l,0x80000020l,0x00000000l,0x00108000l,0x00008020l,0x80108000l,0x80100000l,0x00008020l,0x00000000l,0x00108020l,0x80100020l,0x00100000l,0x80008020l,0x80100000l,0x80108000l,0x00008000l,0x80100000l,0x80008000l,0x00000020l,0x80108020l,0x00108020l,0x00000020l,0x00008000l,0x80000000l,0x00008020l,0x80108000l,0x00100000l,0x80000020l,0x00100020l,0x80008020l,0x80000020l,0x00100020l,0x00108000l,0x00000000l,0x80008000l,0x00008020l,0x80000000l,0x80100020l,0x80108020l,0x00108000l };static unsigned long SP3[64]&#61;{ 0x00000208l,0x08020200l,0x00000000l,0x08020008l,0x08000200l,0x00000000l,0x00020208l,0x08000200l,0x00020008l,0x08000008l,0x08000008l,0x00020000l,0x08020208l,0x00020008l,0x08020000l,0x00000208l,0x08000000l,0x00000008l,0x08020200l,0x00000200l,0x00020200l,0x08020000l,0x08020008l,0x00020208l,0x08000208l,0x00020200l,0x00020000l,0x08000208l,0x00000008l,0x08020208l,0x00000200l,0x08000000l,0x08020200l,0x08000000l,0x00020008l,0x00000208l,0x00020000l,0x08020200l,0x08000200l,0x00000000l,0x00000200l,0x00020008l,0x08020208l,0x08000200l,0x08000008l,0x00000200l,0x00000000l,0x08020008l,0x08000208l,0x00020000l,0x08000000l,0x08020208l,0x00000008l,0x00020208l,0x00020200l,0x08000008l,0x08020000l,0x08000208l,0x00000208l,0x08020000l,0x00020208l,0x00000008l,0x08020008l,0x00020200l };static unsigned long SP4[64]&#61;{ 0x00802001l,0x00002081l,0x00002081l,0x00000080l,0x00802080l,0x00800081l,0x00800001l,0x00002001l,0x00000000l,0x00802000l,0x00802000l,0x00802081l,0x00000081l,0x00000000l,0x00800080l,0x00800001l,0x00000001l,0x00002000l,0x00800000l,0x00802001l,0x00000080l,0x00800000l,0x00002001l,0x00002080l,0x00800081l,0x00000001l,0x00002080l,0x00800080l,0x00002000l,0x00802080l,0x00802081l,0x00000081l,0x00800080l,0x00800001l,0x00802000l,0x00802081l,0x00000081l,0x00000000l,0x00000000l,0x00802000l,0x00002080l,0x00800080l,0x00800081l,0x00000001l,0x00802001l,0x00002081l,0x00002081l,0x00000080l,0x00802081l,0x00000081l,0x00000001l,0x00002000l,0x00800001l,0x00002001l,0x00802080l,0x00800081l,0x00002001l,0x00002080l,0x00800000l,0x00802001l,0x00000080l,0x00800000l,0x00002000l,0x00802080l };static unsigned long SP5[64]&#61;{ 0x00000100l,0x02080100l,0x02080000l,0x42000100l,0x00080000l,0x00000100l,0x40000000l,0x02080000l,0x40080100l,0x00080000l,0x02000100l,0x40080100l,0x42000100l,0x42080000l,0x00080100l,0x40000000l,0x02000000l,0x40080000l,0x40080000l,0x00000000l,0x40000100l,0x42080100l,0x42080100l,0x02000100l,0x42080000l,0x40000100l,0x00000000l,0x42000000l,0x02080100l,0x02000000l,0x42000000l,0x00080100l,0x00080000l,0x42000100l,0x00000100l,0x02000000l,0x40000000l,0x02080000l,0x42000100l,0x40080100l,0x02000100l,0x40000000l,0x42080000l,0x02080100l,0x40080100l,0x00000100l,0x20000000l,0x42080000l,0x42080100l,0x00080100l,0x42000000l,0x42080100l,0x02080000l,0x02000100l,0x40000100l,0x00080000l,0x00080100l,0x02000100l,0x40000100l,0x00080000l,0x00000000l,0x40080000l,0x02080100l,0x40000100l };static unsigned long SP6[64]&#61;{ 0x20000010l,0x20400000l,0x00004000l,0x20404010l,0x20400000l,0x00000010l,0x20404010l,0x00400000l,0x20004000l,0x00404010l,0x00400000l,0x20000010l,0x00400010l,0x20004000l,0x20000000l,0x00004010l,0x00000000l,0x00400010l,0x20004010l,0x00004000l,0x00404000l,0x20004010l,0x00000010l,0x20400010l,0x20400010l,0x00000000l,0x00404010l,0x20404000l,0x00004010l,0x00404000l,0x20404000l,0x20000000l,0x20004000l,0x00000010l,0x20400010l,0x00404000l,0x20404010l,0x00400000l,0x00004010l,0x20000010l,0x00400000l,0x20004000l,0x20000000l,0x00004010l,0x20000010l,0x20404010l,0x00404000l,0x20400000l,0x00404010l,0x20404000l,0x00000000l,0x20400010l,0x00000010l,0x00004000l,0x20400000l,0x00404010l,0x00004000l,0x00400010l,0x20004010l,0x00000000l,0x20404000l,0x20000000l,0x00400010l,0x20004010l }; static unsigned long SP7[64] &#61; {0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };static unsigned long SP8[64] &#61; {0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };void deskey(unsigned char *key,short edf, unsigned long *kn);
void cookey(register unsigned long *raw1, unsigned long *dough);
//void cpkey(register unsigned long *into);
//void usekey(register unsigned long *from);
//void des(unsigned char *inblock,unsigned char *outblock);
void scrunch(register unsigned char *outof, register unsigned long *into);
void unscrun(register unsigned long *outof, register unsigned char *into);
void desfunc(register unsigned long *block,register unsigned long *keys);/***************** DES Function *****************/
unsigned
long OPENCOMM_DesExpandEncKey(unsigned char *pbDesKey,unsigned long ulDesKeyLen,unsigned char *pbDesEncKey,unsigned long *ulDesEncKeyLen);unsigned long OPENCOMM_DesExpandDecKey(unsigned char *pbDesKey,unsigned long ulDesKeyLen,unsigned char *pbDesDecKey,unsigned long *ulDesDecKeyLen);unsigned long OPENCOMM_DesEncRaw(unsigned char *pbDesEncKey,unsigned long ulDesEncKeyLen,unsigned char *pbInData,unsigned long ulInDataLen,unsigned char *pbOutData,unsigned long *ulOutDataLen);unsigned long OPENCOMM_DesDecRaw(unsigned char *pbDesDecKey,unsigned long ulDesDecKeyLen,unsigned char *pbInData,unsigned long ulInDataLen,unsigned char *pbOutData,unsigned long *ulOutDataLen);int myic_DESDecrypt(unsigned char *pDesKey,int nDesKeyLen,unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen);int myic_DESEncrypt(unsigned char *pDesKey,int nDesKeyLen,unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen);void deskey(unsigned char *key,short edf, unsigned long *kn)
{register
int i, j, l, m, n;unsigned long pc1m[56],pcr[56];for ( j &#61; 0; j <56; j&#43;&#43; ) {l &#61; pc1[j];m &#61; l & 07;pc1m[j] &#61; (((unsigned long) key[l >> 3] & (unsigned long)bytebit[m] ) ? 1:0);}for ( i &#61; 0;i <16; i&#43;&#43;){if ( edf &#61;&#61; DE1 ) m &#61; (15 - i) <<1;else m &#61; i <<1;n &#61; m &#43; 1;kn[m] &#61; kn[n] &#61; 0L;for ( j &#61; 0; j <28; j&#43;&#43; ){l &#61; j &#43; totrot[i];if ( l <28 ) pcr[j] &#61; pc1m[l];else pcr[j] &#61; pc1m[l-28];}for (j &#61; 28; j <56; j&#43;&#43; ) {l &#61; j &#43; totrot[i];if ( l <56 ) pcr[j] &#61; pc1m[l];else pcr[j] &#61; pc1m[l-28];} for ( j &#61; 0; j <24; j&#43;&#43; ) {if ( pcr[pc2[j]] ) kn[m] |&#61; bigbyte[j];if ( pcr[pc2[j&#43;24]] ) kn[n] |&#61; bigbyte[j];}}return;
}
void cookey(register unsigned long *raw1, unsigned long *dough)
{register unsigned
long *cook,*raw0;register int i;cook &#61; dough;for ( i &#61; 0; i <16; i&#43;&#43;, raw1&#43;&#43; ) {raw0 &#61; raw1&#43;&#43;;*cook &#61; (*raw0 & 0x00fc0000L) <<6;*cook |&#61; (*raw0 & 0x00000fc0L) <<10;*cook |&#61; (*raw1 & 0x00fc0000L) >> 10;*cook&#43;&#43; |&#61; (*raw1 & 0x00000fc0L) >> 6;*cook &#61; (*raw0 & 0x0003f000L) <<12;*cook |&#61; (*raw0 & 0x0000003fL) <<16;*cook |&#61; (*raw1 & 0x0003f000L) >> 4;*cook&#43;&#43; |&#61; (*raw1 & 0x0000003fL);}return;
}
void scrunch(register unsigned char *outof, register unsigned long *into)
{
*into &#61; (*outof&#43;&#43; & 0xffL) <<24;*into |&#61; (*outof&#43;&#43; & 0xffL) <<16;*into |&#61; (*outof&#43;&#43; & 0xffL) <<8;*into&#43;&#43; |&#61; (*outof&#43;&#43; & 0xffL);*into &#61; (*outof&#43;&#43; & 0xffL) <<24;*into |&#61; (*outof&#43;&#43; & 0xffL) <<16;*into |&#61; (*outof&#43;&#43; & 0xffL) <<8;*into&#43;&#43; |&#61; (*outof & 0xffL);return;
}
void unscrun(register unsigned long *outof, register unsigned char *into)
{
*into&#43;&#43; &#61; (unsigned char)((*outof >> 24) & 0xffL);*into&#43;&#43; &#61; (unsigned char)((*outof >> 16) & 0xffL);*into&#43;&#43; &#61; (unsigned char)((*outof >> 8) & 0xffL);*into&#43;&#43; &#61; (unsigned char)( *outof&#43;&#43; & 0xffL);*into&#43;&#43; &#61; (unsigned char)((*outof >> 24) & 0xffL);*into&#43;&#43; &#61; (unsigned char)((*outof >> 16) & 0xffL);*into&#43;&#43; &#61; (unsigned char)((*outof >> 8) & 0xffL);*into &#61; (unsigned char)( *outof & 0xffL);return;
}
void desfunc(register unsigned long *block,register unsigned long *keys)
{register unsigned
long fval, work, right, leftt;register int round;leftt &#61; block[0];right &#61; block[1];work &#61; ((leftt >> 4) ^ right) & 0x0f0f0f0fL;right ^&#61; work;leftt ^&#61; (work <<4);work &#61; ((leftt >> 16) ^ right) & 0x0000ffffL;right ^&#61; work;leftt ^&#61; (work <<16);work &#61; ((right >> 2) ^ leftt) & 0x33333333L;leftt ^&#61; work;right ^&#61; (work <<2);work &#61; ((right >> 8) ^ leftt) & 0x00ff00ffL;leftt ^&#61; work;right ^&#61; (work <<8);right &#61; ((right <<1) | ((right >>31) & 1L)) & 0xffffffffL;work &#61; (leftt ^ right) & 0xaaaaaaaaL;leftt ^&#61; work;right ^&#61; work;leftt &#61; ((leftt <<1) | ((leftt >> 31)&1L)) & 0xffffffffL;for (round &#61; 0; round <8; round&#43;&#43;) {work &#61; (right <<28) | (right >> 4);work ^&#61; *keys&#43;&#43;;fval &#61; SP7[ work & 0x3fL];fval |&#61; SP5[(work >> 8) & 0x3fL];fval |&#61; SP3[(work >> 16) & 0x3fL];fval |&#61; SP1[(work >> 24) & 0x3fL];work &#61; right ^ *keys&#43;&#43;;fval |&#61; SP8[ work & 0x3fL];fval |&#61; SP6[(work >> 8) & 0x3fL];fval |&#61; SP4[(work >> 16) & 0x3fL];fval |&#61; SP2[(work >> 24) & 0x3fL];leftt ^&#61; fval;work &#61; (leftt <<28) | (leftt >> 4);work ^&#61; *keys&#43;&#43;;fval &#61; SP7[ work & 0x3fL];fval |&#61; SP5[(work >> 8) & 0x3fL];fval |&#61; SP3[(work >> 16) & 0x3fL];fval |&#61; SP1[(work >> 24) & 0x3fL];work &#61; leftt ^ *keys&#43;&#43;;fval |&#61; SP8[ work & 0x3fL];fval |&#61; SP6[(work >> 8) & 0x3fL];fval |&#61; SP4[(work >> 16) & 0x3fL];fval |&#61; SP2[(work >> 24) & 0x3fL];right ^&#61; fval;}right &#61; (right <<31) | (right >> 1);work &#61; (leftt ^ right) & 0xaaaaaaaaL;leftt ^&#61; work;right ^&#61; work;leftt &#61; (leftt <<31) | (leftt >> 1);work &#61; ((leftt >> 8) ^ right) & 0x00ff00ffL;right ^&#61; work;leftt ^&#61; (work <<8);work &#61; ((leftt >> 2) ^ right) & 0x33333333L;right ^&#61; work;leftt ^&#61; (work <<2);work &#61; ((right >> 16) ^ leftt) & 0x0000ffffL;leftt ^&#61; work;right ^&#61; (work <<16);work &#61; ((right >> 4) ^ leftt) & 0x0f0f0f0fL;leftt ^&#61; work;right ^&#61; (work <<4);*block&#43;&#43; &#61; right;*block &#61; leftt;return;
}
/*****************************************************************OPENCOMM_DesExpandEncKey : Expand Des Enc Key 扩展des加密密钥Return value:0 : Successother : failedParameters:pbDesKey : 扩展前的DES密钥(8字节) inputulDesKeyLen : 扩展前的DES密钥长度 inputpbDesEncKey : 扩展后的DES加密密钥(128字节) output*ulDesEncKeyLen : 扩展后的DES加密密钥长度 output
****************************************************************
*/
unsigned
long OPENCOMM_DesExpandEncKey(unsigned char *pbDesKey,unsigned long ulDesKeyLen,unsigned char *pbDesEncKey,unsigned long *ulDesEncKeyLen)
{unsigned
long kn[32], dough[32];if (ulDesKeyLen !&#61; 8)return 0xEE20;deskey(pbDesKey, EN0, kn);cookey(kn, dough);*ulDesEncKeyLen &#61; DES_KEYBYTES; //32 long &#61; 128 bytesmemcpy(pbDesEncKey, dough, *ulDesEncKeyLen);return 0;
}
/*****************************************************************OPENCOMM_DesExpandDecKey : Expand Des Dec Key 扩展des解密密钥Return value:0 : Successother : failedParameters:pbDesKey : 扩展前的DES密钥(8字节) inputulDesKeyLen : 扩展前的DES密钥长度 inputpbDesDecKey : 扩展后的DES解密密钥(128字节) output*ulDesDecKeyLen : 扩展后的DES解密密钥长度 output
****************************************************************
*/
unsigned
long OPENCOMM_DesExpandDecKey(unsigned char *pbDesKey,unsigned long ulDesKeyLen,unsigned char *pbDesDecKey,unsigned long *ulDesDecKeyLen)
{unsigned
long kn[32], dough[32];if (ulDesKeyLen !&#61; 8)return 0xEE20;deskey(pbDesKey, DE1, kn);cookey(kn, dough);*ulDesDecKeyLen &#61; DES_KEYBYTES; //32 long &#61; 128 bytesmemcpy(pbDesDecKey, dough, *ulDesDecKeyLen);return 0;
}
/****************************************************************OPENCOMM_DesEncRaw : Des算法加密小整块明文8字节 Return value:0 : Successother : failedParameters:pbDesEncKey : DES加密密钥 inputulDesEncKeyLen : DES加密密钥长度 inputpbInData : 待加密的明文 inputulInDataLen : 待加密的明文长度 inputpbOutData : 加密后的密文 output*ulOutDataLen : 加密后的密文长度 output
****************************************************************
*/
unsigned
long OPENCOMM_DesEncRaw(unsigned char *pbDesEncKey,unsigned long ulDesEncKeyLen,unsigned char *pbInData,unsigned long ulInDataLen,unsigned char *pbOutData,unsigned long *ulOutDataLen)
{unsigned
long work[2], ek[DES_KEYLONGS];unsigned char cp[DES_BLOCKLEN];if (ulInDataLen !&#61; DES_BLOCKLEN)return 0xEE20;if (ulDesEncKeyLen !&#61; DES_KEYBYTES)return 0xEE20;memcpy(cp, pbInData, DES_BLOCKLEN);scrunch(cp,work); // 8 bytes -> 2 long
memcpy(ek, pbDesEncKey, ulDesEncKeyLen);desfunc(work,ek);unscrun(work,cp); // 2 long -> 8 bytes
memcpy(pbOutData, cp, DES_BLOCKLEN);*ulOutDataLen &#61; DES_BLOCKLEN;return 0;
}
/*****************************************************************OPENCOMM_DesDecRaw : Des算法解密小整块密文8字节 Return value:0 : Successother : failedParameters:pbDesDecKey : DES解密密钥 inputulDesDecKeyLen : DES解密密钥长度 inputpbInData : 待解密的密文 inputulInDataLen : 待解密的密文长度 inputpbOutData : 解密后的明文 output*ulOutDataLen : 解密后的明文长度 output
****************************************************************
*/
unsigned
long OPENCOMM_DesDecRaw(unsigned char *pbDesDecKey,unsigned long ulDesDecKeyLen,unsigned char *pbInData,unsigned long ulInDataLen,unsigned char *pbOutData,unsigned long *ulOutDataLen)
{unsigned
long work[2], dk[DES_KEYLONGS];unsigned char cp[DES_BLOCKLEN];if (ulInDataLen !&#61; DES_BLOCKLEN)return 0xEE20;if (ulDesDecKeyLen !&#61; DES_KEYBYTES)return 0xEE20;memcpy(cp, pbInData, DES_BLOCKLEN);scrunch(cp,work); // 8 bytes -> 2 long
memcpy(dk, pbDesDecKey, ulDesDecKeyLen);desfunc(work,dk);unscrun(work,cp); // 2 long -> 8 bytes
memcpy(pbOutData, cp, DES_BLOCKLEN);
// des_enc(pbDesEncKey, pbInData, pbOutData);*ulOutDataLen &#61; DES_BLOCKLEN;return 0;
}
/********************* DES *********************/int myic_DESEncrypt(unsigned char *pDesKey,int nDesKeyLen,unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen)
{unsigned
char DesKeyBuf[32];unsigned char DesEncKeyBuf[128];int EncKeyLen, KeyLen &#61; 0;int retval &#61; 0, loops, i;if(nInDataLen%8 !&#61; 0)return 0xEE20;if(nDesKeyLen !&#61; 8)return 0xEE20;KeyLen &#61; nDesKeyLen;memcpy(DesKeyBuf, pDesKey, nDesKeyLen);retval &#61; OPENCOMM_DesExpandEncKey(DesKeyBuf, KeyLen,DesEncKeyBuf, (unsigned long *)&EncKeyLen);if(retval !&#61; 0)return retval;loops &#61; nInDataLen/8;for(i &#61; 0; i ){retval &#61; OPENCOMM_DesEncRaw(DesEncKeyBuf, EncKeyLen, pInData &#43; i*8,8, pOutData &#43; i*8, (unsigned long *)pOutDataLen);if(retval !&#61; 0)return retval;}*pOutDataLen &#61; nInDataLen;return retval;
}
int myic_DESDecrypt(unsigned char *pDesKey,int nDesKeyLen,unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen)
{unsigned
char DesKeyBuf[32];unsigned char DesDecKeyBuf[128];int DecKeyLen, KeyLen &#61; 0;int retval &#61; 0, loops, i;if(nInDataLen%8 !&#61; 0)return 0xEE20;if(nDesKeyLen !&#61; 8)return 0xEE20;KeyLen &#61; nDesKeyLen;memcpy(DesKeyBuf, pDesKey, nDesKeyLen);retval &#61; OPENCOMM_DesExpandDecKey(DesKeyBuf, KeyLen,DesDecKeyBuf, (unsigned long *)&DecKeyLen);if(retval !&#61; 0)return retval;loops &#61; nInDataLen/8;for(i &#61; 0; i ){retval &#61; OPENCOMM_DesDecRaw(DesDecKeyBuf, DecKeyLen, pInData &#43; i*8,8, pOutData &#43; i*8, (unsigned long *)pOutDataLen);if(retval !&#61; 0)return retval;}*pOutDataLen &#61; nInDataLen;return retval;
}
//对称明文数据打pading
void CW_dataPadAdd(int tag, unsigned char *date, unsigned int dateLen, unsigned char **padDate, unsigned int *padDateLen)
{
int i, padLen;unsigned char *pTmp &#61; NULL;pTmp &#61; (unsigned char *)malloc(dateLen&#43;24);if (pTmp &#61;&#61; NULL){*padDate &#61; NULL;return ;}memset(pTmp, 0, dateLen&#43;24);memcpy(pTmp, date, dateLen);if (tag &#61;&#61; 0){padLen &#61; 8 - dateLen % 8;for (i&#61;0; i){pTmp[dateLen&#43;i] &#61; (char)padLen;}*padDateLen &#61; dateLen &#43; padLen;}else{padLen &#61; 16 - dateLen % 16;for (i&#61;0; i){pTmp[dateLen&#43;i] &#61; (char)padLen;} }*padDateLen &#61; dateLen &#43; padLen; *padDate &#61; pTmp;
}
#define USER_PASSWORD_KEY "abcd1234"//数据加密
int DesEnc(unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen)
{
int rv;unsigned char *padDate &#61; NULL;unsigned int padDateLen &#61; 0;CW_dataPadAdd(0, pInData, (unsigned int )nInDataLen, &padDate, &padDateLen);rv &#61; myic_DESEncrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),padDate, (int)padDateLen, pOutData, pOutDataLen);if (rv !&#61; 0){if (padDate !&#61; NULL){free(padDate);}return rv; }if (padDate !&#61; NULL){free(padDate);}return 0;
}
//数据加密
int DesEnc_raw(unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen)
{
int rv;unsigned char *padDate &#61; NULL;unsigned int padDateLen &#61; 0;rv &#61; myic_DESEncrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),pInData, (int)nInDataLen, pOutData, pOutDataLen);if (rv !&#61; 0){return rv; }return 0;
}
//解密分配内存错误
#define ERR_MALLOC 20
//密码长度不是8的整数倍, 不合法
#define ERR_FILECONT 20//用户使用函数des解密
int DesDec(unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen)
{
int rv;char padChar;unsigned char *tmpPlain &#61; NULL;tmpPlain &#61; (unsigned char *)malloc(nInDataLen&#43;24);if (tmpPlain &#61;&#61; NULL){return ERR_MALLOC;}memset(tmpPlain, 0, nInDataLen&#43;24);//解密rv &#61; myic_DESDecrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),pInData, nInDataLen, tmpPlain, pOutDataLen);if (rv !&#61; 0){if (tmpPlain !&#61; NULL) free(tmpPlain);return rv;}//去padingpadChar &#61; tmpPlain[*pOutDataLen - 1];if ( (int)padChar<&#61;0 || (int)padChar>8) //异常处理
{if (tmpPlain) free(tmpPlain);return ERR_FILECONT;}*pOutDataLen &#61; *pOutDataLen - (int)padChar; //memset(tmpPlain &#43; *pOutDataLen, 0, (int)padChar); memcpy(pOutData, tmpPlain, *pOutDataLen);if (tmpPlain) free(tmpPlain); return 0;
}
//用户使用函数des解密
int DesDec_raw(unsigned char *pInData,int nInDataLen,unsigned char *pOutData,int *pOutDataLen)
{
int rv;//char padChar;//unsigned char *tmpPlain &#61; NULL;/*tmpPlain &#61; (unsigned char *)malloc(nInDataLen&#43;24);if (tmpPlain &#61;&#61; NULL){return ERR_MALLOC;}memset(tmpPlain, 0, nInDataLen&#43;24);*///解密rv &#61; myic_DESDecrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),pInData, nInDataLen, pOutData, pOutDataLen);if (rv !&#61; 0){//if (tmpPlain !&#61; NULL) free(tmpPlain);return rv;}/*//去padingpadChar &#61; tmpPlain[*pOutDataLen - 1];if ( (int)padChar<&#61;0 || (int)padChar>8) //异常处理{if (tmpPlain) free(tmpPlain);return ERR_FILECONT;}*pOutDataLen &#61; *pOutDataLen - (int)padChar; //memset(tmpPlain &#43; *pOutDataLen, 0, (int)padChar); memcpy(pOutData, tmpPlain, *pOutDataLen);if (tmpPlain) free(tmpPlain); */return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include

#include
<string.h>
#include

#include
"des.h"int main11()
{
int ret &#61; 0;char *plain &#61; "1232343444";//明文int plainlen &#61; strlen(plain);char plain2[4096] &#61; {0};//明文int plainlen2 &#61; 0;unsigned char cryptbuf[4096] &#61; {0};//密文int cryptlen &#61; 0;//用户使用的函数ret &#61; DesEnc(plain, plainlen, cryptbuf, &cryptlen);if (ret !&#61; 0){printf("func DesEnc() err:%d \n");return ret;}//用户使用函数des解密ret &#61; DesDec(cryptbuf, cryptlen, plain2, &plainlen2);if (ret !&#61; 0){printf("func DesDec() err:%d \n");return ret;}if (plainlen !&#61; plainlen2){ret &#61; -2;printf("明文长度和解密后的明文长度 不一样 \n");return ret;}if (memcmp(plain2, plain, plainlen) &#61;&#61; 0 ){printf("明文长度和解密后的明文长度 一样 ok \n");}else{printf("明文长度和解密后的明文长度 不一样 err \n");}system("pause");return ret ;
}

#define _CRT_SECURE_NO_WARNINGS
#include

#include
<string.h>
#include

#include
"des.h"int FileSymEnc(const char *pfile1, const char *pfile2)//pfile1输入文件&#xff0c;
{int ret &#61; 0;FILE *fp1&#61; NULL, *fp2 &#61; NULL;unsigned char plain[4096];int plainlen &#61; 0;unsigned char cryptbuf[4096] &#61; {0};int cryptlen &#61; 0;int tmplen;fp1 &#61; fopen(pfile1, "rb");//读的方式打开文件if (fp1 &#61;&#61; NULL){goto END;}fp2 &#61; fopen(pfile2, "wb");//写的方式打开文件if (fp2 &#61;&#61; NULL){goto END;}while (!feof(fp1))//每次读取4k
{plainlen &#61; fread(plain, 1, 4096, fp1);//每次读取4kif (feof(fp1)) //读完数据以后&#xff0c;判断是否文件结束
{break;//文件不足以4k,则跳出
}//加密&#61;&#61;4k的数据ret &#61; DesDec_raw(plain, plainlen, cryptbuf, &cryptlen);if (ret !&#61; 0){printf("func DesEnc() err:%d \n", ret);goto END;}tmplen &#61; fwrite(cryptbuf, 1, cryptlen, fp2);if (tmplen !&#61; cryptlen)//明文和密文的长度是一样的
{ret &#61; -3;printf("写密文文件失败&#xff0c;请检查是否磁盘已满\n");goto END;}//if (plainlen &#61;&#61; 4096)
}//加密小于4k的数据ret &#61; DesEnc(plain, plainlen, cryptbuf, &cryptlen);if (ret !&#61; 0){printf("func DesEnc() err:%d \n", ret);goto END;}tmplen &#61; fwrite(cryptbuf, 1, cryptlen, fp2);if (cryptlen !&#61; tmplen)//磁盘已满
{ret &#61; -3;printf("写小于4k文件密文失败&#xff0c;请检查是否磁盘已满\n");goto END;}END:if (fp1 !&#61; NULL){fclose(fp1);}if (fp2 !&#61; NULL){fclose(fp2);}return 0;
}
void main()
{
int ret &#61; 0;const char *file1 &#61; "c:/socketclient.dll";const char *file2 &#61; "c:/socketclientend.dll";//const char *file1 &#61; "c:/22.txt";//const char *file2 &#61; "c:/22enc.txt";
ret &#61; FileSymEnc(file1, file2);if (ret !&#61; 0){printf("func FileSymEnc() err\n " );return ;}system("pause");return ;
}

 

转:https://www.cnblogs.com/yaowen/p/4768236.html



推荐阅读
author-avatar
曹lister_638
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有