Android中音频文件的AES算法

时间:2014-12-17 16:00:40

标签: android cryptography aes

您好我正在我的项目中进行AES加密和解密。

我有一个.mp4文件,使用“AES / CBC / pkcs5padding”加密。

我有key和iv值,用于加密音频文件的前256个字节。

我需要使用相同的算法,密钥和iv值解密文件的前256个字节。

我已经关注了一些链接(link1link2)。

我有一个音频播放器演示,并尝试实现我的部分(AES加密和解密)作为演示。

下面我解释了我所做的代码。

此方法从res / raw文件夹下的加密文件中读取数据。

private void readFile() throws IOException, InvalidKeyException,
        NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException {

    Context context = getApplicationContext();
    /*
     * InputStream is = getResources().openRawResource(
     * getResources().getIdentifier("raw/encrypted", "raw",
     * getPackageName())); String text = "";
     * 
     * int size = is.available(); byte[] buffer = new byte[size];
     * is.read(buffer);
     */

    InputStream inStream = context.getResources().openRawResource(
            R.raw.encrypted);

    // get string from file

    byte[] music = new byte[256];
    for (int i = 0; i <= inStream.available(); i = i + 255) {
        music = convertStreamToByteArray(inStream, i);
        byte[] bytesToWrite = new byte[256];

        bytesToWrite = music;
        if (i == 0) {
            bytesToWrite = AES256Cipher.decrypt(iv.getBytes("UTF-8"),
                    key.getBytes("UTF-8"), music);
            // writeFirstSetOfBytes("decrypted.mp4");
        }

        writeFirstSetOfBytes(bytesToWrite);

    }
}

解密方法来自link1。在这里,我通过了上面提到的关键和iv值。

public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes,
            byte[] textBytes) throws java.io.UnsupportedEncodingException,
            NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException {

        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        // SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE,
                new SecretKeySpec(keyBytes, "AES"), ivSpec);
        return cipher.doFinal(textBytes);
    }

此方法用于从输入流中获取字节数组。

public static byte[] convertStreamToByteArray(InputStream is, int size)
        throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[256];
    int i = Integer.MAX_VALUE;
    while ((i = is.read(buff, size, buff.length)) > 0) {
        baos.write(buff, 0, i);
    }

    return baos.toByteArray(); // be sure to close InputStream in calling
                                // function
}

此方法将接收到的字节数组写入目标文件(decrypted.mp4)

private void writeFirstSetOfBytes(byte[] byteToWrite) {

    File file = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
            "/decrypted.mp4");
    FileOutputStream stream = null;
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // writing data into file
    try {
        stream = new FileOutputStream(file);

        stream.write(byteToWrite);
        stream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

我收到以下错误。

javax.crypto.IllegalBlockSizeException: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

我如何达到我的要求?

  1. 如何仅解密前256个字节的数据?
  2. 是否有任何代码行为错误?
  3. 是否有任何第三方图书馆可用?
  4. 如果需要更多说明,请告诉我。

1 个答案:

答案 0 :(得分:-1)

根据维基百科,PKCS#5是PKCS#7的特殊变体,它是为精确的64位块定义的(而PKCS#7适用于任何块大小),但AES仅使用128位块(或更大) ,但这不包括在标准中),因此不匹配。尝试另一种填充方案。