js加密java解密_前端 js加密 后台java 解密 RSA

packagecom.tran.demo.controller;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.stereotype.Controller;importorg.springframework.util.ResourceUtils;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importcom.tran.demo.utils.SecurityUtil;importcom.tran.demo.utils.RetResponse;importcom.tran.demo.utils.RetResult;importcn.hutool.crypto.asymmetric.RSA;

@Controllerpublic classWebController {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")publicString index() {return "index";

}

@RequestMapping("/decrypt")

@ResponseBodypublic RetResult decrypt(byte [] encrypt) throwsException {

File file= ResourceUtils.getFile("classpath:keys/secret_key.txt");

Properties pro= new Properties();//创建集合

pro.load(newFileInputStream(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);returnRetResponse.makeOKRsp();

}

@ResponseBody

@RequestMapping("/getPulbicKey")public String getPublicKey() throwsIOException {

RSA rsa= newRSA();//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= newFileOutputStream(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);

}

}

}returnrsa.getPublicKeyBase64() ;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值