当前位置: 技术问答>linux和unix
求助DES加密问题
来源: 互联网 发布时间:2016-12-24
本文导语: 我想测试一下DES_ncbc_encrypt,所以写了段代码: void cbc_test() { DES_cblock key; DES_cblock ivec; DES_key_schedule schedule; UINT8 input[8]; UINT8 output[8]; int i; for (i = 0; i =0; l-=8) { c2l...
我想测试一下DES_ncbc_encrypt,所以写了段代码:
void cbc_test()
{
DES_cblock key;
DES_cblock ivec;
DES_key_schedule schedule;
UINT8 input[8];
UINT8 output[8];
int i;
for (i = 0; i =0; l-=8)
{
c2l(in,tin0);
c2l(in,tin1);
tin0^=tout0; tin[0]=tin0;
tin1^=tout1; tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
tout0=tin[0]; l2c(tout0,out);
tout1=tin[1]; l2c(tout1,out);
}
if (l != -8)
{
c2ln(in,tin0,tin1,l+8);
tin0^=tout0; tin[0]=tin0;
tin1^=tout1; tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
tout0=tin[0]; l2c(tout0,out);
tout1=tin[1]; l2c(tout1,out);
}
#ifndef CBC_ENC_C__DONT_UPDATE_IV
iv = &(*ivec)[0];
l2c(tout0,iv);
l2c(tout1,iv);
#endif
}
else
{
c2l(iv,xor0);
c2l(iv,xor1);
for (l-=8; l>=0; l-=8)
{
c2l(in,tin0); tin[0]=tin0;
c2l(in,tin1); tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2c(tout0,out);
l2c(tout1,out);
xor0=tin0;
xor1=tin1;
}
if (l != -8)
{
c2l(in,tin0); tin[0]=tin0;
c2l(in,tin1); tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2cn(tout0,tout1,out,l+8);
#ifndef CBC_ENC_C__DONT_UPDATE_IV
xor0=tin0;
xor1=tin1;
#endif
}
#ifndef CBC_ENC_C__DONT_UPDATE_IV
iv = &(*ivec)[0];
l2c(xor0,iv);
l2c(xor1,iv);
#endif
}
tin0=tin1=tout0=tout1=xor0=xor1=0;
tin[0]=tin[1]=0;
}
void cbc_test()
{
DES_cblock key;
DES_cblock ivec;
DES_key_schedule schedule;
UINT8 input[8];
UINT8 output[8];
int i;
for (i = 0; i =0; l-=8)
{
c2l(in,tin0);
c2l(in,tin1);
tin0^=tout0; tin[0]=tin0;
tin1^=tout1; tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
tout0=tin[0]; l2c(tout0,out);
tout1=tin[1]; l2c(tout1,out);
}
if (l != -8)
{
c2ln(in,tin0,tin1,l+8);
tin0^=tout0; tin[0]=tin0;
tin1^=tout1; tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
tout0=tin[0]; l2c(tout0,out);
tout1=tin[1]; l2c(tout1,out);
}
#ifndef CBC_ENC_C__DONT_UPDATE_IV
iv = &(*ivec)[0];
l2c(tout0,iv);
l2c(tout1,iv);
#endif
}
else
{
c2l(iv,xor0);
c2l(iv,xor1);
for (l-=8; l>=0; l-=8)
{
c2l(in,tin0); tin[0]=tin0;
c2l(in,tin1); tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2c(tout0,out);
l2c(tout1,out);
xor0=tin0;
xor1=tin1;
}
if (l != -8)
{
c2l(in,tin0); tin[0]=tin0;
c2l(in,tin1); tin[1]=tin1;
DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2cn(tout0,tout1,out,l+8);
#ifndef CBC_ENC_C__DONT_UPDATE_IV
xor0=tin0;
xor1=tin1;
#endif
}
#ifndef CBC_ENC_C__DONT_UPDATE_IV
iv = &(*ivec)[0];
l2c(xor0,iv);
l2c(xor1,iv);
#endif
}
tin0=tin1=tout0=tout1=xor0=xor1=0;
tin[0]=tin[1]=0;
}
|
ivec叫做初始化向量。CBC(cipher-block-chaining)模式是一种链式加密模式
明文按8字节分块:P1,P2,...Pn,
密文按8字节分块:C1,C2,...Cn.
初始化向量为 C0
加密过程为:
C1 = DES(P1 XOR C0), C2 = DES(P2 XOR C1) ... Cn = (Pn XOR Cn-1)
楼主的问题在于DES_ncbc_encrypt会改变ivec,
DES_ncbc_encrypt(input, output, 8, &schedule, &ivec, DES_ENCRYPT);
运行后ivec等于最后一块密文Pn,这样便于继续进行CBC加密。
楼主需要重新设置ivec, 揭秘才能得到正确结果。
#include
#include
int main(void)
{
DES_cblock key;
DES_cblock ivec;
DES_key_schedule schedule;
DES_cblock input;
DES_cblock output;
int i;
for (i = 0; i