C与C#通讯加密之C语言DES的cbc pkcs7的实现

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif    功能:用mode = cbc , padding = pkcs7 来加密
InBlock.gif         如果to == NULL, 则返回加密后数据的长度
InBlock.gif    书写:evlon ,QQ:273352165
ExpandedBlockEnd.gif
*/

None.gif
int  des_ecb_pkcs7_encrypt(uchar *  from,  int  nLength,  uchar  *  to, uchar key[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int nSize = nLength % 8 ?(nLength + 7/ 8 * 8 : nLength + 8;
InBlock.gif    
if(to == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//计算长度
InBlock.gif
        return nSize;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        deskey(key,EN0);
InBlock.gif        uchar endBuf[
8];
InBlock.gif        
InBlock.gif        
int i=0;
InBlock.gif        
for(; i < nSize; i+=8)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            uchar
* ps = NULL,* pd = NULL;
InBlock.gif            
if(nLength - i >= 8)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ps 
= from + i;
InBlock.gif                pd 
= to + i;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                memset(
&endBuf, i + 8 - nLength, sizeof(endBuf));
InBlock.gif                memcpy(
&endBuf,from + i,nLength - i);
InBlock.gif                ps 
= endBuf;
InBlock.gif                pd 
= to + i;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            des(ps,pd);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
return i;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
int  des_ecb_pkcs7_decrypt(uchar *  from,  int  nLength,  uchar  *  to, uchar key[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(nLength % 8)
InBlock.gif        
return 0;    //数据不正确
InBlock.gif

InBlock.gif    deskey(key,DE1);
InBlock.gif    
int i = 0;
InBlock.gif    
for(; i < nLength; i+=8)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(nLength - i > 8)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            des(from 
+ i,to + i);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            uchar endBuf[
8];
InBlock.gif            des(from 
+ i,endBuf);
InBlock.gif            
InBlock.gif            
//去除数据尾
InBlock.gif
            uchar chEnd = endBuf[7];
InBlock.gif            
if(chEnd > 0 && chEnd < 9)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//有可能是填充字符,去除掉
InBlock.gif
                for(int j = 7; j >= 8 - chEnd; --j)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(endBuf[j] != chEnd)
InBlock.gif                        
return 0;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                memcpy(to 
+ i, endBuf, 8 - chEnd);
InBlock.gif                
InBlock.gif                
return i +  8 - chEnd;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 0;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif    功能:用mode = cbc , padding = pkcs7 来加密
InBlock.gif         如果to == NULL, 则返回加密后数据的长度
InBlock.gif    书写:evlon ,QQ:273352165
ExpandedBlockEnd.gif
*/

None.gif
int  des_cbc_pkcs7_encrypt(uchar *  from,  int  nLength,  uchar  *  to, uchar key[],uchar iv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int nSize = nLength % 8 ?(nLength + 7/ 8 * 8 : nLength + 8;
InBlock.gif    
if(to == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//计算长度
InBlock.gif
        return nSize;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        deskey(key,EN0);
InBlock.gif        uchar
* fromBackup = from;
InBlock.gif        from 
= (uchar*)malloc(nSize);    //申请内存
InBlock.gif
        memcpy(from,fromBackup,nLength);
InBlock.gif        memset(from 
+ nLength, nSize - nLength,nSize - nLength);
InBlock.gif        uchar preEnc[
8];
InBlock.gif        memcpy(preEnc,iv,
8);
InBlock.gif        
InBlock.gif        
//加密块
InBlock.gif
        int i=0;
InBlock.gif        
for(; i < nSize; i+=8)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            uchar
*     ps = from + i;
InBlock.gif            uchar
*     pd = to + i;
InBlock.gif
InBlock.gif            
//XOR
InBlock.gif
            for(int j = 0; j < 8++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ps[j] 
^= preEnc[j];
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            des(ps,pd);
InBlock.gif
InBlock.gif            
//保存前一个输出
InBlock.gif
            memcpy(preEnc, pd,8);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        free(from);
InBlock.gif
InBlock.gif        
//memcpy(to + i,preEnc,8);
InBlock.gif

InBlock.gif
InBlock.gif        
return i;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
int  des_cbc_pkcs7_decrypt(uchar *  from,  int  nLength,  uchar  *  to, uchar key[], uchar iv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(nLength % 8)
InBlock.gif        
return 0;    //数据不正确
InBlock.gif

InBlock.gif
InBlock.gif    uchar
* toBackup = to;
InBlock.gif    to 
= (uchar*)malloc(nLength);    //申请内存
InBlock.gif
InBlock.gif    
//XOR
InBlock.gif
    uchar preEnc[8];
InBlock.gif    memcpy(preEnc,iv,
8);
InBlock.gif
InBlock.gif    deskey(key,DE1);
InBlock.gif
InBlock.gif    
int i = 0;
InBlock.gif    
for(; i < nLength; i+=8)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        uchar
*     ps = from + i;
InBlock.gif        uchar
*     pd = to + i;
InBlock.gif
InBlock.gif        des(ps,pd);
InBlock.gif
InBlock.gif        
//XOR
InBlock.gif
        for(int j = 0; j < 8++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            pd[j] 
^= preEnc[j];
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//保存前一个输出
InBlock.gif
        memcpy(preEnc, ps,8);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
//去除数据尾
InBlock.gif
    uchar chEnd = to[nLength - 1];
InBlock.gif    
if(chEnd > 0 && chEnd < 9)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//有可能是填充字符,去除掉
InBlock.gif
        for(int j = nLength - 1; j >= nLength - chEnd; --j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(to[j] != chEnd)
InBlock.gif                
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int nSize =nLength - chEnd;
InBlock.gif
InBlock.gif        memcpy(toBackup, to, nSize);
InBlock.gif        free(to);
InBlock.gif        
return nSize;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//数据格式不正确
InBlock.gif
        free(to);return 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif


测试代码如下:

None.gif // 把字节数组转换成字串
None.gif
int  Byte2String(uchar *  bytes,  int  nLength,  char *  pszout)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(!pszout)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return nLength * 3;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for (int i = 0; i < nLength; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//sb.AppendFormat("{0} ", b.ToString("X2"));
InBlock.gif
            sprintf(pszout + i * 3,"%2X ",bytes[i]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
*(pszout + nLength * 3 -1= '\0';
InBlock.gif        
return nLength * 3;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /
InBlock.gif
/    测试代码                 ///
ExpandedBlockEnd.gif
/

None.gif
None.gif
void  cbcDesTest1()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar key[
8= dot.gif{1,2,3,4,5,6,7,8};
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar iv[
8= dot.gif{1,2,3,4,5,6,7,8};
InBlock.gif    
InBlock.gif    uchar text[
25= "123456789abcdef中国人ok";
InBlock.gif    uchar 
*output = NULL;
InBlock.gif    uchar text2[
sizeof(text)];
InBlock.gif    memset(
&text2,0,sizeof(text2));
InBlock.gif
InBlock.gif    
int nLength = des_cbc_pkcs7_encrypt(text,24,NULL,key,iv);
InBlock.gif    output 
= new uchar[nLength];
InBlock.gif    memset(output,
0,nLength);
InBlock.gif
InBlock.gif    des_cbc_pkcs7_encrypt(text,
24,output,key,iv);
InBlock.gif    des_cbc_pkcs7_decrypt(output,
24,text2,key,iv);
InBlock.gif
InBlock.gif    delete[] output;
InBlock.gif    output 
= NULL;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  cbcDesTest2()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
char szPrint[1024];
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar key[
8= dot.gif{1,2,3,4,5,6,7,8};
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar iv[
8= dot.gif{1,2,3,4,5,6,7,8};
InBlock.gif
InBlock.gif    StringPack pack;
InBlock.gif    pack.OpCode 
= 2;
InBlock.gif    pack.CharEnd 
= '8';
InBlock.gif    strcpy(pack.Name, 
"123456789abcdef中国人ok");
InBlock.gif
InBlock.gif    uchar
* pPack = (uchar*)&pack;
InBlock.gif
InBlock.gif    
//计算数据长度
InBlock.gif
    int nLength = des_cbc_pkcs7_encrypt(pPack,sizeof(StringPack),NULL,key,iv);
InBlock.gif    uchar
* output = new uchar[nLength];
InBlock.gif    memset(output,
0,nLength);
InBlock.gif
InBlock.gif    
//打印出来
InBlock.gif
    Byte2String(pPack,sizeof(StringPack),szPrint);
InBlock.gif    printf(
"%s\n",szPrint);
InBlock.gif
InBlock.gif    
//加密
InBlock.gif
    des_cbc_pkcs7_encrypt(pPack,sizeof(StringPack),output,key,iv);
InBlock.gif
InBlock.gif    
//打印出来
InBlock.gif
    Byte2String(output,nLength,szPrint);
InBlock.gif    printf(
"%s\n",szPrint);
InBlock.gif
InBlock.gif
InBlock.gif    
//检验一下加密结果,现在解密
InBlock.gif
    uchar *packData = new uchar[nLength];
InBlock.gif    memset(packData,
0,nLength);
InBlock.gif    des_cbc_pkcs7_decrypt(output,nLength,packData,key,iv);
InBlock.gif
InBlock.gif
InBlock.gif    
//好了,我们看一下结果吧
InBlock.gif
    StringPack* pack2 = (StringPack*)packData;
InBlock.gif
InBlock.gif    
//清理内存
InBlock.gif
    delete[] packData;
InBlock.gif    output 
= NULL;
InBlock.gif
InBlock.gif    delete[] output;
InBlock.gif    output 
= NULL;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  cbcDesTest3()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar key[
8= dot.gif{1,2,3,4,5,6,7,8};
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar iv[
8= dot.gif{1,2,3,4,5,6,7,8};
InBlock.gif
InBlock.gif    
// 7 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_cbc_pkcs7_encrypt(text,sizeof(text),output,key,iv);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_cbc_pkcs7_decrypt(output,nRet,text2,key,iv);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 8 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE00xE2};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_cbc_pkcs7_encrypt(text,sizeof(text),output,key,iv);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_cbc_pkcs7_decrypt(output,nRet,text2,key,iv);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
// 9 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE00xE2,0xF0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_cbc_pkcs7_encrypt(text,sizeof(text),output,key,iv);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_cbc_pkcs7_decrypt(output,nRet,text2,key,iv);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// N 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE00xE2,0xF00xA50xED0x9F0xA50xED0x9F0xA50xED0x9F};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_cbc_pkcs7_encrypt(text,sizeof(text),output,key,iv);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_cbc_pkcs7_decrypt(output,nRet,text2,key,iv);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  ecbDesTest1()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    uchar key[
8= dot.gif{1,2,3,4,5,6,7,8};
InBlock.gif
InBlock.gif    
// 7 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_ecb_pkcs7_encrypt(text,sizeof(text),output,key);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_ecb_pkcs7_decrypt(output,nRet,text2,key);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 8 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE00xE2};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_ecb_pkcs7_encrypt(text,sizeof(text),output,key);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_ecb_pkcs7_decrypt(output,nRet,text2,key);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
// 9 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE00xE2,0xF0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_ecb_pkcs7_encrypt(text,sizeof(text),output,key);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_ecb_pkcs7_decrypt(output,nRet,text2,key);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// N 位长
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text[] 
= dot.gif{0xF00xA50xED0x9F0x830x8A0xE00xE2,0xF00xA50xED0x9F0xA50xED0x9F0xA50xED0x9F};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar output[
1024= dot.gif{0};
ExpandedSubBlockStart.gifContractedSubBlock.gif        uchar text2[
1024= dot.gif{0};
InBlock.gif        
int nRet = des_ecb_pkcs7_encrypt(text,sizeof(text),output,key);
InBlock.gif        
char szPrint[1024];
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(output,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
InBlock.gif
InBlock.gif        nRet 
= des_ecb_pkcs7_decrypt(output,nRet,text2,key);
InBlock.gif
InBlock.gif        
//打印出来
InBlock.gif
        Byte2String(text2,nRet,szPrint);
InBlock.gif        printf(
"(%d)\n%s\n",nRet,szPrint);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
int  _tmain( int  argc, _TCHAR *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ecbDesTest1();
InBlock.gif    cbcDesTest1();
InBlock.gif    cbcDesTest2();
InBlock.gif    cbcDesTest3();
InBlock.gif    getchar();
InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值