Porting Android Java-base AES encription to Qt Android

Viewed 88

I've made an app for Android, using Java that uses the java classes for encryption. Here is the basic gist of the java code:

This is how I initialize everything:

// Constants
private final static String encoding = "UTF-8";
private final static String algorithm = "AES/CBC/PKCS5Padding";
private final static int NCHARS = 16;

// Private variables
private byte[] rawdata;
private byte[] keyBytes;
private String status;
private String fileName;

// AES Variables
private static Cipher cipher;
private static SecretKeySpec secretKeySpec;
private static IvParameterSpec ivParameterSpec;

//====================== AES Engine intialization ======================
public AESCrypt (String passwd,String datafile){

    // Initializing variables.
    rawdata = null;
    status = "";
    fileName = datafile;

    // Transforming the passwd to 16 bytes.
    try {
        MessageDigest digester = MessageDigest.getInstance("MD5");
        InputStream in = new ByteArrayInputStream(Charset.forName(encoding).encode(passwd).array());
        byte[] buffer = new byte[NCHARS];
        int byteCount;
        while ((byteCount = in.read(buffer)) > 0) {
            digester.update(buffer, 0, byteCount);
        }
        keyBytes = digester.digest();
    }
    catch(Exception e){
        status = "Error in key generation: " + e.toString();
    }

    // Initilizing the crypto engine
    try {
        cipher = Cipher.getInstance(algorithm);
    }
    catch(Exception e){
        status = "Error in intialization: " + e.toString();
    }
    secretKeySpec = new SecretKeySpec(keyBytes, "AES");
    ivParameterSpec = new IvParameterSpec(keyBytes);

}

This is how I decrypt:

//====================== AES Decryption ======================
public String decrypt(){
    status = "";
    String data = "";

    // Opening the file for reading
    byte[] eBytes = null;
    try{
        DataInputStream fis = new DataInputStream(new FileInputStream(fileName));

        // Reading the size of the written files
        int size = fis.readInt();

        // Reading the rest of the file
        eBytes = new byte[size];
        fis.readFully(eBytes);
        fis.close();

    }
    catch(Exception e){
        status = "Error reading the file: " + e.toString();
    }

    if (!status.isEmpty()) return data;

    // The data is read and it is now decrypted.
    try{
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        rawdata = cipher.doFinal(eBytes);
        data = new String(rawdata,encoding);
    }
    catch(Exception e){
        status = "Error decrypting the file: " + e.toString();
    }

    return data;
}

This is able to decrypt files that were encrypted by the (not shown here) encrypt function just fine. Problem is that I now want to do the exact same thing using Qt for Android. Since this will be a new version of the same App It needs to work on existing encrypted files. So... I need to reproduce this in C++ using Qt for Android. Now AES itself is the easy part. Its an standard and there are plenty of implementations out there. The problem are all the prep functions/ algorithms used on top of it which I have no idea what the do (such as IvParameterSpec, SecretKeySpec, or what is PKCS5Padding).

However I've been reading about QAndroidJniObject. Would it be possible to use this and invoke this code as is from C++/Qt? If so, can anyone give an example of how? Or maybe a recomendation on how to approach this problem?

0 Answers
Related