RSA非堆成加密有公钥和私钥,公钥用于加密,私钥用于解密
一,密钥生成
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Map;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class CreateSecretKey {
public static String getPublicKey(Map<String , Object> keyMap) throws Exception{
Key key = (Key)keyMap.get("PUBLIC_KEY");
return encryptBASE64(key.getEncoded()) ;
}
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception{
Key key = (Key)keyMap.get("PRIVATE_KEY");
return encryptBASE64(key.getEncoded()) ;
}
public static byte[] decryptBASE64(String key) throws Exception{
return new BASE64Decoder().decodeBuffer(key);
}
public static String encryptBASE64(byte[] key) throws Exception{
return new BASE64Encoder().encode(key);
}
public static RsaPairKey initKey() throws Exception{
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
KeyPair keyPair = keyPairGen.generateKeyPair();
return new RsaPairKey(keyPair) ;
}
static class RsaPairKey{
private String privateKey ;
private String publicKey ;
public RsaPairKey(KeyPair pairKey) {
try {
this.privateKey = encryptBASE64(pairKey.getPrivate().getEncoded()) ;
this.publicKey = encryptBASE64(pairKey.getPublic().getEncoded());
} catch (Exception e) {
}
}
public String getPrivateKey() {
return this.privateKey ;
}
public String getPublicKey() {
return this.publicKey ;
}
}
}
二,加密和解密
需要先引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
import org.jasypt.encryption.StringEncryptor;
import com.ulisesbocchio.jasyptspringboot.encryptor.SimpleAsymmetricConfig;
import com.ulisesbocchio.jasyptspringboot.encryptor.SimpleAsymmetricStringEncryptor;
import com.ulisesbocchio.jasyptspringboot.util.AsymmetricCryptography.KeyFormat;
public class GenerateEncryUtils {
public static void main(String[] args) {
SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
config.setKeyFormat(KeyFormat.PEM);
config.setPublicKey("-----BEGIN PUBLIC KEY-----\r\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoIogBPmly4Z104zmD5VL\r\n" +
"gs5sj2Iu5DNWXX2KNIRgqnW6eIfNHPWOoXA6Nq5Waw064iA32LeOVGger4GdZp9t\r\n" +
"FZSdk9kGwCekzy0nAC/pIfjJsCmsi9X7Xs+tVRnettkjonVhqI2hmrN2iO73oBDk\r\n" +
"SVZm0sDEXInu13RbXvKoUo1RTDCRdSMO8uYV/I+ygiV7/IdwEiB4k5c9j4RmWmMj\r\n" +
"4dWk9miHjdMzziAnRUc3BzeRTdu4DIW9oupV8JYwK6FNonv3pbVU4dOfzAdPQzHI\r\n" +
"4odCBCE4oXrVoQpn/c9JzYVG0/qOijyeHFgAF9q19XqG7nALHVgS3m2skyrFwBa6\r\n" +
"BwIDAQAB\r\n" +
"-----END PUBLIC KEY-----\r\n" +
"");
StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
String username = "username";
String password = "password";
String enName = encryptor.encrypt(username);
String enPsw = encryptor.encrypt(password);
System.out.println("Encrypted username :"+enName);
System.out.println("Encrypted password :"+enPsw);
}
public static String encryString(String pubKey , String value) {
SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
config.setKeyFormat(KeyFormat.PEM);
config.setPublicKey(pubKey);
StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
return encryptor.encrypt(value);
}
public static String decryString(String priKey , String value) {
SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
config.setKeyFormat(KeyFormat.PEM);
config.setPrivateKey(priKey);
StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
return encryptor.decrypt(value);
}
}