3DES加解密
2023-12-21 20:03:50
public static void main(String[] args) throws Exception { String content = ""; String plainText = ""; String key = ""; //加密 byte[] encryptedBytes = encrypt(plainText, key); String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println("Encrypted Text: " + encryptedText); //解密 这里注意不能直接content去转byte要先Base64解码 byte[] source = Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8)); String decryptedText = decrypt(source, key); System.out.println("Decrypted Text: " + decryptedText); } //解密 public static String decrypt(byte[] encryptedBytes, String key) throws Exception { byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); DESedeKeySpec spec = new DESedeKeySpec(keyBytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey secretKey = keyFactory.generateSecret(spec); ???????//PKCS5Padding 和 PKCS7Padding 都可以用PKCS5Padding Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes, StandardCharsets.UTF_8); } //加密 public static byte[] encrypt(String plainText, String key) throws Exception { byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); DESedeKeySpec spec = new DESedeKeySpec(keyBytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey secretKey = keyFactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); } //将url参数转换成map public static Map getUrlParams(String param) { Map map = new HashMap(0); if (StringUtil.isEmpty(param)) { return map; } String[] params = param.split("&"); for (int i = 0; i < params.length; i++) { String[] p = params[i].split("="); if (p.length == 2) { map.put(p[0], p[1]); } } return map; }
文章来源:https://blog.csdn.net/qq_17152035/article/details/135137708
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!