C与C#通讯加密之C语言DES的cbc pkcs7的实现(二)
没有想到,上个随笔的代码在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[])
{
//uchar buffer[8];
int nSize = nLength % 8 ?(nLength + 7) / 8 * 8 : nLength + 8;
if(to == NULL)
{
//计算长度
return nSize;
}
else
{
deskey(key,EN0);
uchar preEnc[8];
memcpy(preEnc,iv,8);
//加密块
int i=0;
for(; i < nSize; i+=8)
{
uchar* ps = from + i;
uchar* pd = to + i;
if(nSize - i > 8)
{
//XOR
for(int j = 0; j < 8; ++j)
{
preEnc[j] ^= *(ps + j);
}
}
else
{
//XOR
for(int j = 0; j < nLength - i; ++j)
{
preEnc[j] ^= *(ps + j);
}
for(int j = nLength - i; j < 8; ++j)
{
preEnc[j] ^= nSize - nLength;
}
}
des(preEnc,pd);
//保存前一个输出
memcpy(preEnc, pd,8);
}
return i;
}
}
int des_cbc_pkcs7_decrypt(uchar* from, int nLength, uchar * to, uchar key[], uchar iv[])
{
if(nLength % 8)
return 0; //数据不正确
//uchar* toBackup = to;
//to = (uchar*)malloc(nLength); //申请内存
//XOR
uchar preEnc[8],buffer[8];
memcpy(preEnc,iv,8);
deskey(key,DE1);
int i = 0;
for(; i < nLength; i+=8)
{
uchar* ps = from + i;
uchar* pd = to + i;
des(ps,buffer);
//XOR
for(int j = 0; j < 8; ++j)
{
buffer[j] ^= preEnc[j];
}
if(nLength - i > 8)
{
//保存前一个输出
memcpy(preEnc, ps,8);
memcpy(pd,buffer,sizeof(buffer));
}
else
{
//去除数据尾
uchar chEnd = buffer[sizeof(buffer) - 1];
if(chEnd > 0 && chEnd < 9)
{
//有可能是填充字符,去除掉
for(int j = sizeof(buffer) - 1; j >= (int)(sizeof(buffer) - chEnd); --j)
{
if(buffer[j] != chEnd)
return 0;
}
int nSize =nLength - chEnd;
memcpy(pd, buffer, sizeof(buffer) - chEnd);
return nLength - chEnd;
}
else
{
//数据格式不正确
return 0;
}
}
}
return 0;
}
功能:用mode = cbc , padding = pkcs7 来加密
如果to == NULL, 则返回加密后数据的长度
书写:evlon ,QQ:273352165
*/
int des_cbc_pkcs7_encrypt(uchar* from, int nLength, uchar * to, uchar key[],uchar iv[])
{
//uchar buffer[8];
int nSize = nLength % 8 ?(nLength + 7) / 8 * 8 : nLength + 8;
if(to == NULL)
{
//计算长度
return nSize;
}
else
{
deskey(key,EN0);
uchar preEnc[8];
memcpy(preEnc,iv,8);
//加密块
int i=0;
for(; i < nSize; i+=8)
{
uchar* ps = from + i;
uchar* pd = to + i;
if(nSize - i > 8)
{
//XOR
for(int j = 0; j < 8; ++j)
{
preEnc[j] ^= *(ps + j);
}
}
else
{
//XOR
for(int j = 0; j < nLength - i; ++j)
{
preEnc[j] ^= *(ps + j);
}
for(int j = nLength - i; j < 8; ++j)
{
preEnc[j] ^= nSize - nLength;
}
}
des(preEnc,pd);
//保存前一个输出
memcpy(preEnc, pd,8);
}
return i;
}
}
int des_cbc_pkcs7_decrypt(uchar* from, int nLength, uchar * to, uchar key[], uchar iv[])
{
if(nLength % 8)
return 0; //数据不正确
//uchar* toBackup = to;
//to = (uchar*)malloc(nLength); //申请内存
//XOR
uchar preEnc[8],buffer[8];
memcpy(preEnc,iv,8);
deskey(key,DE1);
int i = 0;
for(; i < nLength; i+=8)
{
uchar* ps = from + i;
uchar* pd = to + i;
des(ps,buffer);
//XOR
for(int j = 0; j < 8; ++j)
{
buffer[j] ^= preEnc[j];
}
if(nLength - i > 8)
{
//保存前一个输出
memcpy(preEnc, ps,8);
memcpy(pd,buffer,sizeof(buffer));
}
else
{
//去除数据尾
uchar chEnd = buffer[sizeof(buffer) - 1];
if(chEnd > 0 && chEnd < 9)
{
//有可能是填充字符,去除掉
for(int j = sizeof(buffer) - 1; j >= (int)(sizeof(buffer) - chEnd); --j)
{
if(buffer[j] != chEnd)
return 0;
}
int nSize =nLength - chEnd;
memcpy(pd, buffer, sizeof(buffer) - chEnd);
return nLength - chEnd;
}
else
{
//数据格式不正确
return 0;
}
}
}
return 0;
}
QQ:273352165
evlon#126.com
转载请注明出处。