在Android上通过AES加密和解密文件的问题

时间:2019-07-02 15:31:37

标签: android encryption upload aes

我正在尝试加密在服务器上发送的语音文件。我使用以下过程:

将语音记录到文件>将文件转换为字节数组>使用生成的密钥加密字节数组>将密钥保存到字符串>上传字节数组>从服务器加载文件>将其转换为字节数组>使用生成的密钥解密<- ,我得到一个错误

javax.crypto.AEADBadTagException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
2019-07-02 17:20:51.771 25417-25448/cz.magician.justtalk W/System.err:     at java.lang.reflect.Constructor.newInstance0(Native Method)
2019-07-02 17:20:51.771 25417-25448/cz.magician.justtalk W/System.err:     at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
2019-07-02 17:20:51.771 25417-25448/cz.magician.justtalk W/System.err:     at com.android.org.conscrypt.OpenSSLCipher$EVP_AEAD.throwAEADBadTagExceptionIfAvailable(OpenSSLCipher.java:1200)
2019-07-02 17:20:51.771 25417-25448/cz.magician.justtalk W/System.err:     at com.android.org.conscrypt.OpenSSLCipher$EVP_AEAD.doFinalInternal(OpenSSLCipher.java:1229)
2019-07-02 17:20:51.771 25417-25448/cz.magician.justtalk W/System.err:     at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:363)
2019-07-02 17:20:51.771 25417-25448/cz.magician.justtalk W/System.err:     at javax.crypto.Cipher.doFinal(Cipher.java:2055)

如果我省略了发送到服务器的部分,则代码有效。如果我省略编码/解码,则代码有效。服务器没什么特别的,只是将接收到的文件保存到特定路径。

EncryptionUtility

package xxx;

import android.util.Base64;

import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.security.SecureRandom;

public class EncryptionUtility {

    //File encryption
    public static byte[] encryptData(SecretKey secretKey, byte[] data) throws Exception {
        SecureRandom secureRandom = new SecureRandom();
        byte[] iv = new byte[12];
        secureRandom.nextBytes(iv);

        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);

        cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
        byte [] encryptedData = cipher.doFinal(data);

        ByteBuffer byteBuffer = ByteBuffer.allocate(4 + iv.length + encryptedData.length);
        byteBuffer.putInt(iv.length);
        byteBuffer.put(iv);
        byteBuffer.put(encryptedData);
        return byteBuffer.array();
    }

    //File decryption
    public static byte[] decryptData(SecretKey secretKey, byte[] encryptedData) throws Exception {
        ByteBuffer byteBuffer = ByteBuffer.wrap(encryptedData);
        int noonceSize = byteBuffer.getInt();

        if(noonceSize < 12 || noonceSize >= 16)
            throw new IllegalArgumentException("Nonce size is incorrect. Make sure that the incoming data is an AES encrypted file.");

        byte[] iv = new byte[noonceSize];
        byteBuffer.get(iv);

        byte[] cipherBytes = new byte[byteBuffer.remaining()];
        byteBuffer.get(cipherBytes);

        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);

        cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
        return cipher.doFinal(cipherBytes);
    }

    //Internal - SecretKey
    public static SecretKey generateSecretKey() {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);
            return kgen.generateKey();
        }
        catch (Exception exc){
            exc.printStackTrace();
            return null;
        }
    }

    public static String parseSecretKeyToString(SecretKey secretKey){
        return Base64.encodeToString(secretKey.getEncoded(), 0);
    }
    public static SecretKey parseStringToSecretKey(String secretKeyString){
        try {
            return new SecretKeySpec(Base64.decode(secretKeyString, 0), 0, 16, "AES");
        }
        catch (Exception exc){
            return null;
        }
    }

}

加密代码

 SecretKey key = EncryptionUtility.generateSecretKey();
                String keyStr = EncryptionUtility.parseSecretKeyToString(key);
                fileBytes.put(param, EncryptionUtility.encryptData(key, readToBytes(file)));

解密代码

new AsyncTask<Void, Void, byte[]>(){
                @Override
                protected byte[] doInBackground(Void... voids) {
                    try {
                        URLConnection connection = new URL("urltovoicefile").openConnection();
                        InputStream inputStream = connection.getInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();

                        int b;
                        while ((b = inputStream.read()) != -1){
                            baos.write(b);
                        }

                        byte[] result = baos.toByteArray();
                        return EncryptionUtility.decryptData(EncryptionUtility.parseStringToSecretKey(keyStr), result);
                    }
                    catch (Exception exc){
                        exc.printStackTrace();
                    }


                    return new byte[0];
                }

                @Override
                protected void onPostExecute(byte[] result) {
                    try {
                        File file = new File(activity.getCacheDir().toString(), "read.m4a");
                        FileOutputStream out = new FileOutputStream(file);
                        out.write(result);
                        out.close();

                        MediaPlayer mediaPlayer = new  MediaPlayer();
                        mediaPlayer.setDataSource(file.getAbsolutePath());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    }
                    catch (Exception exc){
                        exc.printStackTrace();
                    }
                }
            }.execute();

编辑: 上传文件字节数组:

   /**
     * Adds a upload file section to the request
     */
    public void addFile(String fieldName, String fileName, byte[] fileBytes)
            throws IOException {
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"" + fieldName
                + "\"; filename=\"" + fileName + "\"")
                .append(LINE_FEED);
        writer.append(
                "Content-Type: " + URLConnection.guessContentTypeFromName(fileName))
                .append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        outputStream.write(fileBytes);
        outputStream.flush();

        writer.append(LINE_FEED);
        writer.flush();
    }

//编辑此部分代码中的问题

/**
     * Completes the request and receives response from the server.
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     */
    public String finish() throws IOException {
        String response;

        writer.append(LINE_FEED).flush(); //**THIS WAS ALREADY BEING ADDED BY uploadFile**
        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream());
            response = inputStreamToString(in);

            httpConn.disconnect();
        }
        else
            throw new IOException("Server returned non-OK status: " + status);

        return response;
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

最后,问题出在我上传文件的工具中。我在每个文件后添加了CR / LF,然后在请求末尾添加了一个额外的CR / LF。删除它可以解决问题。