des加密,url编码,url解码,des解密 DEMO
package com.example.core.mydemo.des; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.SecureRandom; /** * output: * 原始字符串 = {"phone":"1391111","username":"张三"} * des加密 = 0O8cJ5ZMZyVBL0DUiBoWkLuUvajcaOKqC/4RTWcl6+fjdjK/gXWWJg== * url编码 = 0O8cJ5ZMZyVBL0DUiBoWkLuUvajcaOKqC%2F4RTWcl6%2BfjdjK%2FgXWWJg%3D%3D * url解码 = 0O8cJ5ZMZyVBL0DUiBoWkLuUvajcaOKqC%2F4RTWcl6%2BfjdjK%2FgXWWJg%3D%3D * des解密 = {"phone":"1391111","username":"张三"} */ public class DesUtls { public static String getDESStr(String str, String encryptKey, String type, String charset) throws Exception { SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(encryptKey.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); //加密 if ("ENCRYPT".equals(type)) { //警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除 // BASE64Encoder base64encoder = new BASE64Encoder(); cipher.init(1, securekey, random); // return base64encoder.encode(cipher.doFinal(str.getBytes(charset))); return Base64Encoder.encode(cipher.doFinal(str.getBytes(charset))); }else if ("DECRYPT".equals(type)) { //警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除 // BASE64Decoder base64decoder = new BASE64Decoder(); // byte[] encodeByte = base64decoder.decodeBuffer(str); byte[] encodeByte = Base64Encoder.decode(str.getBytes()); cipher.init(2, securekey, random); byte[] decoder = cipher.doFinal(encodeByte); return new String(decoder, charset); } return "type error"; } public static void main(String[] args) { String encryptKey = "testkey111"; String type = "ENCRYPT"; String charset = "UTF-8"; String str = "{\"phone\":\"1391111\",\"username\":\"张三\"}"; System.out.println("原始字符串 = " + str); try { //des加密 String DESStr= DesUtls.getDESStr(str,encryptKey,type,charset); System.out.println("des加密 = " + DESStr); //url编码 DESStr = URLEncoder.encode(DESStr,"UTF-8"); System.out.println("url编码 = " + DESStr); //url解码 String s = URLDecoder.decode(DESStr,"UTF-8"); System.out.println("url解码 = " + DESStr); //javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher //des解密 System.out.println("des解密 = " + getDESStr(s,encryptKey,"DECRYPT","UTF-8")); //ENCRYPT(des加密) encode(url编码) >> decode(url解码) encrypt(des解密) } catch (Exception e) { e.printStackTrace(); } } } package com.example.core.mydemo.des; import org.apache.commons.codec.binary.Base64; public class Base64Encoder { /** * @param bytes * @return */ public static byte[] decode(final byte[] bytes) { return Base64.decodeBase64(bytes); } /** * 二进制数据编码为BASE64字符串 * * @param bytes * @return * @throws Exception */ public static String encode(final byte[] bytes) { return new String(Base64.encodeBase64(bytes)); } }
标签:return,String,url,DEMO,des,str,import From: https://www.cnblogs.com/oktokeep/p/17917821.html