ITPub博客

首页 > > > c++使用 cryptopp562 加密解密问题

c++使用 cryptopp562 加密解密问题

作者:fondtiger 时间:2019-09-25 13:39:45 0 删除 编辑

我使用vs2010进行c++开发  使用cryptopp类库最新版本 cryptopp562

使用AES加密 填充模式为None 加密模式为ECB

加密字符串为 "123"  密钥16进制表现形式为:AA9EEB723F484ABBDDF890C980EF1DC2

我手动填充补足16位的倍数 手动填充值为十进制 0

--------------------------------------------------

当我对字符串加密后  加密后生成的16进制字符串为:

41E9859DC28714D53552EF4C005FDDCAE8F43BE21DC104D7B78A0038BD9DC73A

当我用解密方法

 1 string AESManager::aes_decrypt(const string &instr, const byte *passPhrase)
 2 {
 3     string recovered;
 4     try
 5     {
 6         ECB_Mode< AES >::Decryption d;
 7         d.SetKey( passPhrase, AES::DEFAULT_KEYLENGTH );
 8         StringSource( instr, true, 
 9             new StreamTransformationFilter( d,
10             new StringSink( recovered )
11             ) 
12             );
13     }
14     catch( CryptoPP::Exception& e )
15     {
16         cerr << e.what() << endl;
17         exit(1);
18     }
19     return recovered;
20 }

会出现异常信息为:

StreamTransformationFilter: ciphertext length is not a multiple of block size

  经过分析 是传入后的 instr 长度不是16倍数所致 因为cryptopp判断instr长度 是用size()来判断 而

41E9859DC28714D53552EF4C005FDDCAE8F43BE21DC104D7B78A0038BD9DC73A

“00”出现在12位  因此cryptopp会认为instr长度为12  不足16   

请问  我如何用简单的方法 来避免这个问题呢?

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2508/viewspace-3147854/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-09-29

  • 博文量
    122
  • 访问量
    13449