首先附上JS代码,文件路径在不同的环境下配置实现过程中修改相应的位置。
//加密
//str 是想加密的字符串
//pwd 是key
function encrypt(str, pwd) {
if (pwd == null || pwd.length <= 0) {
alert("Please enter a password with which to encrypt the message.");
return null;
}
var prand = "";
for (var i = 0; i < pwd.length; i++) {
prand += pwd.charCodeAt(i).toString();
}
var sPos = Math.floor(prand.length / 5);
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
var incr = Math.ceil(pwd.length / 2);
var modu = Math.pow(2, 31) - 1;
if (mult < 2) {
alert("Algorithm cannot find a suitable hash. Please choose a different password. Possible considerations are to choose a more complex or longer password.");
return null;
}
var salt = Math.round(Math.random() * 1000000000) % 100000000;
prand += salt;
while (prand.length > 10) {
prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
}
prand = (mult * prand + incr) % modu;
var enc_chr = "";
var enc_str = "";
for (var i = 0; i < str.length; i++) {
enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
if (enc_chr < 16) {
enc_str += "0" + enc_chr.toString(16);
} else {
enc_str += enc_chr.toString(16);
}
prand = (mult * prand + incr) % modu;
}
salt = salt.toString(16);
while (salt.length < 8) {
salt = "0" + salt;
}
enc_str += salt;
return enc_str;
}
//解密实现
function decrypt(str, pwd) {
if (str == null || str.length < 8) {
alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
return;
}
if (pwd == null || pwd.length <= 0) {
alert("Please enter a password with which to decrypt the message.");
return;
}
var prand = "";
for (var i = 0; i < pwd.length; i++) {
prand += pwd.charCodeAt(i).toString();
}
var sPos = Math.floor(prand.length / 5);
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
var incr = Math.round(pwd.length / 2);
var modu = Math.pow(2, 31) - 1;
var salt = parseInt(str.substring(str.length - 8, str.length), 16);
str = str.substring(0, str.length - 8);
prand += salt;
while (prand.length > 10) {
prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
}
prand = (mult * prand + incr) % modu;
var enc_chr = "";
var enc_str = "";
for (var i = 0; i < str.length; i += 2) {
enc_chr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
enc_str += String.fromCharCode(enc_chr);
prand = (mult * prand + incr) % modu;
}
return enc_str;
}Java调用ScriptEngineManager进行JS角本的解析。对应的JAVA代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import javax.script.ScriptEngineManager;
import com.sun.script.javascript.RhinoScriptEngine;
/**
* * Copyright (C): http://www.html580.com
*
* Description:
*
*
* @author dengzhifeng 280160522@qq.com
*/
public class DesUtils {
static private DesUtils desUtils;
public static String key = "html580.com";
public static String path = "/WEB-INF/views/js/des.js";
ScriptEngineManager engineManager;
RhinoScriptEngine engine;
public DesUtils() {
URL url = getClass().getResource("/");
try {
String file = (URLDecoder.decode(url.getPath(),"utf-8")+"../views/js/des.js").replaceAll("%20", " ");
String content = readFile(new File(file));
engineManager = new ScriptEngineManager();
engine = (RhinoScriptEngine)engineManager.getEngineByName("javascript");
engine.eval(content);
} catch (Exception e) {
e.printStackTrace();
}
}
static public DesUtils getInstance() {
synchronized (DesUtils.class) {
if (desUtils == null)
desUtils = new DesUtils();
}
return desUtils;
}
public String decrypt(String value) {
try {
Object pwd=engine.invokeFunction("decrypt", value ,key);
return pwd.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String encrypt(String value) {
try {
Object pwd=engine.invokeFunction("encrypt", value ,key);
return pwd.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void main(String<> args) {
String e = DesUtils.getInstance().encrypt("test") ;
System.out.println("pwd=" + e );
System.out.println("pwd=" + DesUtils.getInstance().decrypt( e ));
}
/**
* 读取文件内容
*/
public static String readFile(File file) {
String content = "";
InputStream is = null;
try {
is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
String str = line.trim() ;
content += str + "\n" ;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
}
}
return content;
}
}前端JS调用
后端Java调用参加上面JAVA写法。