接口RSA加解密参考

后端依赖

当然,这里也可以自行实现,获取使用rsa+aes组合的方案来实现。

<dependency>
    <groupId>cn.shuibo</groupId>
    <artifactId>rsa-encrypt-body-spring-boot</artifactId>
    <version>1.0.1.RELEASE</version>
</dependency>

示例

@SpringBootApplication
@EnableSecurity
public class DemoApplication {
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
      
      // 加密
      @Encrypt
      @GetMapping("/encryption")
      public TestBean encryption(){
          TestBean testBean = new TestBean();
          testBean.setName("shuibo.cn");
          testBean.setAge(18);
          return testBean;
      }

      // 解密
      @Decrypt
      @PostMapping("/decryption")
      public String decryption(@RequestBody TestBean testBean){
          return testBean.toString();
      }
}

配置

rsa:
  encrypt:
    open: false # 是否开启加密 true  or  false
    showLog: true # 是否打印加解密log true  or  false
    publicKey: # RSA公钥 软件生成
    privateKey: # RSA私钥 软件生成

前端js

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.0.0-rc.1/jsencrypt.js"></script>

function rsa_encryption(jsonData) {
   var encrypt = new JSEncrypt();
   encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
   var encrypted = encrypt.encrypt(JSON.stringify(jsonData));
   console.log('加密前数据:%o', str);
   console.log('加密后数据:%o', encrypted);
   return encrypted;
}
posted @ 2024-03-19 14:37  itwetouch  阅读(18)  评论(0编辑  收藏  举报