使用Android 4.2及更早版本加密 - PRNGFixes

时间:2015-02-17 17:21:15

标签: android security prng

我需要加密密码,但对于Android 4.2及以下版本,我的解决方案不起作用,加密密码是随机的。这是由于PRNG。所以我看到了这篇文章:

https://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html

我实施了PRNGFix,但它没有改变......

如何解决这个随机生成数字的问题?

代码:

KeySpec spec = new PBEKeySpec(PASSWORD.toCharArray(), byteSalt, NB_ITER_RFC, SIZE_KEY);
SecretKey temp = factory.generateSecret(spec);
Cipher c = Cipher.getInstance(DES_EDE_PKCS5);
IvParameterSpec ivParam = new IvParameterSpec(bytesIv);
c.init(Cipher.ENCRYPT_MODE, temp, ivParam);
byte[] encrypted = c.doFinal(texteAChiffrer.getBytes("UTF-8"));
mdp = Base64.encodeToString(encrypted, Base64.DEFAULT);

或者:

PBEKeySpec pbeKeySpec = new PBEKeySpec(PASSWORD.toCharArray(), byteSalt, NB_ITER_RFC, SIZE_KEY);
byte[] key2 = PBEParametersGenerator.PKCS12PasswordToBytes(pbeKeySpec.getPassword());
SecretKey temp2 = factory.generateSecret(pbeKeySpec);
Cipher c2 = Cipher.getInstance(DES_EDE_PKCS5);
c2.init(Cipher.ENCRYPT_MODE, temp2, ivParam);
byte[] encrypted2 = c2.doFinal(texteAChiffrer.getBytes("UTF-8"));       
mdp = Base64.encodeToString(encrypted2, Base64.DEFAULT);

这两个解决方案从Android 4.3和最新版本(4.4和5.0)得到相同的结果

感谢您的帮助:)

0 个答案:

没有答案