viyana
viyana
采纳率50%
2018-09-27 09:10 阅读 5.8k

aes解密后明文为乱码的问题

自己尝试使用aes的cbc模式加密字符串,并解密字符串,并在最后使用OpenSSl自带的获取错误消息函数,获取错误信息,显示在加密和解密的过程中没有报错,但是将最后的明文结果输出,得到的始终是乱码啊。下面为自己写的代码,请各位大神看看啊
1.
1. #define SIZE 1000
1. #define BITS 128
1. using namespace std;
1. int main()
1. {
1. char in[50];
1. cin.get(in, 50);
1. AES_KEY* Enkey = new(std::nothrow) AES_KEY;
1. AES_KEY* Dekey = new(std::nothrow) AES_KEY;
1. if (Dekey == nullptr || Enkey == nullptr)
1. {
1. return 0;
1. }
1. char userkey[16]="123456789012345";
1. for (int i=0;i<16;++i)
1. {
1. userkey[i] = 32 + i;
1. }
1. AES_set_encrypt_key((const unsigned char*)userkey, BITS, Enkey);
1. AES_set_decrypt_key((const unsigned char*)userkey, BITS, Dekey);
1.
1. unsigned char* szOutEn;
1. unsigned char* szOutDe;
1. unsigned char szIvec[16];
1. int len;
1. len = 0;
1. if ((strlen(in)+1)%16 == 0)
1. {
1. len = strlen(in) + 1;
1. }
1. else
1. {
1. len = (strlen(in) / 16 + 1) * 16;
1. }
1. szOutEn = new(std::nothrow) unsigned char[len];
1. szOutDe = new(std::nothrow) unsigned char[len];
1. if (nullptr == szOutEn || nullptr == szOutDe)
1. {
1. return 0;
1. }
1.
1. AES_cbc_encrypt((const unsigned char*)in, szOutEn, len, Enkey, szIvec, AES_ENCRYPT);
1. cout << "加密后的密文为:" << szOutEn << endl;
1. AES_cbc_encrypt((const unsigned char*)szOutEn, szOutDe, len, Dekey, szIvec, AES_DECRYPT);
1.

1. unsigned long ulErr = ERR_get_error();
1. char szErrMsg[1024] = { 0 };
1. char* pszTmp = nullptr;
1. pszTmp = ERR_error_string(ulErr, szErrMsg);
1. cout << "解密后的密文为:" << szOutDe << endl;
1. cout << szOutEn << endl;
1. cout << "错误码:" << ulErr<<endl;
1. cout << szErrMsg << endl;
1. /*AES_encrypt_BaseEncond((const unsigned char*)in, Enkey);
1. AES_decrypt_BaseDecode("Encode.txt", Dekey);*/
1. delete Enkey;
1. delete Dekey;
1. cin.get();
1. return 0;
1. }

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • wjintao wjintao 2018-09-27 09:50

    你可以直接赋值in变量试试,如:char *in="asdfghjkl";

    点赞 评论 复制链接分享
  • viyana viyana 2018-09-27 11:52

    楼上的方法试了,不对啊

    点赞 评论 复制链接分享

相关推荐