WjcDes这个代码 如果单线程加密是好的。。如果多线程加密就开始出错了。请问问题在那里。
///////h
enum    {ENCRYPT,DECRYPT};
typedef bool (*PSubKey)[16][48];bool Des_Go(char *Out,char *In,long datalen,const char *Key,int keylen,int &out_length,bool Type = ENCRYPT);//////////////////////////////////////////////////////////////////////////static void DES(char Out[8], char In[8], const PSubKey pSubKey, bool Type,char *tmp_data);//标准DES加/解密
static bool SetKey(const char* Key, int len,char *tmp_data,PSubKey PS0,PSubKey PS1);// 设置密钥
static void SetSubKey(PSubKey pSubKey, const char Key[8],char *tmp_data);// 设置子密钥
static void F_func(bool In[32], const bool Ki[48],char *tmp_data);// f 函数
static void S_func(bool Out[32], const bool In[48]);// S 盒代替
static void Transform(bool *Out, bool *In, const char *Table, int len,char *tmp_data);// 变换
static void Xor(bool *InA, const bool *InB, int len);// 异或
static void RotateL(bool *In, int len, int loop,char *tmp_data);// 循环左移
static void ByteToBit(bool *Out, const char *In, int bits);// 字节组转换成位组
static void BitToByte(char *Out, const bool *In, int bits);// 位组转换成字节组
//cpp
//////////////////////////////////////////////////////////////////////////
// Code starts from Line 130
//critical_session m_lock;
//////////////////////////////////////////////////////////////////////////
bool Des_Go(char *Out, char *In, long datalen, const char *Key, int keylen,int &out_length, bool Type)
{
//  if( !( Out && In && Key && (datalen=(datalen+7)&0xfffffff8) ) ) 
//  return false;

char *Tmp_data=new char[256];
memset(Tmp_data,0x0,256);
    bool SubKey[2][16][48]={0}; //16位
    PSubKey a=new bool [1][16][48];
PSubKey b=new bool [1][16][48];
bool Is3DES=SetKey(Key, keylen,Tmp_data,a,b);
   
if( !Is3DES ) {   // 1次DES
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8,out_length+=8)
DES(Out, In, a, Type,Tmp_data);
} else{   // 3次DES 加密:加(key0)-解(key1)-加(key0) 解密::解(key0)-加(key1)-解(key0)
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8,out_length+=8) {
DES(Out, In,  a, Type,Tmp_data);
DES(Out, Out, b, !Type,Tmp_data);
DES(Out, Out, a, Type,Tmp_data);
             }
}

delete[] a;
delete[] b;
delete[] Tmp_data;
return true;
}
bool  SetKey(const char* Key, int len,char *tmp_data,PSubKey PS0,PSubKey PS1)
{
bool Is3DES=true;
char deskey[16];
memset(deskey, 0, 16);
memcpy(deskey, Key, len>16?16:len);
SetSubKey(PS0, &deskey[0],tmp_data);
Is3DES = len>8 ? (SetSubKey(PS1, &deskey[8],tmp_data), true) : false;
//delete[] deskey;
return Is3DES;
}
void DES(char Out[8], char In[8], const PSubKey pSubKey, bool Type,char *tmp_data)
{
static bool M[64], tmp[32], *Li=&M[0], *Ri=&M[32];
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64,tmp_data);
if( Type == ENCRYPT ){
for(int i=0; i<16; ++i) {
memcpy(tmp, Ri, 32);
F_func(Ri, (*pSubKey)[i],tmp_data);
Xor(Ri, Li, 32);
memcpy(Li, tmp, 32);
}
}else{
for(int i=15; i>=0; --i) {
memcpy(tmp, Li, 32);
F_func(Li, (*pSubKey)[i],tmp_data);
Xor(Li, Ri, 32);
memcpy(Ri, tmp, 32);
}
}
Transform(M, M, IPR_Table, 64,tmp_data);
BitToByte(Out, M, 64);
}
void SetSubKey(PSubKey pSubKey, const char Key[8],char *tmp_data)
{
static bool K[64], *KL=&K[0], *KR=&K[28];
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56,tmp_data);
for(int i=0; i<16; ++i) {
RotateL(KL, 28, LOOP_Table[i],tmp_data);
RotateL(KR, 28, LOOP_Table[i],tmp_data);
Transform((*pSubKey)[i], K, PC2_Table, 48,tmp_data);
}
}
void F_func(bool In[32], const bool Ki[48],char *tmp_data)
{
static bool MR[48];
Transform(MR, In, E_Table, 48,tmp_data);
Xor(MR, Ki, 48);
S_func(In, MR);
Transform(In, In, P_Table, 32,tmp_data);
}
void S_func(bool Out[32], const bool In[48])
{
for(char i=0,j,k; i<8; ++i,In+=6,Out+=4) {
j = (In[0]<<1) + In[5];
k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
ByteToBit(Out, &S_Box[i][j][k], 4);
}
}
void Transform(bool *Out, bool *In, const char *Table, int len,char *tmp_data)
{
char *p_data=tmp_data;
for(int i=0; i<len; ++i)
{    
// Tmp[i] = In[ Table[i]-1 ];
*p_data=In[ Table[i]-1 ]; 
p_data++;
}
memcpy(Out, tmp_data, len);
}
void Xor(bool *InA, const bool *InB, int len)
{
for(int i=0; i<len; ++i)
InA[i] ^= InB[i];
}
void RotateL(bool *In, int len, int loop,char *tmp_data)
{
memcpy(tmp_data, In, loop);
memcpy(In, In+loop, len-loop);
memcpy(In+len-loop, tmp_data, loop);
}
void ByteToBit(bool *Out, const char *In, int bits)
{
for(int i=0; i<bits; ++i)
Out[i] = (In[i>>3]>>(i&7)) & 1;
}
void BitToByte(char *Out, const bool *In, int bits)
{
memset(Out, 0, bits>>3);
for(int i=0; i<bits; ++i)
Out[i>>3] |= In[i]<<(i&7);
}

解决方案 »

  1.   

    看了你的程序,好象错误不在这个算法里!可能是你用别的地方用错了,比如调用GoDes函数中数据或者密钥有问题!
      

  2.   

    我倒是检查过代码了
    因为这个代码的全局变量已经被我修改到函数中了。如果我加锁的话当然可以。但是考虑到效率问题。。
    我外部调用就是用new 出来一个BUF 。然后传进去。NEW 在多线程里应该是安全的我一度怀疑算法有问题但是现在如果开多个线程(比如100)就有问题。。
      

  3.   

    这是我在线程里边的处理DWORD WINAPI threadFunc(LPVOID threadNum){
    for(int j=0;j<500;j++)
    { //m_lock.lock();
    string s_key_info="123456";
    des_data *p_des=new des_data;
    memset(p_des,0x0,sizeof(des_data));
    p_des->us_order=5;
    p_des->us_buff=5;
    p_des->us_data=5;
    p_des->us_buf=5;
           
    int n_out_len=0;
    int des_one=8; //des 特定值
    int length=sizeof(des_data);
    int quotient, remainder, i; 
    quotient =  length / des_one; 
    remainder = length % des_one; 
    quotient = remainder?++quotient:quotient; 
    char *buf=new char[quotient*des_one]; memset(buf,0x0,quotient*des_one);     
    // /m_lock.lock();
    Des_Go(buf, (char *)p_des, length, s_key_info.c_str(),s_key_info.size(),n_out_len, ENCRYPT);
    char *p_en_buf=new char[n_out_len];
    memset(p_en_buf,0x0,n_out_len);
    int n_first=0;
    Des_Go(p_en_buf,buf,n_out_len, s_key_info.c_str(),s_key_info.size(),n_first,DECRYPT);
    //m_lock.unlock();
    des_data *p_des_de=(des_data *)p_en_buf; ///这里的值可能是莫名其妙的大数了
      

  4.   

    把一些原来用户的全局的变量改为线程变量,__declspec( thread )int tls_i = 1;类似
      

  5.   

    当然的。。你看我的线程里边的处理就是。。NEW 一个STRUCT 。然后加密我而且把之前作者写的全局的变量放到函数里边了。类也封过了。。但是问题依然。
    所以我就直接测试作者的函数了。。
      

  6.   

    有兴趣的朋友可以留下MSN。发代码
      

  7.   

    把一些需要在整组DES加密函数当中多次使用(比如先在初始化函数当中初始化,然后再到加密函数当中使用的),要么采用线程变量(使用线程局部存储空间),要么使用栈内存/堆内存(动态分配),然后在调用后续函数的时候以参数传递过去。
      

  8.   

    看了你的线程对应的函数,觉得是你的的数据有问题,被加密或者被解密的数据必须为8字节的倍数,不够8字节的应该用0填充,因为DES算法是按照8字节数据进行处理的,我不知道你的des_data数据是不是符合这个条件。
      

  9.   

    是要求输出缓冲区必须是8的倍数
    比如 需加密长度 为7   我开辟长度为8 的BUF。解密数据 。我开辟长度为解密长度一样就好。。
      

  10.   

    我进去加密的那个结构体其实就是 4个INT  就是16啊所以应该不是的。