| Java标准库提供了一个URLEncoder类来对任意字符串进行URL编码 |
| |
| # 加密 |
| public class Main { |
| public static void main(String[] args) { |
| String encoded = URLEncoder.encode("中文!", StandardCharsets.UTF_8); |
| System.out.println(encoded); |
| } |
| } |
| # 控制台 |
| %E4%B8%AD%E6%96%87%21 |
| |
| # 解密 |
| public class Main { |
| public static void main(String[] args) { |
| String decoded = URLDecoder.decode("%E4%B8%AD%E6%96%87%21", StandardCharsets.UTF_8); |
| System.out.println(decoded); |
| } |
| } |
| # 控制台 |
| 中文! |
| Base64编码可以把任意长度的二进制数据变为纯文本 |
| |
| # 加密 |
| public class Main { |
| public static void main(String[] args) { |
| byte[] input = new byte[] { (byte) 0xe4, (byte) 0xb8, (byte) 0xad }; |
| String b64encoded = Base64.getEncoder().encodeToString(input); |
| System.out.println(b64encoded); |
| } |
| } |
| # 控制台 |
| 5Lit |
| |
| # 解密 |
| public class Main { |
| public static void main(String[] args) { |
| byte[] output = Base64.getDecoder().decode("5Lit"); |
| System.out.println(Arrays.toString(output)); |
| } |
| } |
| # 控制台 |
| [-28, -72, -83] |
| |
| # 数组长度不是3的整数时,加密 |
| public class Main { |
| public static void main(String[] args) { |
| byte[] input = new byte[] { (byte) 0xe4, (byte) 0xb8, (byte) 0xad, 0x21 }; |
| String b64encoded = Base64.getEncoder().encodeToString(input); |
| String b64encoded2 = Base64.getEncoder().withoutPadding().encodeToString(input); |
| System.out.println(b64encoded); |
| System.out.println(b64encoded2); |
| byte[] output = Base64.getDecoder().decode(b64encoded2); |
| System.out.println(Arrays.toString(output)); |
| } |
| } |
| # 控制台 |
| 5LitIQ== |
| 5LitIQ |
| [-28, -72, -83, 33] |
| |
| # 针对URL的Base64编码 |
| public class Main { |
| public static void main(String[] args) { |
| byte[] input = new byte[] { 0x01, 0x02, 0x7f, 0x00 }; |
| String b64encoded = Base64.getUrlEncoder().encodeToString(input); |
| System.out.println(b64encoded); |
| byte[] output = Base64.getUrlDecoder().decode(b64encoded); |
| System.out.println(Arrays.toString(output)); |
| } |
| } |
| # 控制台 |
| AQJ_AA== |
| [1, 2, 127, 0] |
算法 |
输出长度(位) |
输出长度(字节) |
MD5 |
128 bits |
16 bytes |
SHA-1 |
160 bits |
20 bytes |
RipeMD-160 |
160 bits |
20 bytes |
SHA-256 |
256 bits |
32 bytes |
SHA-512 |
512 bits |
64 bytes |
| public class Main { |
| public static void main(String[] args) throws Exception { |
| |
| MessageDigest md = MessageDigest.getInstance("MD5"); |
| |
| md.update("Hello".getBytes("UTF-8")); |
| md.update("World".getBytes("UTF-8")); |
| byte[] result = md.digest(); |
| System.out.println(new BigInteger(1, result).toString(16)); |
| } |
| } |
| # 控制台 |
| 68e109f0f40ca72a15e05cc22786f8e6 |
| package com.md5.demo; |
| |
| import java.security.MessageDigest; |
| import java.util.Random; |
| |
| import org.apache.commons.codec.binary.Hex; |
| |
| |
| |
| public class PasswordUtil { |
| |
| |
| |
| public static String generate(String password) { |
| Random r = new Random(); |
| StringBuilder sb = new StringBuilder(16); |
| sb.append(r.nextInt(99999999)).append(r.nextInt(99999999)); |
| int len = sb.length(); |
| if (len < 16) { |
| for (int i = 0; i < 16 - len; i++) { |
| sb.append("0"); |
| } |
| } |
| String salt = sb.toString(); |
| password = md5Hex(password + salt); |
| char[] cs = new char[48]; |
| for (int i = 0; i < 48; i += 3) { |
| cs[i] = password.charAt(i / 3 * 2); |
| char c = salt.charAt(i / 3); |
| cs[i + 1] = c; |
| cs[i + 2] = password.charAt(i / 3 * 2 + 1); |
| } |
| return new String(cs); |
| } |
| |
| |
| |
| |
| public static boolean verify(String password, String md5) { |
| char[] cs1 = new char[32]; |
| char[] cs2 = new char[16]; |
| for (int i = 0; i < 48; i += 3) { |
| cs1[i / 3 * 2] = md5.charAt(i); |
| cs1[i / 3 * 2 + 1] = md5.charAt(i + 2); |
| cs2[i / 3] = md5.charAt(i + 1); |
| } |
| String salt = new String(cs2); |
| return md5Hex(password + salt).equals(new String(cs1)); |
| } |
| |
| |
| |
| |
| public static String md5Hex(String src) { |
| try { |
| MessageDigest md5 = MessageDigest.getInstance("MD5"); |
| byte[] bs = md5.digest(src.getBytes()); |
| return new String(new Hex().encode(bs)); |
| } catch (Exception e) { |
| return null; |
| } |
| } |
| } |
| |
| # 测试 |
| public static void main(String[] args) { |
| |
| String password1 = generate("admin"); |
| System.out.println("结果:" + password1 + " 长度:"+ password1.length()); |
| |
| System.out.println(verify("admin", password1)); |
| |
| |
| String password2= generate("admin"); |
| System.out.println("结果:" + password2 + " 长度:"+ password2.length()); |
| |
| System.out.println(verify("admin", password2)); |
| } |
| |
| public static String getRandomString(int length){ |
| String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| Random random=new Random(); |
| StringBuffer sb=new StringBuffer(); |
| for(int i=0;i<length;i++){ |
| int number=random.nextInt(62); |
| sb.append(str.charAt(number)); |
| } |
| return sb.toString(); |
| } |
| |
| String password1 = DigestUtils.md5Hex(stu1.name+stu1.password+stu1.email); |
| System.out.println(password1); |
| |
| |
| String password1 = DigestUtils.md5Hex(stu1.name+stu1.password+stu1.email); |
| DigestUtils.md5Hex(DigestUtils.md5Hex("774"+password1+"@@__")); |
| public class Main { |
| public static void main(String[] args) throws Exception { |
| |
| MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| |
| md.update("Hello".getBytes("UTF-8")); |
| md.update("World".getBytes("UTF-8")); |
| byte[] result = md.digest(); |
| System.out.println(new BigInteger(1, result).toString(16)); |
| } |
| } |
| # 控制台 |
| db8ac1c259eb89d4a131b253bacfca5f319d54f2 |
| public class Test1 { |
| |
| public static void main(String[] args) throws Exception { |
| |
| MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| |
| md.update("Hello".getBytes("UTF-8")); |
| md.update("World".getBytes("UTF-8")); |
| byte[] result = md.digest(); |
| System.out.println(new BigInteger(1, result).toString(16)); |
| } |
| |
| } |
| # 控制台 |
| 872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4 |
| public class Test1 { |
| |
| public static void main(String[] args) throws Exception { |
| |
| MessageDigest md = MessageDigest.getInstance("SHA-512"); |
| |
| md.update("Hello".getBytes("UTF-8")); |
| md.update("World".getBytes("UTF-8")); |
| byte[] result = md.digest(); |
| System.out.println(new BigInteger(1, result).toString(16)); |
| } |
| |
| } |
| # 控制台 |
| 8ae6ae71a75d3fb2e0225deeb004faf95d816a0a58093eb4cb5a3aa0f197050d7a4dc0a2d5c6fbae5fb5b0d536a0a9e6b686369fa57a027687c3630321547596 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-05-07 vue2.0入门