What are best practices for using AES encryption in Android?

后端 未结 5 824
醉话见心
醉话见心 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();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 16:59

    Neither implementation you give in your question is entirely correct, and neither implementation you give should be used as is. In what follows, I will discuss aspects of password-based encryption in Android.

    Keys and Hashes

    I will start discussing the password-based system with salts. The salt is a randomly generated number. It is not "deduced". Implementation 1 includes a generateSalt() method that generates a cryptographically strong random number. Because the salt is important to security, it should be kept secret once it is generated, though it only needs to be generated once. If this is a Web site, it's relatively easy to keep the salt secret, but for installed applications (for desktop and mobile devices), this will be much more difficult.

    The method getHash() returns a hash of the given password and salt, concatenated into a single string. The algorithm used is SHA-512, which returns a 512-bit hash. This method returns a hash that's useful for checking a string's integrity, so it might as well be used by calling getHash() with just a password or just a salt, since it simply concatenates both parameters. Since this method won't be used in the password-based encryption system, I won't be discussing it further.

    The method getSecretKey(), derives a key from a char array of the password and a hex-encoded salt, as returned from generateSalt(). The algorithm used is PBKDF1 (I think) from PKCS5 with SHA-256 as the hash function, and returns a 256-bit key. getSecretKey() generates a key by repeatedly generating hashes of the password, salt, and a counter (up to the iteration count given in PBE_ITERATION_COUNT, here 100) in order to increase the time needed to mount a brute-force attack. The salt's length should be at least as long as the key being generated, in this case, at least 256 bits. The iteration count should be set as long as possible without causing unreasonable delay. For more information on salts and iteration counts in key derivation, see section 4 in RFC2898.

    The implementation in Java's PBE, however, is flawed if the password contains Unicode characters, that is, those that require more than 8 bits to be represented. As stated in PBEKeySpec, "the PBE mechanism defined in PKCS #5 looks at only the low order 8 bits of each character". To work around this problem, you can try generating a hex string (which will contain only 8-bit characters) of all 16-bit characters in the password before passing it to PBEKeySpec. For example, "ABC" becomes "004100420043". Note also that PBEKeySpec "requests the password as a char array, so it can be overwritten [with clearPassword()] when done". (With respect to "protecting strings in memory", see this question.) I don't see any problems, though, with representing a salt as a hex-encoded string.

    Encryption

    Once a key is generated, we can use it to encrypt and decrypt text.

    In implementation 1, the cipher algorithm used is AES/CBC/PKCS5Padding, that is, AES in the Cipher Block Chaining (CBC) cipher mode, with padding defined in PKCS#5. (Other AES cipher modes include counter mode (CTR), electronic codebook mode (ECB), and Galois counter mode (GCM). Another question on Stack Overflow contains answers that discuss in detail the various AES cipher modes and the recommended ones to use. Be aware, too, that there are several attacks on CBC mode encryption, some of which are mentioned in RFC 7457.)

    Note that you should use an encryption mode that also checks the encrypted data for integrity (e.g., authenticated encryption with associated data, AEAD, described in RFC 5116). However, AES/CBC/PKCS5Padding doesn't provide integrity checking, so it alone is not recommended. For AEAD purposes, using a secret that's at least twice as long as a normal encryption key is recommended, to avoid related key attacks: the first half serves as the encryption key, and the second half serves as the key for the integrity check. (That is, in this case, generate a single secret from a password and salt, and split that secret in two.)

    Java Implementation

    The various functions in implementation 1 use a specific provider, namely "BC", for its algorithms. In general, though, it is not recommended to request specific providers, since not all providers are available on all Java implementations, whether for lack of support, to avoid code duplication, or for other reasons. This advice has especially become important since the release of Android P preview in early 2018, because some functionality from the "BC" provider has been deprecated there — see the article "Cryptography Changes in Android P" in the Android Developers Blog. See also the Introduction to Oracle Providers.

    Thus, PROVIDER should not exist and the string -BC should be removed from PBE_ALGORITHM. Implementation 2 is correct in this respect.

    It is inappropriate for a method to catch all exceptions, but rather to handle only the exceptions it can. The implementations given in your question can throw a variety of checked exceptions. A method can choose to wrap only those checked exceptions with CryptoException, or specify those checked exceptions in the throws clause. For convenience, wrapping the original exception with CryptoException may be appropriate here, since there are potentially many checked exceptions the classes can throw.

    SecureRandom in Android

    As detailed in the article "Some SecureRandom Thoughts", in the Android Developers Blog, the implementation of java.security.SecureRandom in Android releases before 2013 has a flaw that reduces the strength of random numbers it delivers. This flaw can be mitigated as described in that article.

    0 讨论(0)
  • 2020-11-30 17:06

    I found a nice implementation here : http://nelenkov.blogspot.fr/2012/04/using-password-based-encryption-on.html and https://github.com/nelenkov/android-pbe That was also helpful in my quest for a good enough AES Implementation for Android

    0 讨论(0)
  • 2020-11-30 17:08

    You have asked a pretty interesting question. As with all algorithms the cipher key is the "secret sauce", since once that's known to the public, everything else is too. So you look into ways to this document by Google

    security

    Besides Google In-App Billing also gives thoughts on security which is insightful as well

    billing_best_practices

    0 讨论(0)
  • 2020-11-30 17:09

    #2 should never be used as it uses only "AES" (which means ECB mode encryption on text, a big no-no) for the cipher. I'll just talk about #1.

    The first implementation seems to adhere to best practices for encryption. The constants are generally OK, although both the salt size and the number of iterations for performing PBE are on the short side. Futhermore, it seems to be for AES-256 since the PBE key generation uses 256 as a hard coded value (a shame after all those constants). It uses CBC and PKCS5Padding which is at least what you would expect.

    Completely missing is any authentication/integrity protection, so an attacker can change the cipher text. This means that padding oracle attacks are possible in a client/server model. It also means that an attacker can try and change the encrypted data. This will likely result in some error somewhere becaues the padding or content is not accepted by the application, but that's not a situation that you want to be in.

    Exception handling and input validation could be enhanced, catching Exception is always wrong in my book. Furhtermore, the class implements ICrypt, which I don't know. I do know that having only methods without side effects in a class is a bit weird. Normally, you would make those static. There is no buffering of Cipher instances etc., so every required object gets created ad-nauseum. However, you can safely remove ICrypto from the definition it seems, in that case you could also refactor the code to static methods (or rewrite it to be more object oriented, your choice).

    The problem is that any wrapper always makes assumptions about the use case. To say that a wrapper is right or wrong is therefore bunk. This is why I always try to avoid generating wrapper classes. But at least it does not seem explicitly wrong.

    0 讨论(0)
提交回复
热议问题