Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保安";“加密”;Android N中已弃用提供程序,下面的代码现在是否需要替换?_Android_Encryption - Fatal编程技术网

保安";“加密”;Android N中已弃用提供程序,下面的代码现在是否需要替换?

保安";“加密”;Android N中已弃用提供程序,下面的代码现在是否需要替换?,android,encryption,Android,Encryption,博客: 正如博客中所述,它建议不要使用提供者的硬编码命名,下面的代码是否受此不推荐的影响? import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.cry

博客:

正如博客中所述,它建议不要使用提供者的硬编码命名,下面的代码是否受此不推荐的影响?

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; } }

导入java.io.UnsupportedEncodingException; 导入java.security.InvalidKeyException; 导入java.security.NoSuchAlgorithmException; 导入java.security.spec.InvalidKeySpecException; 导入javax.crypto.BadPaddingException; 导入javax.crypto.Cipher; 导入javax.crypto.IllegalBlockSizeException; 导入javax.crypto.NoSuchPaddingException; 导入javax.crypto.SecretKey; 导入javax.crypto.SecretKeyFactory; 导入javax.crypto.spec.DESKeySpec; 导入android.util.Base64; 导入android.util.Log; 公共类本地加密 { 公共静态字符串encryptIt(字符串值、字符串加密过程){ if(value==null | | value.trim().length()==0) { 返回值; } 试一试{ DESKeySpec keySpec=newdeskeyspec(cryptoPass.getBytes(“UTF8”)); SecretKeyFactory keyFactory=SecretKeyFactory.getInstance(“DES”); SecretKey key=keyFactory.generateSecret(keySpec); 字节[]明文=value.getBytes(“UTF8”); //密码不是线程安全的 Cipher Cipher=Cipher.getInstance(“DES”); cipher.init(cipher.ENCRYPT_模式,密钥); //Log.d(“加密:“+value+”->”,encrypedValue); 返回Base64.encodeToString(cipher.doFinal(明文),Base64.DEFAULT); }捕获(InvalidKeyException e){ e、 printStackTrace(); }捕获(不支持的编码异常e){ e、 printStackTrace(); }捕获(InvalidKeySpece异常){ e、 printStackTrace(); }捕获(无算法异常){ e、 printStackTrace(); }捕获(BadPaddingException e){ e、 printStackTrace(); }捕获(无此填充例外){ e、 printStackTrace(); }捕获(非法块大小异常e){ e、 printStackTrace(); } 返回值; }; 公共静态字符串解密(字符串值、字符串加密过程){ if(value==null | | value.trim().length()==0) { 返回值; } 试一试{ DESKeySpec keySpec=newdeskeyspec(cryptoPass.getBytes(“UTF8”)); SecretKeyFactory keyFactory=SecretKeyFactory.getInstance(“DES”); SecretKey key=keyFactory.generateSecret(keySpec); 字节[]encrypedPwdBytes=Base64.decode(值,Base64.DEFAULT); //密码不是线程安全的 Cipher Cipher=Cipher.getInstance(“DES”); cipher.init(cipher.DECRYPT_模式,密钥); byte[]decrypedValueBytes=(cipher.doFinal(encrypedPwdBytes)); String decrypedValue=新字符串(decrypedValue字节,“UTF8”); //Log.d(“解密:”+value+“->”,decrypedValue); 返回decrypedValue; }捕获(InvalidKeyException e){ e、 printStackTrace(); }捕获(不支持的编码异常e){ e、 printStackTrace(); }捕获(InvalidKeySpece异常){ e、 printStackTrace(); }捕获(无算法异常){ e、 printStackTrace(); }捕获(BadPaddingException e){ e、 printStackTrace(); }捕获(无此填充例外){ e、 printStackTrace(); }捕获(非法块大小异常e){ e、 printStackTrace(); } 返回值; } }
您的代码中没有任何内容与本文中的任何内容相关。然而,您的代码有足够多的不好之处,但这将是另一个问题的主题。谢谢James,是的,这可能是一个不好的代码,因为它超出了我的理解水平&我从中复制了它,它有错误的捕获异常的层次结构,对不对?一般建议:始终使用完全限定的密码字符串
Cipher.getInstance(“DES”)可能会产生不同的密码。它很可能导致添加“
”DES/ECB/pkcs5p”
,但不一定非得如此。如果它发生更改,您将失去不同JVM之间的兼容性。供参考:现在不要使用DES。它只提供56位的安全性。AES会更好,因为它的最小密钥大小为128位,更安全。DES的最大密文大小也有实际限制。看,永远不要使用。它是确定性的,因此在语义上不安全。您至少应该使用随机模式,如或。最好是对密文进行身份验证,这样就不可能进行类似的攻击。这可以通过诸如GCM或EAX之类的经过身份验证的模式来实现,也可以通过一个方案来实现。