/*********************************************************
* 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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#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 8 typedef 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[] = {
, , , , , , , , , , , , , ,
, , , , , , , , , , , , , ,
, , , , , , , , , , , , , ,
, , , , , , , , , , , , , }; static unsigned char pc2[] = {
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , }; static unsigned short bytebit[] = {,,,,,,, };
static unsigned char totrot[] = {,,,,,,,,,,,,,,,};
static unsigned long bigbyte[] = {
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[] ={
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[]={
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[]={
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[]={
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[]={
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[]={
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[] = {
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[] = {
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[],pcr[]; for ( j = ; j < ; j++ )
{
l = pc1[j];
m = l & ;
pc1m[j] = (((unsigned long) key[l >> ] & (unsigned long)bytebit[m] ) ? :);
}
for ( i = ;i < ; i++)
{
if ( edf == DE1 ) m = ( - i) << ;
else m = i << ;
n = m + ;
kn[m] = kn[n] = 0L;
for ( j = ; j < ; j++ )
{
l = j + totrot[i];
if ( l < ) pcr[j] = pc1m[l];
else pcr[j] = pc1m[l-];
}
for (j = ; j < ; j++ )
{
l = j + totrot[i];
if ( l < ) pcr[j] = pc1m[l];
else pcr[j] = pc1m[l-];
}
for ( j = ; j < ; j++ )
{
if ( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
if ( pcr[pc2[j+]] ) kn[n] |= bigbyte[j];
}
}
return;
} void cookey(register unsigned long *raw1, unsigned long *dough)
{
register unsigned long *cook,*raw0;
register int i; cook = dough;
for ( i = ; i < ; i++, raw1++ ) {
raw0 = raw1++;
*cook = (*raw0 & 0x00fc0000L) << ;
*cook |= (*raw0 & 0x00000fc0L) << ;
*cook |= (*raw1 & 0x00fc0000L) >> ;
*cook++ |= (*raw1 & 0x00000fc0L) >> ;
*cook = (*raw0 & 0x0003f000L) << ;
*cook |= (*raw0 & 0x0000003fL) << ;
*cook |= (*raw1 & 0x0003f000L) >> ;
*cook++ |= (*raw1 & 0x0000003fL);
}
return;
} void scrunch(register unsigned char *outof, register unsigned long *into)
{
*into = (*outof++ & 0xffL) << ;
*into |= (*outof++ & 0xffL) << ;
*into |= (*outof++ & 0xffL) << ;
*into++ |= (*outof++ & 0xffL);
*into = (*outof++ & 0xffL) << ;
*into |= (*outof++ & 0xffL) << ;
*into |= (*outof++ & 0xffL) << ;
*into++ |= (*outof & 0xffL);
return;
} void unscrun(register unsigned long *outof, register unsigned char *into)
{
*into++ = (unsigned char)((*outof >> ) & 0xffL);
*into++ = (unsigned char)((*outof >> ) & 0xffL);
*into++ = (unsigned char)((*outof >> ) & 0xffL);
*into++ = (unsigned char)( *outof++ & 0xffL);
*into++ = (unsigned char)((*outof >> ) & 0xffL);
*into++ = (unsigned char)((*outof >> ) & 0xffL);
*into++ = (unsigned char)((*outof >> ) & 0xffL);
*into = (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 = block[];
right = block[];
work = ((leftt >> ) ^ right) & 0x0f0f0f0fL; right ^= work;
leftt ^= (work << );
work = ((leftt >> ) ^ right) & 0x0000ffffL; right ^= work;
leftt ^= (work << );
work = ((right >> ) ^ leftt) & 0x33333333L; leftt ^= work;
right ^= (work << );
work = ((right >> ) ^ leftt) & 0x00ff00ffL; leftt ^= work;
right ^= (work << );
right = ((right << ) | ((right >>) & 1L)) & 0xffffffffL;
work = (leftt ^ right) & 0xaaaaaaaaL; leftt ^= work;
right ^= work;
leftt = ((leftt << ) | ((leftt >> )&1L)) & 0xffffffffL; for (round = ; round < ; round++) {
work = (right << ) | (right >> );
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> ) & 0x3fL];
fval |= SP3[(work >> ) & 0x3fL];
fval |= SP1[(work >> ) & 0x3fL];
work = right ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> ) & 0x3fL];
fval |= SP4[(work >> ) & 0x3fL];
fval |= SP2[(work >> ) & 0x3fL];
leftt ^= fval;
work = (leftt << ) | (leftt >> );
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> ) & 0x3fL];
fval |= SP3[(work >> ) & 0x3fL];
fval |= SP1[(work >> ) & 0x3fL];
work = leftt ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> ) & 0x3fL];
fval |= SP4[(work >> ) & 0x3fL];
fval |= SP2[(work >> ) & 0x3fL];
right ^= fval;
} right = (right << ) | (right >> );
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = (leftt << ) | (leftt >> );
work = ((leftt >> ) ^ right) & 0x00ff00ffL;
right ^= work;
leftt ^= (work << );
work = ((leftt >> ) ^ right) & 0x33333333L;
right ^= work;
leftt ^= (work << );
work = ((right >> ) ^ leftt) & 0x0000ffffL;
leftt ^= work;
right ^= (work << );
work = ((right >> ) ^ leftt) & 0x0f0f0f0fL;
leftt ^= work;
right ^= (work << );
*block++ = right;
*block = leftt;
return;
} /*****************************************************************
OPENCOMM_DesExpandEncKey : Expand Des Enc Key 扩展des加密密钥
Return value:
0 : Success
other : failed
Parameters:
pbDesKey : 扩展前的DES密钥(8字节) input
ulDesKeyLen : 扩展前的DES密钥长度 input
pbDesEncKey : 扩展后的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[], dough[]; if (ulDesKeyLen != )
return 0xEE20; deskey(pbDesKey, EN0, kn);
cookey(kn, dough);
*ulDesEncKeyLen = DES_KEYBYTES; //32 long = 128 bytes
memcpy(pbDesEncKey, dough, *ulDesEncKeyLen); return ;
} /*****************************************************************
OPENCOMM_DesExpandDecKey : Expand Des Dec Key 扩展des解密密钥
Return value:
0 : Success
other : failed
Parameters:
pbDesKey : 扩展前的DES密钥(8字节) input
ulDesKeyLen : 扩展前的DES密钥长度 input
pbDesDecKey : 扩展后的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[], dough[]; if (ulDesKeyLen != )
return 0xEE20; deskey(pbDesKey, DE1, kn);
cookey(kn, dough);
*ulDesDecKeyLen = DES_KEYBYTES; //32 long = 128 bytes
memcpy(pbDesDecKey, dough, *ulDesDecKeyLen); return ;
} /****************************************************************
OPENCOMM_DesEncRaw : Des算法加密小整块明文8字节
Return value:
0 : Success
other : failed
Parameters:
pbDesEncKey : DES加密密钥 input
ulDesEncKeyLen : DES加密密钥长度 input
pbInData : 待加密的明文 input
ulInDataLen : 待加密的明文长度 input
pbOutData : 加密后的密文 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[], ek[DES_KEYLONGS];
unsigned char cp[DES_BLOCKLEN]; if (ulInDataLen != DES_BLOCKLEN)
return 0xEE20; if (ulDesEncKeyLen != 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 = DES_BLOCKLEN; return ;
} /*****************************************************************
OPENCOMM_DesDecRaw : Des算法解密小整块密文8字节
Return value:
0 : Success
other : failed
Parameters:
pbDesDecKey : DES解密密钥 input
ulDesDecKeyLen : DES解密密钥长度 input
pbInData : 待解密的密文 input
ulInDataLen : 待解密的密文长度 input
pbOutData : 解密后的明文 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[], dk[DES_KEYLONGS];
unsigned char cp[DES_BLOCKLEN]; if (ulInDataLen != DES_BLOCKLEN)
return 0xEE20; if (ulDesDecKeyLen != 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 = DES_BLOCKLEN; return ;
} /********************* DES *********************/ int myic_DESEncrypt(
unsigned char *pDesKey,
int nDesKeyLen,
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen)
{
unsigned char DesKeyBuf[];
unsigned char DesEncKeyBuf[];
int EncKeyLen, KeyLen = ;
int retval = , loops, i; if(nInDataLen% != )
return 0xEE20; if(nDesKeyLen != )
return 0xEE20;
KeyLen = nDesKeyLen;
memcpy(DesKeyBuf, pDesKey, nDesKeyLen); retval = OPENCOMM_DesExpandEncKey(DesKeyBuf, KeyLen,
DesEncKeyBuf, (unsigned long *)&EncKeyLen);
if(retval != )
return retval; loops = nInDataLen/;
for(i = ; i < loops; i++)
{
retval = OPENCOMM_DesEncRaw(DesEncKeyBuf, EncKeyLen, pInData + i*,
, pOutData + i*, (unsigned long *)pOutDataLen);
if(retval != )
return retval;
}
*pOutDataLen = nInDataLen;
return retval;
} int myic_DESDecrypt(
unsigned char *pDesKey,
int nDesKeyLen,
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen)
{
unsigned char DesKeyBuf[];
unsigned char DesDecKeyBuf[];
int DecKeyLen, KeyLen = ;
int retval = , loops, i; if(nInDataLen% != )
return 0xEE20; if(nDesKeyLen != )
return 0xEE20;
KeyLen = nDesKeyLen;
memcpy(DesKeyBuf, pDesKey, nDesKeyLen); retval = OPENCOMM_DesExpandDecKey(DesKeyBuf, KeyLen,
DesDecKeyBuf, (unsigned long *)&DecKeyLen);
if(retval != )
return retval; loops = nInDataLen/;
for(i = ; i < loops; i++)
{
retval = OPENCOMM_DesDecRaw(DesDecKeyBuf, DecKeyLen, pInData + i*,
, pOutData + i*, (unsigned long *)pOutDataLen);
if(retval != )
return retval;
}
*pOutDataLen = 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 = NULL; pTmp = (unsigned char *)malloc(dateLen+);
if (pTmp == NULL)
{
*padDate = NULL;
return ;
}
memset(pTmp, , dateLen+);
memcpy(pTmp, date, dateLen); if (tag == )
{
padLen = - dateLen % ;
for (i=; i<padLen; i++)
{
pTmp[dateLen+i] = (char)padLen;
}
*padDateLen = dateLen + padLen;
}
else
{
padLen = - dateLen % ;
for (i=; i<padLen; i++)
{
pTmp[dateLen+i] = (char)padLen;
}
} *padDateLen = dateLen + padLen;
*padDate = pTmp;
} #define USER_PASSWORD_KEY "abcd1234" //数据加密
int DesEnc(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen)
{
int rv;
unsigned char *padDate = NULL;
unsigned int padDateLen = ; CW_dataPadAdd(, pInData, (unsigned int )nInDataLen, &padDate, &padDateLen); rv = myic_DESEncrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),
padDate, (int)padDateLen, pOutData, pOutDataLen);
if (rv != )
{
if (padDate != NULL)
{
free(padDate);
}
return rv;
} if (padDate != NULL)
{
free(padDate);
}
return ;
} //数据加密
int DesEnc_raw(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen)
{
int rv;
unsigned char *padDate = NULL;
unsigned int padDateLen = ; rv = myic_DESEncrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),
pInData, (int)nInDataLen, pOutData, pOutDataLen);
if (rv != )
{
return rv;
}
return ;
} //解密分配内存错误
#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 = NULL; tmpPlain = (unsigned char *)malloc(nInDataLen+);
if (tmpPlain == NULL)
{
return ERR_MALLOC;
}
memset(tmpPlain, , nInDataLen+); //解密
rv = myic_DESDecrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),
pInData, nInDataLen, tmpPlain, pOutDataLen);
if (rv != )
{
if (tmpPlain != NULL) free(tmpPlain);
return rv;
} //去pading
padChar = tmpPlain[*pOutDataLen - ];
if ( (int)padChar<= || (int)padChar>) //异常处理
{
if (tmpPlain) free(tmpPlain);
return ERR_FILECONT;
} *pOutDataLen = *pOutDataLen - (int)padChar;
//memset(tmpPlain + *pOutDataLen, 0, (int)padChar);
memcpy(pOutData, tmpPlain, *pOutDataLen);
if (tmpPlain) free(tmpPlain);
return ;
} //用户使用函数des解密
int DesDec_raw(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen)
{
int rv;
//char padChar;
//unsigned char *tmpPlain = NULL; /*
tmpPlain = (unsigned char *)malloc(nInDataLen+24);
if (tmpPlain == NULL)
{
return ERR_MALLOC;
}
memset(tmpPlain, 0, nInDataLen+24);
*/ //解密
rv = myic_DESDecrypt((unsigned char *)USER_PASSWORD_KEY, strlen(USER_PASSWORD_KEY),
pInData, nInDataLen, pOutData, pOutDataLen);
if (rv != )
{
//if (tmpPlain != NULL) free(tmpPlain);
return rv;
}
/*
//去pading
padChar = tmpPlain[*pOutDataLen - 1];
if ( (int)padChar<=0 || (int)padChar>8) //异常处理
{
if (tmpPlain) free(tmpPlain);
return ERR_FILECONT;
} *pOutDataLen = *pOutDataLen - (int)padChar;
//memset(tmpPlain + *pOutDataLen, 0, (int)padChar);
memcpy(pOutData, tmpPlain, *pOutDataLen);
if (tmpPlain) free(tmpPlain);
*/
return ;
}
#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "des.h" int main11()
{
int ret = ;
char *plain = "";//明文
int plainlen = strlen(plain); char plain2[] = {};//明文
int plainlen2 = ; unsigned char cryptbuf[] = {};//密文
int cryptlen = ;
//用户使用的函数
ret = DesEnc(plain, plainlen, cryptbuf, &cryptlen);
if (ret != )
{
printf("func DesEnc() err:%d \n");
return ret;
} //用户使用函数des解密
ret = DesDec(cryptbuf, cryptlen, plain2, &plainlen2);
if (ret != )
{
printf("func DesDec() err:%d \n");
return ret;
} if (plainlen != plainlen2)
{
ret = -;
printf("明文长度和解密后的明文长度 不一样 \n");
return ret;
} if (memcmp(plain2, plain, plainlen) == )
{
printf("明文长度和解密后的明文长度 一样 ok \n");
}
else
{
printf("明文长度和解密后的明文长度 不一样 err \n");
} system("pause");
return ret ;
}
#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "des.h" int FileSymEnc(const char *pfile1, const char *pfile2)//pfile1输入文件,
{
int ret = ;
FILE *fp1= NULL, *fp2 = NULL;
unsigned char plain[];
int plainlen = ; unsigned char cryptbuf[] = {};
int cryptlen = ; int tmplen; fp1 = fopen(pfile1, "rb");//读的方式打开文件
if (fp1 == NULL)
{
goto END;
} fp2 = fopen(pfile2, "wb");//写的方式打开文件
if (fp2 == NULL)
{
goto END;
} while (!feof(fp1))//每次读取4k
{
plainlen = fread(plain, , , fp1);//每次读取4k
if (feof(fp1)) //读完数据以后,判断是否文件结束
{
break;//文件不足以4k,则跳出
} //加密==4k的数据
ret = DesDec_raw(plain, plainlen, cryptbuf, &cryptlen);
if (ret != )
{
printf("func DesEnc() err:%d \n", ret);
goto END;
} tmplen = fwrite(cryptbuf, , cryptlen, fp2);
if (tmplen != cryptlen)//明文和密文的长度是一样的
{
ret = -;
printf("写密文文件失败,请检查是否磁盘已满\n");
goto END;
} //if (plainlen == 4096)
} //加密小于4k的数据
ret = DesEnc(plain, plainlen, cryptbuf, &cryptlen);
if (ret != )
{
printf("func DesEnc() err:%d \n", ret);
goto END;
} tmplen = fwrite(cryptbuf, , cryptlen, fp2);
if (cryptlen != tmplen)//磁盘已满
{
ret = -;
printf("写小于4k文件密文失败,请检查是否磁盘已满\n");
goto END;
} END:
if (fp1 != NULL)
{
fclose(fp1);
}
if (fp2 != NULL)
{
fclose(fp2);
}
return ;
} void main()
{
int ret = ;
const char *file1 = "c:/socketclient.dll";
const char *file2 = "c:/socketclientend.dll"; //const char *file1 = "c:/22.txt";
//const char *file2 = "c:/22enc.txt"; ret = FileSymEnc(file1, file2);
if (ret != )
{
printf("func FileSymEnc() err\n " );
return ;
} system("pause");
return ;
}

C加密解密的更多相关文章

  1. PHP的学习--RSA加密解密

    PHP服务端与客户端交互或者提供开放API时,通常需要对敏感的数据进行加密,这时候rsa非对称加密就能派上用处了. 举个通俗易懂的例子,假设我们再登录一个网站,发送账号和密码,请求被拦截了. 密码没加 ...

  2. 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输

    Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...

  3. .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现

    场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...

  4. php使用openssl进行Rsa长数据加密(117)解密(128) 和 DES 加密解密

    PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...

  5. c#和js互通的AES加密解密

    一.使用场景 在使用前后端分离的框架中常常会进行传输数据相互加密解密以确保数据的安全性,如web Api返回加密数据客户端或web端进行解密,或者客户端或web端进行加密提交数据服务端解密数据等等. ...

  6. PHP AES的加密解密

    AES加密算法 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DE ...

  7. [PHP]加密解密函数

    非常给力的authcode加密函数,Discuz!经典代码(带详解) function authcode($string, $operation = 'DECODE', $key = '', $exp ...

  8. 非对称技术栈实现AES加密解密

    非对称技术栈实现AES加密解密 正如前面的一篇文章所述,https协议的SSL层是实现在传输层之上,应用层之下,也就是说在应用层上看到的请求还是明码的,对于某些场景下要求这些http请求参数是非可读的 ...

  9. 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解

    原文地址:http://docode.top/Article/Detail/10003 目录: 1..Net(C#)平台下Des加密解密源代码 2..Net(C#)平台下Aes加密解密源代码 3..N ...

  10. .Net(c#)加密解密之Aes和Des

    .Net(c#)加密解密工具类: /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelp ...

随机推荐

  1. TestNG官方文档中文版(2)-annotation(转)

    1. 介绍    TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器). 编写一个测试的 ...

  2. CSS选择器的兼容性

    CSS 1 CSS2.1 CSS3 :hover 在IE6中只有a元素可用.E:empty 貌似在webkit核心浏览器中有些小bug.如果这个bug依然存在,不太确定如何测试.IE6不支持.clas ...

  3. subclipse svn 在64位win7下报Failed to load JavaHL Library

  4. UVALive 4763

    一开始,没敢写,感觉会超时...其实就是暴力搜索.DFS #include<iostream> #include<stdio.h> #include<string.h&g ...

  5. Tkinter教程之Menu篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811321 '''Tkinter教程之Menu篇''''''1.创建一个简单的Menu'''# ...

  6. Protocol Buffers编码详解,例子,图解

    Protocol Buffers编码详解,例子,图解 本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力 ...

  7. JAVA 基础 重新开始

    之前做android开发,因为JAVA基础不牢固的原因,自己对写代码很不自信,很多时候要去找源码或者在相近的代码上修修改改以得到想要的结果,从某种意义上来说这根本算不上真正意义上的程序员.后来看到某位 ...

  8. 搭建linux下teamspeak3多人语音服务器

    最近项目中新的需求,需要支持多人在线实时通话.就安装测试一下teamspeak.http://www.teamspeak.com/ 主页有服务器版本和客户端版本供下载安装.软硬件环境: melot@m ...

  9. JavaFx版本植物大战僵尸

    http://www.cnblogs.com/lslvxy/archive/2013/04/17/3026711.html —————————————————————————————————————— ...

  10. Android问题-DelphiXE8安装后编译Android提示SDK无法更新问题(XE10也可以解决)

    资料来原:http://www.chenruixuan.com/archives/479.html (DelphiXE8 更新SDK)http://www.dfwlt.com/forum.php?mo ...