From cdc90eec3e5077f2f79adb458255081dff07f1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E5=9B=BD=E4=B8=9C?= Date: Fri, 23 Sep 2022 10:08:33 +0800 Subject: [PATCH] =?UTF-8?q?MD5=E5=AF=86=E7=A0=81=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/encryption/PasswdFactory.java | 214 +----------------- 1 file changed, 6 insertions(+), 208 deletions(-) diff --git a/utils/src/main/java/com/common/utils/encryption/PasswdFactory.java b/utils/src/main/java/com/common/utils/encryption/PasswdFactory.java index 9319a64..cb0c974 100644 --- a/utils/src/main/java/com/common/utils/encryption/PasswdFactory.java +++ b/utils/src/main/java/com/common/utils/encryption/PasswdFactory.java @@ -19,216 +19,14 @@ import java.util.Locale; */ public class PasswdFactory { - private PasswdFactory() { - throw new AssertionError("No " + getClass().getName() + " instances for you!"); - } - /** - * 加密密码 - * - * @param id 用户主键 - * @param username 用户账户 - * @param passwd 用户密码 - * @return + * 加密 + * @version v1.0 + * @author dong + * @date 2022/9/23 10:07 */ - public static String encryptPasswd(String id, String username, String passwd) { -// System.out.println("id -> "+id); -// System.out.println("username -> "+username); -// System.out.println("passwd -> "+passwd); - String str = id + "@" + BASE64.encode(username) + "@" + passwd; - return AES.encryptToHex(str, key(id), iv()); - } - - /** - * 解密密码 - * - * @param id 用户主键 - * @param passwd 加密米密码 - * @return - */ - public static String decryptPasswd(String id, String passwd) { - String encryptText = AES.decryptByHex(passwd, - key(id), iv()); - if (encryptText != null) { - return encryptText.split("@")[2]; - } - return null; - } - - static String iv() { - return AES.padRight(AES.padLeft("Az", 9, '*'), 16, '0'); - } - - static String key(String id) { - return MD5.md5(id, 32); - } - - static class AES { - - static byte[] encrypt(String src, String key, String iv) { - try { - SecretKeySpec keySpec = new SecretKeySpec(toHash256ByteArray(key), "AES"); - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)); - cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); - byte[] encrypted = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8)); - return Base64.getMimeEncoder().encode(encrypted); - } catch (Exception e) { - return null; - } - } - - static String encryptToHex(String src, String key, String iv) { - try { - return byteArrayToHex(encrypt(src, key, iv)); - } catch (Exception e) { - return null; - } - } - - static String decrypt(byte[] src, String key, String iv) { - try { - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - SecretKeySpec keySpec = new SecretKeySpec(toHash256ByteArray(key), "AES"); - IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)); - cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); - byte[] encrypted = Base64.getMimeDecoder().decode(src); - return new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8); - } catch (Exception e) { - return null; - } - } - - static String decryptByHex(String src, String key, String iv) { - try { - return decrypt(hexToByteArray(src), key, iv); - } catch (Exception e) { - return null; - } - } - - static byte[] toHash256ByteArray(String src) { - try { - MessageDigest digester = MessageDigest.getInstance("SHA-256"); - digester.update(src.getBytes(StandardCharsets.UTF_8)); - return digester.digest(); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e.getMessage()); - } - } - - static String byteArrayToHex(byte[] src) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < src.length; i++) { - String hex = Integer.toHexString(src[i] & 0xFF); - if (hex.length() == 1) { - hex = '0' + hex; - } - sb.append(hex.toUpperCase(Locale.ENGLISH)); - } - return sb.toString(); - } - - static byte[] hexToByteArray(String src) { - byte[] bytes = new byte[src.length() / 2]; - for (int index = 0; index <= src.length() - 1; index += 2) { - String sub = src.substring(index, index + 2); - int value = Integer.parseInt(sub, 16); - bytes[index / 2] = (byte) value; - } - return bytes; - } - - static String padRight(String str, int len, char c) { - int offset = len - str.length(); - if (offset <= 0) { - return str; - } else { - char[] chars = new char[len]; - System.arraycopy(str.toCharArray(), 0, chars, 0, str.length()); - - for (int i = str.length(); i < len; ++i) { - chars[i] = c; - } - return new String(chars); - } - } - - static String padLeft(String str, int len, char c) { - int offset = len - str.length(); - if (offset <= 0) return str; - char[] chars = new char[len]; - System.arraycopy(str.toCharArray(), 0, chars, offset, str.length()); - for (int i = 0; i < offset; i++) { - chars[i] = c; - } - return new String(chars); - } - - } - - static class MD5 { - - static String md5(String str, int bit) { - String result = null; - try { - if (bit == 64 || bit == 32 || bit == 16) { - byte[] data = str.getBytes(StandardCharsets.UTF_8); - MessageDigest md = MessageDigest.getInstance("md5"); - if (bit == 64) { - Base64.Encoder encoder = Base64.getMimeEncoder(); - result = encoder.encodeToString(md.digest(data)); - } else { - StringBuilder sb = new StringBuilder(); - md.update(data); - byte[] bytes = md.digest(); - int i = 0; - for (byte b : bytes) { - i = b; - if (i < 0) - i += 256; - if (i < 16) { - sb.append("00"); - } else { - sb.append(Integer.toHexString(b & 0xFF)); - } - } - result = sb.toString().toUpperCase(); - if (bit == 16) { - result = result.substring(8, 24); - } - } - } - } catch (Exception e) { - // ignore - } - return result; - } - } - - static class BASE64 { - - static String encode(String src) { - if (src == null) return null; - Base64.Encoder encoder = Base64.getEncoder(); - return encoder.encodeToString(src.getBytes(StandardCharsets.UTF_8)); - } - - static String decode(String src) { - if (src == null) return null; - Base64.Decoder encoder = Base64.getDecoder(); - return new String(encoder.decode(src), StandardCharsets.UTF_8); - } - } - - public static void main(String[] args) { - String id = "e0246ac8-d382-460c-8540-fca750bbeb5b"; - String username = "成都强森化工有限公司"; - String password = ""; - String encrypt = encryptPasswd(id, username, password); - System.out.println(encrypt); - String decrypt = decryptPasswd(id, encrypt); - System.out.println(decrypt); + public static String encryptPasswd(String id, String username, String passwd)throws Exception { + return MD5.md5(id+passwd); }