import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
- @author lwb
- @date 2022/10/19 10:58 上午
*/
@RestController
@RequestMapping("/rsa")
public class RSADemo {
//私钥
@Value("${pcPrivateKey:123}")
private String pcPrivateKey;
//公钥
@Value("${pcPublicKey:123}")
private String pcPublicKey;
/**
* 公钥加密私钥解密
*/
@GetMapping("/encryption")
public String bind() {
RSA rsa = new RSA(null, pcPublicKey);
String data = "123456";//密码
String s = rsa.encryptBase64(data, KeyType.PublicKey);
System.out.println(s);//公钥加密后密码
RSA rsa2 = new RSA(pcPrivateKey, null);
String s1 = rsa2.decryptStr(s, KeyType.PrivateKey);
System.out.println(s1);//私钥解密后密码
return s1;
}
/**
* 私钥加密公钥解密
*/
@GetMapping("/encryption2")
public String bindas() {
RSA rsa = new RSA(pcPrivateKey, null);
String data = "123456";//密码
String s = rsa.encryptBase64(data, KeyType.PrivateKey);
System.out.println(s);//私钥加密后密码
RSA rsa2 = new RSA(null, pcPublicKey);
String s1 = rsa2.decryptStr(s, KeyType.PublicKey);
System.out.println(s1);//公钥解密后密码
return s1;
}
}