使用BASE64Encoder及BASE64Decoder,Conversion to Dalvik format failed with error 1
阿新 • • 發佈:2018-12-30
可能也會出現java.lang.NoClassDefFoundError 問題
一篇文章提到不要使用sun.misc包下的BASE64Encoder及BASE64Decoder。這兩個方法都是sun公司的內部方法,並沒有在java api中公開過,所以使用這些方法是不安全的,將來隨時可能會從中去除,所以相應的應該使用替代的物件及方法
點選開啟連結
所以可以考慮使用Base64
例如:
/** * 加密 * @param content * @param key * @return */ public static String encryptAES(String content, String key) { try { byte[] byteContent = content.getBytes("UTF-8"); byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = AES_KEY_NUMBER.getBytes(); IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encryptedBytes = cipher.doFinal(byteContent); return new String(Base64.encode(encryptedBytes, Base64.DEFAULT)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } /** * 解密 * @param content * @param key * @return */ public static String decryptAES(String content, String key) { try { byte[] bytesrc = Base64.decode(content.getBytes(), Base64.DEFAULT); byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = AES_KEY_NUMBER.getBytes(); IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); byte[] result = cipher.doFinal(bytesrc); return new String(result, "UTF-8"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } }
其中
import android.util.Base64;
另外需要匯入rt.jar包。