QString UBCryptoUtils::symetricEncrypt(const QString& clear) { QByteArray clearData = clear.toUtf8(); int cipheredLength = clearData.length() + AES_BLOCK_SIZE; int paddingLength = 0; unsigned char *ciphertext = (unsigned char *)malloc(cipheredLength); if(!EVP_EncryptInit_ex(&mAesEncryptContext, NULL, NULL, NULL, NULL)){ free(ciphertext); return QString(); } if(!EVP_EncryptUpdate(&mAesEncryptContext, ciphertext, &cipheredLength, (unsigned char *)clearData.data(), clearData.length())){ free(ciphertext); return QString(); } /* update ciphertext with the final remaining bytes */ if(!EVP_EncryptFinal_ex(&mAesEncryptContext, ciphertext + cipheredLength, &paddingLength)){ free(ciphertext); return QString(); } QByteArray cipheredData((const char *)ciphertext, cipheredLength + paddingLength); free(ciphertext); return QString::fromLatin1(cipheredData.toBase64()); }
QByteArray UBAbstractPublisher::encrypt(const QString& clear) { static const char *key = "9ecHaspud9uD9ste5erAchehefrup3ec"; EVP_CIPHER_CTX aesEncryptContext; EVP_CIPHER_CTX_init(&aesEncryptContext); EVP_EncryptInit_ex(&aesEncryptContext, EVP_aes_256_ecb(), NULL, (const unsigned char*)key, NULL); QByteArray clearData = clear.toUtf8(); int cipheredLength = clearData.length() + AES_BLOCK_SIZE; int paddingLength = 0; unsigned char *cipherText = (unsigned char *)malloc(cipheredLength); if(!EVP_EncryptInit_ex(&aesEncryptContext, NULL, NULL, NULL, NULL)) return QByteArray(); if(!EVP_EncryptUpdate(&aesEncryptContext, cipherText, &cipheredLength, (unsigned char *)clearData.data(), clearData.length())) return QByteArray(); /* update ciphertext with the final remaining bytes */ if(!EVP_EncryptFinal_ex(&aesEncryptContext, cipherText + cipheredLength, &paddingLength)) return QByteArray(); QByteArray cipheredData((const char *)cipherText, cipheredLength + paddingLength); free(cipherText); EVP_CIPHER_CTX_cleanup(&aesEncryptContext); return cipheredData; }