"Your app contains unsafe cryptographic encryption patterns" - How I can get rid of this w
"Your app contains unsafe cryptographic encryption patterns" - How I can get rid of this warning?
Ask Question Asked 2 years, 2 months ago Active 2 years ago Viewed 5k times edited Sep 19 '19 at 9:37 asked Sep 19 '19 at 2:29
OR
Jetpack Security
For more details: Remediation for Unsafe Cryptographic Encryption
Share Improve this answer answered Nov 5 '19 at 7:39
- security-crypto library forcing to update min-sdk-version 26. Any solution for the lower version? – Azay Gupta Nov 6 '19 at 7:03
- 1 What if you need to decrypt some data that was previously encrypted with a former key? – Roman Samoilenko Sep 19 '19 at 5:36
- For that you can use asymmetric cryptography. which encrypt data with different private key and on other end decrypt data with public key. This link may help you. – Mahesh Sep 19 '19 at 6:49
- 2 How is it possible to have a single public key that can decrypt a message encrypted with a different private key? Aren't the keys generated as a standalone pair? – Roman Samoilenko Sep 19 '19 at 11:20
-
I think, it's not related to original asked questions. Plz ask separate question. but you can get your questions answer from here.
– Mahesh
Sep 23 '19 at 7:39
https://stackoverflow.com/questions/58002913/your-app-contains-unsafe-cryptographic-encryption-patterns-how-i-can-get-rid
#########################################################################################################################################################
unsafe cryptographic encryption patterns , How to solve it? [duplicate]
Ask Question Asked 2 years, 2 months ago Active 2 years, 2 months ago Viewed 2k times edited Oct 1 '19 at 6:31 asked Sep 20 '19 at 10:41Abdul Hanan 4377 bronze badges
- What password? The user's password??? – charles-allen Sep 20 '19 at 11:41
- its email+salt – Abdul Hanan Sep 21 '19 at 11:32
The cipher does not provide data integrity [com.lloyds.keystorage.AESCrypt] At AESCrypt.java:[line 25] CIPHER_INTEGRITY The cipher does not provide data integrity [com.lloyds.keystorage.AESCrypt] At AESCrypt.java:[line 15] CIPHER_INTEGRITY Hard coded cryptographic key found [com.lloyds.keystorage.AESCrypt] At AESCrypt.java:[line 35] HARD_CODE_KEY
The solution is to use a cipher that includes a Hash based Message Authentication Code (HMAC) to sign the data:
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
And to store the secret key in separate configuration files or keystores.
Below is the whole class after a full refactoring:
Share Improve this answer edited Sep 23 '19 at 11:12 answered Sep 20 '19 at 11:58import android.util.Base64 import static java.nio.charset.StandardCharsets.UTF_8; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class AESCrypt { private static final String TRANSFORMATION = "AES/GCM/NoPadding"; public static String encrypt(String value) throws Exception { Key key = generateKey(); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedByteValue = cipher.doFinal(value.getBytes(UTF_8)); return Base64.encodeToString(encryptedByteValue, Base64.DEFAULT); } public static String decrypt(String value) throws Exception { Key key = generateKey(); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedValue64 = Base64.decode(value, Base64.DEFAULT); byte[] decryptedByteValue = cipher.doFinal(decryptedValue64); return new String(decryptedByteValue, UTF_8); } private static Key generateKey() { return new SecretKeySpec(Configuration.getKey().getBytes(UTF_8), TRANSFORMATION); } }
Boris 17.9k1212 gold badges4242 silver badges6666 bronze badges
-
But,
java.util.Base64
requires API level 26. What's the reason of usingjava.util.Base64
orandroid.util.Base64
? – Cheok Yan Cheng Sep 22 '19 at 22:32 -
Also, what is the implementation of
Configuration.getKey()
? If it is always returning the same value for different devices, will Google flag warning again? – Cheok Yan Cheng Sep 22 '19 at 22:37 -
@CheokYanCheng, can you share a link to the resource showing that you need the level 26 for a Java 8 class
java.util.Base64
? – Boris Sep 23 '19 at 10:51 - 1 See developer.android.com/reference/java/util/Base64.Encoder (Added in API level 26) – Cheok Yan Cheng Sep 23 '19 at 10:58
- 1 @Boris i remove hard code key now the warning gone thanks. :-) – Abdul Hanan Oct 10 '19 at 6:24
Not the answer you're looking for? Browse other questions tagged java android firebase-authentication or ask your own question.
https://stackoverflow.com/questions/58026804/unsafe-cryptographic-encryption-patterns-how-to-solve-it
com.java.android.sample.Java...
? – Boris Sep 24 '19 at 11:39