在Android N中不推荐使用安全“加密”提供程序,以下代码现在需要替代方案吗?

时间:2017-03-14 07:49:03

标签: android encryption

博客:https://android-developers.googleblog.com/2016/06/security-crypto-provider-deprecated-in.html

根据博客中的说明,它建议不要使用提供商的硬编码命名,以下代码是否会受到此弃用的影响?

import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import android.util.Base64; import android.util.Log; public class LocalCrypto { public static String encryptIt(String value,String cryptoPass) { if(value==null || value.trim().length()==0) { return value; } try { DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); byte[] clearText = value.getBytes("UTF8"); // Cipher is not thread safe Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); //Log.d( "Encrypted: " + value + " -> " , encrypedValue); return Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } return value; }; public static String decryptIt(String value,String cryptoPass) { if(value==null || value.trim().length()==0) { return value; } try { DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT); // cipher is not thread safe Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes)); String decrypedValue = new String(decrypedValueBytes,"UTF8"); //Log.d("Decrypted: " + value + " -> " , decrypedValue); return decrypedValue; } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } return value; } }

0 个答案:

没有答案