BASE64Decoder替代类Base64
来源:互联网 发布:itertools python 编辑:程序博客网 时间:2024/05/16 01:28
之前base64加密用的是sun公司的sun.misc.BASE64Encoder/BASE64Decoder,由于后面版本更新,sun公司被oracle公司收购,加密类BASE64Encoder被org.apache.commons.codec.binary.Base64替代了。
两者加密数据的结果其实是相同的
import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.HashMap;import java.util.Map;import java.util.TreeMap;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import sun.misc.BASE64Encoder;import com.alibaba.fastjson.JSONObject;public class TestEncode { private static final Logger logger = LoggerFactory.getLogger(TestEncode.class); public static void main(String[] args) throws Exception { JSONObject condition = new JSONObject(); condition.put("payAmount", "1"); condition.put("merchantCode", "XM01"); condition.put("transDesc", "选择支付渠道"); condition.put("bizNo", "dadad3333"); condition.put("bizType", "76"); condition.put("idType", "5"); condition.put("accountName", "卫涵菡"); condition.put("serialNo", "2"); condition.put("idNo", "131124197907242963"); String encryptKey = "abc"; String result1 = aesEncrypt1(condition.toJSONString(),encryptKey); String result2 = aesEncrypt2(condition.toJSONString(),encryptKey); logger.info(result1); logger.info(result2); } public static String aesEncrypt1(String content, String encryptKey) throws Exception { logger.info("加密前的数据content={}",content); return base64Encode1(aesEncryptToBytes(content, encryptKey)); } public static String aesEncrypt2(String content, String encryptKey) throws Exception { logger.info("加密前的数据content={}",content); return base64Encode2(aesEncryptToBytes(content, encryptKey)); } public static String base64Encode1(byte[] bytes) { return new String(new Base64().encode(bytes)); } public static String base64Encode2(byte[] bytes) { return new BASE64Encoder().encode(bytes); } public static byte[] aesEncryptToBytes(String content, String encryptKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(encryptKey.getBytes()); kgen.init(128, random); Cipher cipher = Cipher.getInstance("AES"); cipher.init(1, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); return cipher.doFinal(content.getBytes("utf-8")); } }
阅读全文
0 0
- BASE64Decoder 替代类 Base64
- BASE64Decoder替代类Base64
- sun.misc.BASE64Decoder 替代类
- sun.misc.BASE64Decoder替代类
- BASE64Decoder
- 什么是Base64以及sun.misc.BASE64Decoder的用法
- sun.misc.BASE64Decoder 导入 错误,找不到base64包
- 什么是Base64以及sun.misc.BASE64Decoder的用法
- 什么是Base64以及sun.misc.BASE64Decoder的用法
- Base64加密以及sun.misc.BASE64Decoder的用法
- base64编码解码及sun.misc.BASE64Decoder的用法
- 什么是Base64以及sun.misc.BASE64Decoder的用法
- sun.misc.BASE64Decoder 导入 错误,找不到base64包
- Base64加密以及sun.misc.BASE64Decoder的用法
- Base64类
- Base64类
- Eclipse报错找不到sun.misc.BASE64Decoder类的解决办法!
- BASE64Decoder小解
- Google Guava学习(10)-Guava字符串工具Spiltter
- android监听Home键
- SEO独家揭秘:政府高权重锚文本虚拟外链技术!
- Ubuntu下搭建spark集群开发环境
- 模块进阶
- BASE64Decoder替代类Base64
- jsp自定义标签
- 为什么在重写 equals方法的同时必须重写 hashcode方法
- 如何遍历HttpServletRequest获取请求参数
- 解决Java中乱码最狠的办法,直接在Tomcat中修改
- idea 开启@Slf4j 注解
- conda的一些命令
- 创建类和对象
- OpenStack职业道路:老板想要什么