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;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值