前端 js加密 後臺java 解密 RSA
阿新 • • 發佈:2019-03-17
property all abc col 後臺 type row resource var
前端代碼 :
$.ajax({
type:"GET",
url:"http://localhost:8084/getPulbicKey",
dataType:"json",
success:function(data){
console.log(data);
var encrypt = new JSEncrypt();
encrypt.setPublicKey(data);
var encryptData = encrypt.encrypt("abc");//加密後的字符串 JSON加密
console.log(encryptData)
$.ajax({
type:"GET",
url:"http://localhost:8084/decrypt",
dataType:"json",
data:{ "encrypt":encryptData},
success:function(data){
console.log(data);
},
error:function(data){
console.log("error:"+data);
}
});
},
error: function(data){
console.log("error:"+data);
}
});
後臺代碼:
package com.tran.demo.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tran.demo.utils.SecurityUtil;
import com.tran.demo.utils.RetResponse;
import com.tran.demo.utils.RetResult;
import cn.hutool.crypto.asymmetric.RSA;
@Controller
public class WebController {
private final static Logger log = LoggerFactory.getLogger(WebController.class);
private final static String publicKey = "PUBLIC_KEY";
private final static String privateKey = "PRIVATE_KEY";
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/decrypt")
@ResponseBody
public RetResult decrypt(byte [] encrypt) throws Exception {
File file = ResourceUtils.getFile("classpath:keys/secret_key.txt");
Properties pro = new Properties();//創建集合
pro.load(new FileInputStream(file));
System.out.println("PUBLIC_KEY"+pro.get("PRIVATE_KEY"));
System.out.println("PRIVATE_KEY"+pro.get("PUBLIC_KEY"));
String decryptRSADefault = SecurityUtil.decryptRSADefault(pro.get("PRIVATE_KEY").toString(), new String(encrypt,"UTF-8"));
System.out.println("解密:"+decryptRSADefault);
return RetResponse.makeOKRsp();
}
@ResponseBody
@RequestMapping("/getPulbicKey")
public String getPublicKey() throws IOException {
RSA rsa = new RSA();
//Resource resource = new ClassPathResource("keys/secret_key.txt");
File file = ResourceUtils.getFile("classpath:keys/secret_key.txt");
Properties pro = new Properties();//創建集合
FileOutputStream out = null ;
try {
if(!file.exists()) {
file.createNewFile();
}
pro.setProperty(privateKey, rsa.getPrivateKeyBase64());
pro.setProperty(publicKey, rsa.getPublicKeyBase64());
out = new FileOutputStream(file);
pro.store(out,"密鑰");
out.flush();
} catch (Exception e) {
log.error("流異常:::{}", e );
} finally {
pro.clear();
if(out != null ) {
try {
out.close();
} catch (IOException e) {
log.error("流異常",e);
}
}
}
return rsa.getPublicKeyBase64() ;
}
}
前端 js加密 後臺java 解密 RSA