What are best practices for using AES encryption in Android?

后端 未结 5 842
醉话见心
醉话见心 2020-11-30 16:20

Why I ask this question:

I know there have been a lot of questions about AES encryption, even for Android. And there are lots of code snippets if yo

5条回答
  •  执念已碎
    2020-11-30 16:51

    Use BouncyCastle Lightweight API. It provides 256 AES With PBE and Salt.
    Here sample code, which can encrypt/decrypt files.

    public void encrypt(InputStream fin, OutputStream fout, String password) {
        try {
            PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest());
            char[] passwordChars = password.toCharArray();
            final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
            pGen.init(pkcs12PasswordBytes, salt.getBytes(), iterationCount);
            CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
            ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
            aesCBC.init(true, aesCBCParams);
            PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());
            aesCipher.init(true, aesCBCParams);
    
            // Read in the decrypted bytes and write the cleartext to out
            int numRead = 0;
            while ((numRead = fin.read(buf)) >= 0) {
                if (numRead == 1024) {
                    byte[] plainTemp = new byte[aesCipher.getUpdateOutputSize(numRead)];
                    int offset = aesCipher.processBytes(buf, 0, numRead, plainTemp, 0);
                    final byte[] plain = new byte[offset];
                    System.arraycopy(plainTemp, 0, plain, 0, plain.length);
                    fout.write(plain, 0, plain.length);
                } else {
                    byte[] plainTemp = new byte[aesCipher.getOutputSize(numRead)];
                    int offset = aesCipher.processBytes(buf, 0, numRead, plainTemp, 0);
                    int last = aesCipher.doFinal(plainTemp, offset);
                    final byte[] plain = new byte[offset + last];
                    System.arraycopy(plainTemp, 0, plain, 0, plain.length);
                    fout.write(plain, 0, plain.length);
                }
            }
            fout.close();
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    public void decrypt(InputStream fin, OutputStream fout, String password) {
        try {
            PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest());
            char[] passwordChars = password.toCharArray();
            final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
            pGen.init(pkcs12PasswordBytes, salt.getBytes(), iterationCount);
            CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
            ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
            aesCBC.init(false, aesCBCParams);
            PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());
            aesCipher.init(false, aesCBCParams);
    
            // Read in the decrypted bytes and write the cleartext to out
            int numRead = 0;
            while ((numRead = fin.read(buf)) >= 0) {
                if (numRead == 1024) {
                    byte[] plainTemp = new byte[aesCipher.getUpdateOutputSize(numRead)];
                    int offset = aesCipher.processBytes(buf, 0, numRead, plainTemp, 0);
                    // int last = aesCipher.doFinal(plainTemp, offset);
                    final byte[] plain = new byte[offset];
                    System.arraycopy(plainTemp, 0, plain, 0, plain.length);
                    fout.write(plain, 0, plain.length);
                } else {
                    byte[] plainTemp = new byte[aesCipher.getOutputSize(numRead)];
                    int offset = aesCipher.processBytes(buf, 0, numRead, plainTemp, 0);
                    int last = aesCipher.doFinal(plainTemp, offset);
                    final byte[] plain = new byte[offset + last];
                    System.arraycopy(plainTemp, 0, plain, 0, plain.length);
                    fout.write(plain, 0, plain.length);
                }
            }
            fout.close();
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题