C++ DES 加密(Cryptopp )

转载注明出处

http://blog.csdn.net/xugangjava



写了一个DES加密的插件,用的是crypto++库,各种字符的转换有点晕

插件是ATL的Activex 只贴出关键代码,希望对你有用,如有不足欢迎指出。

//加密代码

STDMETHODIMP CMAddr::En(BSTR instr, BSTR* outstr)
{
CComBSTR com_instr(instr);


if(com_instr.Length()==0){
com_instr.Empty();
return S_OK;
}


int pos=0;
const int bsize=DES::BLOCKSIZE;
char* ptr_instr=NULL;
CString cstr_result;
CString cstr_buffer;
DESEncryption encryption_DES;
unsigned char key[ DES::KEYLENGTH ] = "xugangf";
byte blockin[bsize];
byte blockout[bsize];
        //设置密钥
encryption_DES.SetKey(key, DES::KEYLENGTH);


int nwLen=0;
        //将宽字节字符集转成多字节,crypto 没unicode的版本让我比较蛋疼
nwLen=WideCharToMultiByte(CP_ACP,NULL,com_instr.m_str,-1,NULL,0,NULL,FALSE);
ptr_instr=new char[nwLen+bsize+1];
if(!ptr_instr)
{
delete[] ptr_instr;
return FALSE;
}
WideCharToMultiByte(CP_ACP,NULL,com_instr.m_str,-1,ptr_instr,nwLen,NULL,FALSE);


while(pos<nwLen)
{
ZeroMemory(blockin,bsize);
ZeroMemory(blockout,bsize);
memcpy_s(blockin,bsize,ptr_instr+pos,bsize);
                //解密数据块
encryption_DES.ProcessBlock(blockin, blockout);
//转换成16进制数字输出,不然加密后是乱码而且无法解密,
//codepage找不到这个字符就给你个乱码
//你用乱码来解密,解出来也是乱码
for(int bpos=0;bpos<bsize;bpos++)
{
cstr_buffer.Format(L"%02x",blockout[bpos]);
cstr_result.Append(cstr_buffer);
}
pos+=bsize;
}
       //免得内存泄露,清理一下
com_instr.Empty();
delete[] ptr_instr;
CComBSTR result;
bstr_t  bstr_result(cstr_result.LockBuffer()); 
result.Append(bstr_result.GetBSTR());
result.CopyTo(outstr);
cstr_result.UnlockBuffer();
result.Empty();
cstr_result.Empty();
cstr_buffer.Empty();

return S_OK;
}



//解密代码
STDMETHODIMP CMAddr::De(BSTR instr, BSTR* outstr)
{
CComBSTR com_instr(instr);
if(com_instr.Length()==0){
com_instr.Empty();
return S_OK;
}
int pos=0;//char pos
const int bsize=DES::BLOCKSIZE;//block size
int blockpos=0;// block pos
int charpos=0;//char pos

DESDecryption decryption_DES;
        //设置密钥
unsigned char key[ DES::KEYLENGTH ] = "xugangf";
decryption_DES.SetKey(key, DES::KEYLENGTH);


byte blockin[bsize];
byte blockout[bsize];


int nwLen=0;
 //将传进来的16进制代码转多字节,BSTR 进来自动是宽字节的
nwLen=WideCharToMultiByte(CP_ACP,NULL,com_instr.m_str,-1,NULL,0,NULL,NULL);
char* ptr_instr=new char[nwLen+bsize+1];
if(!ptr_instr)
{
delete[] ptr_instr;
return FALSE;
}
WideCharToMultiByte(CP_ACP,NULL,com_instr.m_str,-1,ptr_instr,nwLen,NULL,FALSE);

char tempbuf[4];
while(charpos<nwLen)
{


ZeroMemory(blockin,bsize);
ZeroMemory(blockout,bsize);
  //两个字符合成一个16进制的unsigned char
for( blockpos=0;blockpos<DES::BLOCKSIZE;blockpos++,charpos+=2)
{
tempbuf[0]=ptr_instr[charpos];
tempbuf[1]=ptr_instr[charpos+1];
tempbuf[2]=0;
blockin[blockpos]= (unsigned char) strtol(tempbuf, (char**)NULL, 16);
}
decryption_DES.ProcessBlock(blockin, blockout);
  //将解密出的字符串转到ptr_instr
memcpy_s(ptr_instr+pos,bsize,blockout,bsize);
pos+=bsize;
}

        //多字节转宽字节
nwLen = MultiByteToWideChar (CP_ACP, 0, ptr_instr, -1, NULL, 0);
wchar_t *ptr_winstr = new wchar_t[nwLen+bsize+1];
if(!ptr_winstr)
{
delete[] ptr_winstr;
return FALSE;
}
MultiByteToWideChar (CP_ACP, 0,ptr_instr, -1, ptr_winstr , nwLen);

com_instr.Empty();
delete[] ptr_instr;
CComBSTR result(ptr_winstr);
 //写结果
result.CopyTo(outstr);
result.Empty();
delete[] ptr_winstr;
return S_OK;
}


你可能感兴趣的:(C++,加密,解密,null,delete,encryption)