|
@@ -0,0 +1,107 @@
|
|
|
+package com.museum.common.util;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Hb_zzZ on 2020/4/17.
|
|
|
+ */
|
|
|
+public class Base64Converter {
|
|
|
+
|
|
|
+ final static Base64.Encoder encoder = Base64.getEncoder();
|
|
|
+ final static Base64.Decoder decoder = Base64.getDecoder();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给字符串加密
|
|
|
+ * @param text
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String encode(String text) {
|
|
|
+ byte[] textByte = new byte[0];
|
|
|
+ try {
|
|
|
+ textByte = text.getBytes("UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String encodedText = encoder.encodeToString(textByte);
|
|
|
+ return encodedText;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将加密后的字符串进行解密
|
|
|
+ * @param encodedText
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String decode(String encodedText) {
|
|
|
+ String text = null;
|
|
|
+ try {
|
|
|
+ text = new String(decoder.decode(encodedText), "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return text;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据逻辑截取加密后的密码
|
|
|
+ * @param text
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String subText(String text){
|
|
|
+ //去掉前8位字符串
|
|
|
+ text = text.substring(8);
|
|
|
+ //去掉后8位字符串
|
|
|
+ text = text.substring(0, text.length() - 8);
|
|
|
+ //最后两个字符串换到前面,并且去掉剩下的后8位字符串
|
|
|
+ String result = text.substring(text.length() - 2) + text.substring(0, text.length() - 10);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
+
|
|
|
+// String username = "Miracle Luna";
|
|
|
+// String password = "AUPhhlhkExMTExMTExMQ==tGC1irnLMTLF9V7HLh";
|
|
|
+ String password = "1UxELRpIExMTExcWG627AcMTQBIN2mog";
|
|
|
+
|
|
|
+ password = password.substring(8);
|
|
|
+ System.out.println(password);
|
|
|
+ password = password.substring(0, password.length() - 8);
|
|
|
+ System.out.println(password);
|
|
|
+ String key = password.substring(password.length() - 2) + password.substring(0, password.length() - 10);
|
|
|
+ System.out.println(key);
|
|
|
+
|
|
|
+
|
|
|
+ // 加密
|
|
|
+// System.out.println("==== [加密后] 用户名/密码 =====");
|
|
|
+// System.out.println(Base64Converter.encode(username));
|
|
|
+// System.out.println(Base64Converter.encode(password));
|
|
|
+
|
|
|
+ // 解密
|
|
|
+ System.out.println("\n==== [解密后] 用户名/密码 =====");
|
|
|
+// System.out.println(Base64Converter.decode(Base64Converter.encode(username)));
|
|
|
+ System.out.println(Base64Converter.decode(key));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ String password = "1UxELRpIExMTExcWG627AcMTQBIN2mog";
|
|
|
+ System.out.println(decodePassword(password));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * 将加密后的字符串进行解密
|
|
|
+ * @param ciphertext 密文
|
|
|
+ * @return 明文
|
|
|
+ */
|
|
|
+ public static String decodePassword(String ciphertext){
|
|
|
+ String key = subText(ciphertext);
|
|
|
+ return decode(key);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|