AES 256 encryption in C++ and Qt 5

前端 未结 1 840
温柔的废话
温柔的废话 2020-12-24 04:17

I have a Java code for encryption in place as follows!

private static byte[] encrypt(byte[] raw, byte[] clear) throws 
   Exception {  
    SecretKeySpec ske         


        
相关标签:
1条回答
  • 2020-12-24 04:53

    Try this:

    #include <crypto++/aes.h>
    #include <crypto++/modes.h>
    #include <crypto++/filters.h>
    #include <crypto++/hex.h>
    #include <crypto++/sha.h>
    #include <crypto++/md5.h>
    
    QString Foo::decrypt(const QString &password)
    {
        string plain;
        string encrypted = password.toStdString();
        // Hex decode symmetric key:
        HexDecoder decoder;
        decoder.Put( (byte *)PRIVATE_KEY,32*2 );
        decoder.MessageEnd();
        word64 size = decoder.MaxRetrievable();
        char *decodedKey = new char[size];
        decoder.Get((byte *)decodedKey, size);
        // Generate Cipher, Key, and CBC
        byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
        StringSource( reinterpret_cast<const char *>(decodedKey), true,
                      new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
        memset( iv, 0x00, AES::BLOCKSIZE );
        try {
            CBC_Mode<AES>::Decryption Decryptor
            ( key, sizeof(key), iv );
            StringSource( encrypted, true,
                          new HexDecoder(new StreamTransformationFilter( Decryptor,
                                         new StringSink( plain ) ) ) );
        }
        catch (Exception &e) { // ...
        }
        catch (...) { // ...
        }
        return QString::fromStdString(plain);
    }
    
    QString Foo::encrypt(const QString &password)
    {
        string plain = password.toStdString();
        string ciphertext;
        // Hex decode symmetric key:
        HexDecoder decoder;
        decoder.Put( (byte *)PRIVATE_KEY, 32*2 );
        decoder.MessageEnd();
        word64 size = decoder.MaxRetrievable();
        char *decodedKey = new char[size];
        decoder.Get((byte *)decodedKey, size);
        // Generate Cipher, Key, and CBC
        byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
        StringSource( reinterpret_cast<const char *>(decodedKey), true,
                      new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
        memset( iv, 0x00, AES::BLOCKSIZE );
        CBC_Mode<AES>::Encryption Encryptor( key, sizeof(key), iv );
        StringSource( plain, true, new StreamTransformationFilter( Encryptor,
                      new HexEncoder(new StringSink( ciphertext ) ) ) );
        return QString::fromStdString(ciphertext);
    }
    

    Update:

    Use above code as follows:

    //...
    #define PRIVATE_KEY "729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011"
    QString encrypted = Foo::encryptPassword("test");
    // use encrypted
    

    Personally I don't like to reveal private key in source code. So I'll pass it to compiler in command line:

    g++ -DPRIVATE_KEY \"\"\"123...\"\"\" ...
    

    Where PRIVATE_KEY is your private key in plain text. If you have the key in HEX encode, just remove Hex decode symmetric key step.

    0 讨论(0)
提交回复
热议问题