Created
November 11, 2016 16:53
-
-
Save hoetz/e6c271abffc5372acf95332c68d8d8a1 to your computer and use it in GitHub Desktop.
Encrypt in c++ Qt / decrypt in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QString> | |
#include <Poco/Foundation.h> | |
#include <Poco/Crypto/Cipher.h> | |
#include <Poco/Crypto/CipherKey.h> | |
#include <Poco/Crypto/CipherFactory.h> | |
#include <openssl/crypto.h> | |
#include <QDebug> | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
std::string password = "01234567891234560123456789123456"; | |
std::string ivString("0123456789123456"); | |
Poco::Crypto::Cipher::ByteVec iv { ivString.begin(), ivString.end()}; | |
Poco::Crypto::Cipher::ByteVec key2 {password.begin(), password.end()}; | |
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory(); | |
Poco::Crypto::CipherKey key("aes-256-cbc", key2, iv); // iterationCount = 1 | |
Poco::Crypto::Cipher* pCipher = factory.createCipher(key); | |
std::string plainText = "123456789012345"; | |
std::string encrypted = pCipher->encryptString(plainText, Poco::Crypto::Cipher::ENC_BASE64); //Base64-encoded output | |
qDebug() << "plainText=" << QString().fromStdString(plainText); | |
qDebug() << "encrypted=" << QString().fromStdString(encrypted); | |
std::string decrypted = pCipher->decryptString(encrypted, Poco::Crypto::Cipher::ENC_BASE64); | |
qDebug() << "decrypted=" << QString().fromStdString(decrypted); | |
return a.exec(); | |
} | |
##### c# #### | |
public static string Decrypt(string encryptedInputBase64) | |
{ | |
using (AesCryptoServiceProvider aesEncryptor = new AesCryptoServiceProvider()) | |
{ | |
var encryptedData = Convert.FromBase64String(encryptedInputBase64); | |
var btKey = System.Text.Encoding.ASCII.GetBytes("01234567891234560123456789123456"); | |
byte[] iv = Encoding.ASCII.GetBytes("0123456789123456"); | |
var keyString = System.Text.Encoding.Unicode.GetString(aesEncryptor.Key); | |
aesEncryptor.Mode = CipherMode.CBC; | |
aesEncryptor.Padding = PaddingMode.PKCS7; | |
aesEncryptor.KeySize = 256; | |
aesEncryptor.BlockSize = 128; | |
aesEncryptor.Key = btKey; | |
aesEncryptor.IV = iv; | |
return InternalDecrypt(aesEncryptor, encryptedData); | |
} | |
} | |
private static string InternalDecrypt(AesCryptoServiceProvider aesEncryptor, byte[] encryptedData) | |
{ | |
using (ICryptoTransform decryptor = aesEncryptor.CreateDecryptor(aesEncryptor.Key, | |
aesEncryptor.IV)) | |
{ | |
byte[] decrypted; | |
// Create the streams used for decryption. | |
using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) | |
{ | |
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, | |
decryptor, | |
CryptoStreamMode.Read)) | |
{ | |
decrypted = new byte[encryptedData.Length]; | |
var byteCount = csDecrypt.Read(decrypted, 0, encryptedData.Length); | |
string strResult = Encoding.ASCII.GetString(decrypted); | |
int pos = strResult.IndexOf('\0'); | |
if (pos >= 0) | |
strResult = strResult.Substring(0, pos); | |
return strResult; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you I was looking for an example of Poco::Crypto , i found your code .