![](/img/trans.png)
[英]java.security.NoSuchAlgorithmException: AES/ECB/PKCS5Padding
[英]by which i can replace AES/ECB/PKCS5Padding for security concern in android application?
在我的IDE中,警告顯示為Android 4.3及更高版本上的潛在不安全隨機數。
閱讀Android開發博客以獲取更多信息
如何使其安全?
package com.example.encryptographytest;
import java.security.Security;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String key = "1234567891234567";
String data = "example";
Log.d("CRYPTO-TEST", ""+ decrypt(encrypt(data, key), key));
Log.d("CRYPTO-TEST", ""+encrypt(data, key));
}
public static String encrypt(String input, String key){
byte[] crypted = null;
try{
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
}catch(Exception e){
System.out.println(e.toString());
}
return new String(Base64.encode(crypted, Base64.DEFAULT));
// return new String(Base64.encodeBase64(crypted));
}
public static String decrypt(String input, String key){
byte[] output = null;
try{
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal( Base64.decode(input, Base64.DEFAULT));
}catch(Exception e){
System.out.println(e.toString());
}
return new String(output);
}
}
首先,Android 4.3本質上是不安全的。 現在有多種漏洞利用,包括諸如StageFright之類的遠程漏洞利用。
其次,假設您正在談論some-securerandom-thoughts.html ,因為您的鏈接已失效。 它在談論隨機數的生成,但是我在您的代碼中看不到。 相反,我看到了AES加密,這不是完全隨機的。
另外,查看在Android 4.3及更早版本上可能不安全的隨機數 ,此警告可能與您的代碼的先前版本有關,后者依賴於SecureRandom來初始化KeyGenerator。
用谷歌搜索“ aes not secure”會帶來很多結果和意見,但對於大多數人來說似乎足夠安全。
話雖這么說,為“ ecb不安全”做google會帶來為什么我不應該使用ECB加密的問題? ,恰如其分地說明了為什么它不安全。 但這在任何平台上都是不安全的,不僅僅是Android 4.3。
希望對您有所幫助,請說明警告是否確實來自此代碼段或指定確切的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.