前端加密后端Java解密
分享⼀下最简单的处理,前端采⽤JS⾃带的 atob加密,后端采⽤⼯具解密
前端JS代码
//加密字符串,可以先将中⽂加密,然后再⽤btoa加密
encryption(str) {
var encStr = encodeURIComponent(str);
encStr = btoa(encStr);
return encStr;
},
//解密,可以先将ascii解密,然后再将⾮ascii解密
decrypt(str) {
var decStr = atob(str);
decStr = decodeURIComponent(decStr);
return decStr;
},
后端代码
package com.fudian.project.system.utils;
import Matcher;
import Pattern;
import java.io.UnsupportedEncodingException;
import java.URLDecoder;
import java.URLEncoder;
/
**
* Window.atob 加解密
* @author ZYGisComputer
*/
public class BtoaEncode {
public static void main(String[] args) {
String admin = ption("admin");
System.out.println(admin);
System.out.println(BtoaEncode.decrypt(admin));
}
private static String base64hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static boolean isMatcher(String inStr, String reg) {
Pattern pattern = Patternpile(reg);
Matcher matcher = pattern.matcher(inStr);
if (matcher.matches()) {
return true;
}
return false;
}
/**
* btoa method
* @param inStr
* @return
*/
public static String btoa(String inStr) {
if (inStr == null || isMatcher(inStr, "([^\\u0000-\\u00ff])")) {
return null;
return null;
}
StringBuilder result = new StringBuilder();
int i = 0;
int mod = 0;
int ascii;
int prev = 0;
while (i < inStr.length()) {
ascii = inStr.charAt(i);
mod = i % 3;
switch (mod) {
case 0:
result.append(String.valueOf(base64hash.charAt(ascii >> 2)));
break;
case 1:
result.append(String.valueOf(base64hash.charAt((prev & 3) << 4 | (ascii >> 4))));                    break;
case 2:
result.append(String.valueOf(base64hash.charAt((prev & 0x0f) << 2 | (ascii >> 6))));                    result.append(String.valueOf(base64hash.charAt(ascii & 0x3f)));
break;
}
prev = ascii;
i++;
}
if (mod == 0) {
result.append(String.valueOf(base64hash.charAt((prev & 3) << 4)));
result.append("==");
} else if (mod == 1) {
result.append(String.valueOf(base64hash.charAt((prev & 0x0f) << 2)));
result.append("=");
}
String();
}
/**
* atob method  逆转encode的思路即可
* @param inStr
* @return
*/
public static String atob(String inStr) {
if (inStr == null)
return null;
inStr = placeAll("\\s|=", "");
StringBuilder result = new StringBuilder();
int cur;
int prev = -1;
int mod;
int i = 0;
while (i < inStr.length()) {
cur = base64hash.indexOf(inStr.charAt(i));
mod = i % 4;js代码加密软件
switch (mod) {
case 0:
break;
case 1:
result.append(String.valueOf((char) (prev << 2 | cur >> 4)));
break;
case 2:
result.append(String.valueOf((char) ((prev & 0x0f) << 4 | cur >> 2)));
break;
case 3:
result.append(String.valueOf((char) ((prev & 3) << 6 | cur)));
result.append(String.valueOf((char) ((prev & 3) << 6 | cur)));                    break;
}
prev = cur;
i++;
}
String();
}
/**
* 加密字符串
* @return
*/
public static String encryption(String str) {
String encode;
try {
encode = de(str,"utf-8");
//de 会将空格解释为+号
placeAll("\\+", "%20");
String btoa = BtoaEncode.btoa(encode);
return btoa;
} catch (UnsupportedEncodingException e) {
System.out.println("btoa加密函数出现错误。");
}
return str;
}
/**
* 解密字符串
* @return
*/
public static String decrypt(String str) {
String atob = BtoaEncode.atob(str);
try {
String decode = URLDecoder.decode(atob, "utf-8");
return decode;
} catch (UnsupportedEncodingException e) {
System.out.println("atob加密函数出现错误。");
}
return str;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。