前端 js加密 后台java 解密 RSA

       
前端代码 :
      $.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() ;
} }

git:https://gitee.com/xxoo0_297/js_rsa_java.git

上一篇:【Android Studio安装部署系列】十二、Android studio代码混淆


下一篇:快速搭建Spring Boot项目