Salut à tous,
Après de longues heures de travail, j'ai pas mal avancé sur mon projet de bot en c++. Seulement voilà, comme beaucoup, je me heurte à une difficulté, l'encryption des credentials. Tout se passe très bien pour ce qui est de la génération de ma chaine à encrypter ainsi que pour la récupération et la transformation de la clé envoyée par le serveur. Mon problème se pose au moment de générer les credentials étant donné que si je traite le même paquet plusieurs fois, j'obtiens des credentials différents (peut-être un problème de stupide mais je bloque vraiment).
Voici ma classe, elle est très inspirée de celle de Munrek trouvée ici https://cadernis.fr/index.php?threads/résolu-serialisation-identificationmessage.1404/#post-16492 avec laquelle je rencontre le même problème
rsa.cpp
Un très grand merci à ceux qui prendront le temps de m'aider
Après de longues heures de travail, j'ai pas mal avancé sur mon projet de bot en c++. Seulement voilà, comme beaucoup, je me heurte à une difficulté, l'encryption des credentials. Tout se passe très bien pour ce qui est de la génération de ma chaine à encrypter ainsi que pour la récupération et la transformation de la clé envoyée par le serveur. Mon problème se pose au moment de générer les credentials étant donné que si je traite le même paquet plusieurs fois, j'obtiens des credentials différents (peut-être un problème de stupide mais je bloque vraiment).
Voici ma classe, elle est très inspirée de celle de Munrek trouvée ici https://cadernis.fr/index.php?threads/résolu-serialisation-identificationmessage.1404/#post-16492 avec laquelle je rencontre le même problème
rsa.cpp
C:
#include "rsa.h"
const std::string RSAD2::MPublicKey =
"-----BEGIN PUBLIC KEY-----\nMIIBUzANBgkqhkiG9w0BAQEFAAOCAUAAMIIBOwKCATIAgucoka9J2PXcNdjcu6CuDmgteIMB+rih\n2UZJIuSoNT/0J/lEKL/W4UYbDA4U/6TDS0dkMhOpDsSCIDpO1gPG6+6JfhADRfIJItyHZflyXNUj\nWOBG4zuxc/L6wldgX24jKo+iCvlDTNUedE553lrfSU23Hwwzt3+doEfgkgAf0l4ZBez5Z/ldp9it\n2NH6/2/7spHm0Hsvt/YPrJ+EK8ly5fdLk9cvB4QIQel9SQ3JE8UQrxOAx2wrivc6P0gXp5Q6bHQo\nad1aUp81Ox77l5e8KBJXHzYhdeXaM91wnHTZNhuWmFS3snUHRCBpjDBCkZZ+CxPnKMtm2qJIi57R\nslALQVTykEZoAETKWpLBlSm92X/eXY2DdGf+a7vju9EigYbX0aXxQy2Ln2ZBWmUJyZE8B58CAwEA\nAQ==\n-----END PUBLIC KEY-----"
;
RSAD2::RSAD2()
{
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
}
QByteArray RSAD2::Encrypt(QByteArray helloConnectMessageKey, std::string accountName, std::string accountPassword, std::string salt) {
QByteArray* byteList = new QByteArray();
std::string s = AdaptSalt(salt);
byteList->append(QString::fromStdString(s));
char* empty = new char[32];
for (int i =0; i<32; ++i){
empty[i]='\0';
}
byteList->append(empty, 32);
unsigned char nameLenght = accountName.length();
byteList->append(QString(nameLenght));
byteList->append(QString::fromStdString(accountName));
byteList->append(QString::fromStdString(accountPassword));
return LoginPKeyEncrypt(*byteList, transformLoginPublicKey(DecryptHelloConnectMessageKey(helloConnectMessageKey)));
}
bool RSAD2::CompareByteArrays(QByteArray firstArray, QByteArray secondArray) {
if (firstArray.length() != secondArray.length()){
return false;
}
for (int index = 0; index < firstArray.length(); ++index) {
if (firstArray[index] != secondArray[index]) {
return false;
}
++index;
}
return true;
}
QByteArray RSAD2::DecryptHelloConnectMessageKey(QByteArray helloConnectMessageKey){
std::vector<unsigned char> signature = std::vector<unsigned char>(
helloConnectMessageKey.begin(), helloConnectMessageKey.end());
char * DPKey =(char *) malloc(MPublicKey.size());
strcpy(DPKey, MPublicKey.c_str());
BIO *bp_dofus = BIO_new_mem_buf(DPKey, MPublicKey.size());
RSA *my_rsa = PEM_read_bio_RSA_PUBKEY(bp_dofus, NULL, NULL, NULL);
unsigned char *inputSignature, *outputSignature;
inputSignature = (unsigned char*) malloc(5000);
outputSignature = (unsigned char*) malloc(5000);
inputSignature = reinterpret_cast<unsigned char*> (&signature[0]);
int buflen = RSA_public_decrypt(signature.size(), inputSignature, outputSignature, my_rsa, RSA_PKCS1_PADDING);
QByteArray outputSignatureArray((const char*)outputSignature, buflen);
return outputSignatureArray;
}
std::string RSAD2::AdaptSalt(std::string salt) {
if (salt.length() < 32) {
while (salt.length() < 32) {
salt += " ";
}
}
return salt;
}
std::string RSAD2::transformLoginPublicKey(QByteArray key) {
std::vector<unsigned char> lPKey = std::vector<unsigned char>(
key.begin(), key.end());
const std::string lPKeyStr(lPKey.begin(), lPKey.end());
std::string lPKeyStrEnc = base64_encode(reinterpret_cast<const unsigned char*>(lPKeyStr.c_str()), lPKeyStr.length());
std::stringstream lPKeyStrEncFor;
for(int i=0; i!= lPKeyStrEnc.length(); i++){
lPKeyStrEncFor << lPKeyStrEnc[i];
if((i+1)%76==0){
if(i!=0) lPKeyStrEncFor << "\n";
}
}
std::stringstream SlPKeyStrEnc;
SlPKeyStrEnc << "-----BEGIN PUBLIC KEY-----" << "\n" << lPKeyStrEncFor.str() << "\n" << "-----END PUBLIC KEY-----";
return SlPKeyStrEnc.str();
}
QByteArray RSAD2::LoginPKeyEncrypt(QByteArray credentialsArray, std::string LoginPublicKey) {
std::vector<unsigned char> credentials = std::vector<unsigned char>(
credentialsArray.begin(), credentialsArray.end());
char * LoginPublicKeyByte = (char *) malloc(LoginPublicKey.size());
strcpy(LoginPublicKeyByte, LoginPublicKey.c_str());
BIO *bp_login = BIO_new_mem_buf(LoginPublicKeyByte, -1);
RSA *my_second_rsa = PEM_read_bio_RSA_PUBKEY(bp_login, NULL, NULL, NULL);
unsigned char *pinputCredentials, *poutputCredentials;
pinputCredentials = (unsigned char*) malloc(5000);
poutputCredentials = (unsigned char*) malloc(5000);
pinputCredentials = reinterpret_cast<unsigned char*> (&credentials[0]);
int buflen = RSA_public_encrypt(credentials.size(), pinputCredentials, poutputCredentials, my_second_rsa, RSA_PKCS1_PADDING);
QByteArray outputCredentialsArray((const char*)poutputCredentials, buflen);
return outputCredentialsArray;
}