Qt uses AES encrypted data to send to springboot (java) server for decryption

Leek Clock 2022-09-23 07:48:08 阅读数:580

qtusesaesencrypteddata

背景

目前用Qt和同事(He does the server side)Make a third-party account login thing:
After the user enters the account and password on the local client,The client encrypts the password symmetrically,将数据post给服务器.
服务器接收到数据之后,对密码进行解密,得到明文,Then use that account+Password for third-party platform login.

问题(重点、难点)

无论是DES还是AES,不同的平台下,Or the libraries implemented by different people are generally difficult to connect.尤其是那个DES,There is a bunch of customizable initialization data,Once there is a parameter that does not match,That's a thousand miles away.

解决方案

java使用Hutool
Qt使用QtAes

javaThere seems to be an official encryption and decryption library that comes with it,There are many third parties(It seems to be an official package).Currently used by colleaguesHutool,挺好用的.

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.Mode;
...
{

// Test key,It seems to have a fixed length24,不然出问题
String key = "abcdefghijklmnop78945612";
// DES myCrypto = new DES(key.getBytes());
// DES myDes = new DES(
// Mode.ECB,
// Padding.PKCS5Padding,
// key.getBytes()
// );
AES myCrypto = new AES(key.getBytes());
// AES myCrypto = new AES(
// Mode.ECB,
// Padding.PKCS5Padding,
// key.getBytes()
// );
byte[] enData = myCrypto.encrypt("12345678abcdef");
String hexStr = HexUtil.encodeHexStr(enData);
System.out.println(hexStr);
hexStr = "0f5cba12ce291aeb42e12ffda5e08f441c0f896ba2de8791074fd9a50e1746a0";
byte[] hexData = HexUtil.decodeHex(hexStr);
byte[] deData = myCrypto.decrypt(hexData);
String deStr = new String(deData, "utf-8");
System.out.println(deStr);
}

QtUse the library you found earlier hereQtAes就行,But pay attention to the parameters.The following code shows the specific parameter settings:

#include "QtAes/src/qaesencryption.h" //It depends on which path you put it in
...
// The parameters here are tested by me,The rest of the parameters can be tried one by one with the backend if you are free.
QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB, QAESEncryption::PKCS7);
QByteArray enData = encryption.encode("12345678abcdef中文文本", "abcdefghijklmnop78945612"); //Use the same key as the server
QString passwordData = enData.toHex();
copyright:author[Leek Clock],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230625501820.html