使用BASE64Encoder及BASE64Decoder,Conversion to Dalvik format failed with error 1

       可能也会出现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包。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值