viyana 2018-09-27 09:10 采纳率: 0%
浏览 6848

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 2018-09-27 09:50
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献