当前位置:   article > 正文

QT使用OpenSSL的DES/CBC/PKCS7加密字符串_qt5 怎么使用opensll进行sm4/cbc/pkcs7padding加密解密

qt5 怎么使用opensll进行sm4/cbc/pkcs7padding加密解密

首先安装Openssl,然后QT项目的pro文件中添加引用

 

加密方法大部分来自:C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式 - osc_pyxgl9fl的个人空间 - OSCHINA - 中文开源技术交流社区

但是加密出来的内容未转码,显示的是乱码,后台需要的是HEX,增加加密后字符串转换,根据自己的需求修改部分代码。main.cpp 代码如下

  1. #include <battery.h>
  2. #include <QGuiApplication>
  3. #include <QQmlApplicationEngine>
  4. #include <QLibrary>
  5. #include <QLocale>
  6. #include <QTranslator>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <string>
  11. using namespace std;
  12. #include <openssl/des.h>
  13. //加密 cbc pkcs5padding 本身实现 //pkcs7padding 跟 pkcs5padding是同样的
  14. std::string des_cbc_pkcs5_encrypt(const std::string &clearText)
  15. {
  16. static unsigned char cbc_iv[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
  17. //初始化IV向量【我这边密码与IV相同】
  18. std::string strCipherText;
  19. DES_cblock keyEncrypt, ivec;
  20. memcpy(keyEncrypt, cbc_iv, sizeof(cbc_iv));
  21. DES_key_schedule keySchedule; //密钥表
  22. DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性
  23. memcpy(ivec, cbc_iv, sizeof(cbc_iv));
  24. // 循环加密,每8字节一次
  25. const_DES_cblock inputText;
  26. DES_cblock outputText;
  27. std::vector<unsigned char> vecCiphertext;
  28. unsigned char tmp[8];
  29. for (int i = 0; i < clearText.length() / 8; i++)
  30. {
  31. memcpy(inputText, clearText.c_str() + i * 8, 8);
  32. DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT); //加密
  33. memcpy(tmp, outputText, 8);
  34. for (int j = 0; j < 8; j++)
  35. vecCiphertext.push_back(tmp[j]);
  36. //重置ivec
  37. memcpy(ivec, outputText, 8);
  38. }
  39. if (clearText.length() % 8 != 0)
  40. {
  41. int tmp1 = clearText.length() / 8 * 8;
  42. int tmp2 = clearText.length() - tmp1;
  43. memset(inputText,(8-tmp2), 8);
  44. memcpy(inputText, clearText.c_str() + tmp1, tmp2);
  45. }
  46. else
  47. {
  48. memset(inputText,8, 8);
  49. }
  50. // 加密函数
  51. DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_ENCRYPT); //加密
  52. memcpy(tmp, outputText, 8);
  53. for (int j = 0; j < 8; j++)
  54. vecCiphertext.push_back(tmp[j]);
  55. strCipherText.clear();
  56. strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
  57. return strCipherText;
  58. }
  59. //解密 cbc pkcs5padding 本身实现 //zeropadding / pkcs7padding 跟 pkcs5padding是同样的
  60. std::string des_cbc_pkcs5_decrypt(const std::string &cipherText)
  61. {
  62. static unsigned char cbc_iv[8] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
  63. //初始化IV向量
  64. std::string clearText;
  65. DES_cblock keyEncrypt, ivec;
  66. memcpy(keyEncrypt, cbc_iv, sizeof(cbc_iv));
  67. DES_key_schedule keySchedule; //密钥表
  68. DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性
  69. memcpy(ivec, cbc_iv, sizeof(cbc_iv));
  70. // 循环解密,每8字节一次
  71. const_DES_cblock inputText;
  72. DES_cblock outputText;
  73. std::vector<unsigned char> vecCleartext;
  74. unsigned char tmp[8];
  75. for (int i = 0; i < cipherText.length() / 8; i++)
  76. {
  77. memcpy(inputText, cipherText.c_str() + i * 8, 8);
  78. DES_ncbc_encrypt(inputText, outputText, 8, &keySchedule, &ivec, DES_DECRYPT); //解密
  79. memcpy(tmp, outputText, 8);
  80. for (int j = 0; j < 8; j++)
  81. vecCleartext.push_back(tmp[j]);
  82. //重置ivec
  83. //memcpy(ivec, outputText, 8); //解密过程不须要用前一块的结果做为下一块的IV
  84. }
  85. if (clearText.length() % 8 != 0)
  86. {
  87. int tmp1 = clearText.length() / 8 * 8;
  88. int tmp2 = clearText.length() - tmp1;
  89. memset(inputText,0, tmp2);
  90. memcpy(inputText, cipherText.c_str() + tmp1, tmp2);
  91. DES_ncbc_encrypt(inputText, outputText, tmp2, &keySchedule, &ivec, DES_DECRYPT); //解密
  92. memcpy(tmp, outputText, tmp2);
  93. for (int j = 0; j < 8; j++)
  94. vecCleartext.push_back(tmp[j]);
  95. }
  96. clearText.clear();
  97. clearText.assign(vecCleartext.begin(), vecCleartext.end());
  98. return clearText;
  99. }
  100. // 加密后密文转hex
  101. string StringToHex(const std::string& data)
  102. {
  103. const std::string hex = "0123456789ABCDEF";
  104. std::stringstream ss;
  105. for (std::string::size_type i = 0; i < data.size(); ++i)
  106. ss << hex[(unsigned char)data[i] >> 4] << hex[(unsigned char)data[i] & 0xf];
  107. std::cout << ss.str() << std::endl;
  108. return ss.str();
  109. }
  110. //---------------------以上代码可以写在一个单独的头文件中---------------------------------
  111. int main(int argc, char *argv[])
  112. {
  113. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  114. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  115. #endif
  116. QGuiApplication app(argc, argv);
  117. {
  118. string result = des_cbc_pkcs5_encrypt("123456");
  119. string hexResult = StringToHex(result);
  120. qDebug() << "---------hex---------- " << QString::fromStdString(hexResult);
  121. }
  122. QTranslator translator;
  123. const QStringList uiLanguages = QLocale::system().uiLanguages();
  124. for (const QString &locale : uiLanguages) {
  125. const QString baseName = "DemoApp_" + QLocale(locale).name();
  126. if (translator.load(":/i18n/" + baseName)) {
  127. app.installTranslator(&translator);
  128. break;
  129. }
  130. }
  131. QQmlApplicationEngine engine;
  132. const QUrl url(QStringLiteral("qrc:/main.qml"));
  133. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  134. &app, [url](QObject *obj, const QUrl &objUrl) {
  135. if (!obj && url == objUrl)
  136. QCoreApplication::exit(-1);
  137. }, Qt::QueuedConnection);
  138. engine.load(url);
  139. return app.exec();
  140. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/308423
推荐阅读
相关标签
  

闽ICP备14008679号