没有想到,上个随笔的代码在POS机上出错,查了半天,如果去掉malloc和free,就没有事了。
所以,把现在的不用动态内存分配的算法记录下
/**/
/*
功能:用mode = cbc , padding = pkcs7 来加密
如果to == NULL, 则返回加密后数据的长度
书写:evlon ,QQ:273352165
*/
int
des_cbc_pkcs7_encrypt(uchar
*
from,
int
nLength, uchar
*
to, uchar key[],uchar iv[])
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//uchar buffer[8];
int nSize = nLength % 8 ?(nLength + 7) / 8 * 8 : nLength + 8;
if(to == NULL)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//计算长度
return nSize;
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
deskey(key,EN0);
uchar preEnc[8];
memcpy(preEnc,iv,8);
//加密块
int i=0;
for(; i < nSize; i+=8)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
uchar* ps = from + i;
uchar* pd = to + i;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(nSize - i > 8)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//XOR
for(int j = 0; j < 8; ++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
preEnc[j] ^= *(ps + j);
}
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//XOR
for(int j = 0; j < nLength - i; ++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
preEnc[j] ^= *(ps + j);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
for(int j = nLength - i; j < 8; ++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
preEnc[j] ^= nSize - nLength;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
des(preEnc,pd);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//保存前一个输出
memcpy(preEnc, pd,8);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
return i;
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
int
des_cbc_pkcs7_decrypt(uchar
*
from,
int
nLength, uchar
*
to, uchar key[], uchar iv[])
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(nLength % 8)
return 0; //数据不正确
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//uchar* toBackup = to;
//to = (uchar*)malloc(nLength); //申请内存
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//XOR
uchar preEnc[8],buffer[8];
memcpy(preEnc,iv,8);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
deskey(key,DE1);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
int i = 0;
for(; i < nLength; i+=8)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
uchar* ps = from + i;
uchar* pd = to + i;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
des(ps,buffer);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//XOR
for(int j = 0; j < 8; ++j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
buffer[j] ^= preEnc[j];
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(nLength - i > 8)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//保存前一个输出
memcpy(preEnc, ps,8);
memcpy(pd,buffer,sizeof(buffer));
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//去除数据尾
uchar chEnd = buffer[sizeof(buffer) - 1];
if(chEnd > 0 && chEnd < 9)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//有可能是填充字符,去除掉
for(int j = sizeof(buffer) - 1; j >= (int)(sizeof(buffer) - chEnd); --j)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(buffer[j] != chEnd)
return 0;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
int nSize =nLength - chEnd;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
memcpy(pd, buffer, sizeof(buffer) - chEnd);
return nLength - chEnd;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//数据格式不正确
return 0;
}
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
return 0;
}