目录
DES加解密算法:含DES密钥生成、参数名ASCII码从小到大排序
DES加解密算法
需要的Maven依赖
<!-- 工具类 --> <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- FASTJSON --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.20</version> </dependency>
使用的JDK版本:1.8.0_111
DES加解密算法:含DES密钥生成、参数名ASCII码从小到大排序
import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang.StringUtils; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; /** * @version V1.0 * @Title: DES加解密算法 * @ClassName: com.newcapec.util.encrypt.DLDesUtil.java * @Description: * SysConstant.CHARSET_UTF8 = "UTF-8"; * @Copyright 2016-2018 - Powered By 研发中心 * @author: 王延飞 * @date: 2018-08-16 11:43 */ public class DLDesUtil { private static final String algorithm = "DESede"; //算法名称 private static final String fullAlg = algorithm + "/CBC/PKCS5Padding"; /** * @param encryptSrc 待加密字符串 * @param encodeFlag 是否URL编码 1-URL编码 0-不URL编码 * @param keyData DES密钥 * @return java.lang.String * @Title: DES加密 * @methodName: encrypt * @Description: * @author: 王延飞 * @date: 2018-08-16 11:43 */ public static String encrypt(String encryptSrc, String encodeFlag, String keyData) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { Cipher cipher1 = Cipher.getInstance(fullAlg); int blockSize = cipher1.getBlockSize(); byte[] iv = new byte[blockSize]; for (int i = 0; i < blockSize; ++i) { iv[i] = 0; } Cipher cipher = Cipher.getInstance(fullAlg); SecretKey secretKey = new SecretKeySpec(Base64.decode(keyData), StringUtils.substringBefore(fullAlg, "/")); IvParameterSpec ivSpec = new IvParameterSpec(iv); /**加密*/ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); byte[] cipherBytes = cipher.doFinal(encryptSrc.getBytes(SysConstant.CHARSET_UTF8)); String cipherString = Base64.encode(cipherBytes); if (encodeFlag.equals("1")) { cipherString = URLEncoder.encode(cipherString, SysConstant.CHARSET_UTF8); } return cipherString; } /** * @param decryptSrc 待解密字符串 * @param decodeFlag 是否URL解码 1-URL解码 0-不URL解码 * @param keyData DES密钥 * @return java.lang.String * @Title: DES解密 * @methodName: decrypt * @Description: * @author: 王延飞 * @date: 2018-08-16 11:45 */ public static String decrypt(String decryptSrc, String decodeFlag, String keyData) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { if (decodeFlag.equals("1")) { decryptSrc = URLDecoder.decode(decryptSrc, SysConstant.CHARSET_UTF8); } Cipher cipher1 = Cipher.getInstance(fullAlg); int blockSize = cipher1.getBlockSize(); byte[] iv = new byte[blockSize]; for (int i = 0; i < blockSize; ++i) { iv[i] = 0; } Cipher cipher = Cipher.getInstance(fullAlg); SecretKey secretKey = new SecretKeySpec(Base64.decode(keyData), StringUtils.substringBefore(fullAlg, "/")); IvParameterSpec ivSpec = new IvParameterSpec(iv); /**解密*/ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); byte[] resultBytes = cipher.doFinal(Base64.decode(decryptSrc)); String resultStr = new String(resultBytes, SysConstant.CHARSET_UTF8); return resultStr; } private static final String[] keyArr = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}; /** * @return java.lang.String * @Title: 生成密钥 * @methodName: getDesPassword * @Description: * @author: 王延飞 * @date: 2018-08-16 11:47 */ public static String getDesPassword() { StringBuffer sb = new StringBuffer(); Random ran = new Random(); for (int i = 0; i < 32; i++) { sb.append(keyArr[ran.nextInt(58)]); } return sb.toString(); } /** * @param paramMap 参数 * @return java.lang.String * @Title: 参数名ASCII码从小到大排序 * @methodName: paramSort * @Description: * @author: 王延飞 * @date: 2018-08-16 14:29 */ public static String paramSort(Map<String, String> paramMap) { List<String> list = new ArrayList<>(); for (String mapkey : paramMap.keySet()) { String value = paramMap.get(mapkey); if (StringUtils.isNotEmpty(value)) { list.add(new StringBuilder(mapkey).append("=").append(value).toString()); } } Collections.sort(list); String paramSorted = StringUtils.join(list, "&"); return paramSorted; } /** * @param str URL键值对的格式(即key1=value1&key2=value2…) * @return java.util.Map<java.lang.String , java.lang.String> * @Title: URL键值对的格式(即key1=value1&key2=value2…),转换为MAP * @methodName: paramToMAP * @Description: * @author: 王延飞 * @date: 2018-08-16 16:41 */ public static Map<String, String> paramToMAP(String str) { String[] strs = str.split("&"); Map<String, String> m = new HashMap<String, String>(); for (String s : strs) { String[] ms = s.split("="); m.put(ms[0], ms[1]); } return m; } public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { JSONObject jsonObject = new JSONObject(); jsonObject.put("hostUserId", "470143"); jsonObject.put("hostCustomerCode", "6797"); jsonObject.put("hostCustomerName", "114测试-t50"); jsonObject.put("isBindEcard", "0"); jsonObject.put("payUserIdNum", "201700001"); jsonObject.put("payUserName", "张三1"); jsonObject.put("payUserPhone", "15515822065"); jsonObject.put("pluginType", "3"); jsonObject.put("interurl", "fillcard"); String jsonString = jsonObject.toJSONString(); System.out.println("参数名<JSON>:" + jsonString); //String key = DLDesUtil.getDesPassword(); String key = "X6kWEiP8BwjE3esQbIUQKST4iRHEtgKU"; System.out.println("密钥:" + key); String en = DLDesUtil.encrypt(jsonString, "1", key); System.out.println("密文:" + en); String de = DLDesUtil.decrypt(en, "1", key); System.out.println("明文:" + de); JSONObject json = JSONObject.parseObject(de); String hostUserId = json.getString("hostUserId"); System.out.println("hostUserId:" + hostUserId); /*String deskey = SysConstant.DL_CLOUD_DESKEY; String enStr = "gaZ7WbjF8Q1MY6%2BXldMDuP%2BCyiaBZndmbBe2hxqLqDDhiKnfSbGO%2BXbanwqqxkOKh9X2%2BuXX7YKpjVfBn6RPMWPus83sPnQtDHj2gXKvBcc%2Bys6m%2B8KQkpz3rYZBrQCKeKiAT69Su68KSr3JZCUlkB3Slh6dMJOPgACgvy7kXpNIlKHlMHshK6v7o%2FWmLCo73xC%2FwJRqn5wwJHnSiiKVZ0ub8VTKMRNpEUS0M3%2FBIc8%3D"; String de = DLDesUtil.decrypt(enStr, "1", deskey); System.out.println("明文:" + de); JSONObject json = JSONObject.parseObject(de);*/ } }
混合使用对称加密和非对称加密。
混合密码系统结合了非对称加密算法和对称加密算法的优势:
- 利用非对称加密算法的“非对称性”,解决对称密钥的传递问题。
- 利用对称加密算法的加解密速度,解决非对称加解密的速度问题。
混合密码系统的加密
混合加密
混合加密
- 发送者利用一个随机数生成器生成一个对称加密算法的会话密钥。
- 使用会话密钥加密会话消息,得到会话消息密文(对称加密)。
- 使用接受者的公钥加密会话密钥,得到会话密钥密文(非对称加密)。 4.组合会话密钥密文和会话消息密文。
混合密码系统的解密
混合解密
混合解密
- 分离密文,等到会话密钥密文和会话消息密文。
- 用私钥解密会话密钥密文,得到会话密钥(非对称加密)。
- 用会话密钥解密会话消息密文,得到会话消息(对称加密)。