AES 256 encryption - Qt equivalent for Java

前端 未结 2 1146
情话喂你
情话喂你 2021-01-03 03:21

I have my AES 256 encryption method implemented and working fine in Java as follows!

  private static final byte[] IV = {
    0, 2, 4, 8, 16, 32, 64, 127, 
          


        
2条回答
  •  鱼传尺愫
    2021-01-03 03:49

    I guess your Java implementation misses a hash step on the key. I'm using a SHA256 hash of the key. To test c++ implementation, change code to this:

    QString encrypt(QByteArray r, const QString &password)
     {
    const char *sample = r.data();
    string plain = password.toStdString();
    string ciphertext;
    // Generate Cipher, Key, and CBC
    byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
    //StringSource( reinterpret_cast(sample), true,
    //              new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
    for(int i=0; i< AES::MAX_KEYLENGTH; ++i){
        key[i] = reinterpret_cast(decodedKey)[i];
    }
    memset( iv, 0x00, AES::BLOCKSIZE );
    CBC_Mode::Encryption Encryptor( key, sizeof(key), iv );
    StringSource( plain, true, new StreamTransformationFilter( Encryptor,
                  new HexEncoder(new StringSink( ciphertext ) ) ) );
    return QString::fromStdString(ciphertext);
    } 
    

提交回复
热议问题