1、在pom文件中引入依赖
<!-- 对配置文件中的数据进行加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
2、在项目resources文件夹中新建文件夹“key”。并添加以下三个公钥和私钥文件
命名:private.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAzgdvW9uvYoqzlAXpM7gsBxCaRGrt1h9fIwWYfZUDO9qEI+S5
h9jmPhfglnX0O8s05p1pWU1YbDqCR4kIiAyv5RKe1HJd3BxJ1kp+Y/soBjNYqXvi
gkn90TgQE9Ip1t+LR5kRMgRbwSvvFqXm0aId7468g8LxyavjHRfJy7uuiVVCw8qC
h7HBdsIn/p3oS7yi64qbooRc+VQv+/NLFLeL0izENXgpzCJEK/4Yy2ttrP6LODqP
3fGmbjy2eNcOV2JkWGio/Akcr0K1+JsvW4wOGDn9Atr1hzMgo9lpjjiBkRfzxrYN
YMiKLO+AKW6WApzQmWuoSAEEPs9yq4yua5UY9wIDAQABAoIBACiKjf8lW32ojW2z
PBBRTViWi8SqxeI8ihv6HeeYSuqcNSRa/acdpzDQwgDZdRXPBrnaf8By19N0L+6j
qGodFP+eimKDnrSRXHYucnB6G4Yc1HWFSy0YIjnpLkC4OkNt+Zo9JIUTFnsMih+K
xcC6hel9zNqgobNtf2NaKvtMEywyoGH/XD55rrQ24vqrIkb+yKjbMPA45ELaUsmk
gWdS9CR2uA40dQg4JEmghvjidD1MaNrTndeNnSEZzdyiRulksA0gdfuqGRJMtVHJ
tzkY7Q0ahpTk9Fn9jfUy3Uos6c+8rnzrA1TnXoIM9qjwl+9oBxK0/WxXUsmTCxbs
PeVuygECgYEA7VL1CgB2SB4CmnaMHhcVkJjBYL3RFRvM7Q6zLK4DWpfMQDBP1Bal
6mIRfnpBq/ksHKbIcvv4j5e86eADgoXxPpTnXZfJn0jy11cYikaelHp9Zzej9xOt
KslxR/s0v0uYvFj9GeWrS9DqwOMzY6Dw04RyG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EqqjzAIGE3IXnj4WgJVw928EJKSTTW9HjrChlBTwB1XItrYVQo34t
Eqblu19MZRtyszeBAvlMT+UqcXAo04drEU2M/DLBAoGBAIgCMkp9INWWLbeVYHpk
I0G2IkBg+tiBARZ966Jnp1X4tJcdFQoJdP1n1POn+xz/AfP990TTy4KO4yRi43RU
umVBylS8e+EDHGCEzh+xkk70zryNi13Tm+LZeEnwSTbozcmu55/5aDB/dBbQwl/J
FMrbdBB4KdCPelKWpdpQrFCz
-----END PRIVATE KEY-----
命名:public.pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzgdvW9uvYoqzlAXpM7gs
BxCaRGrt1h9fIwWYfZUDO9qEI+S5h9jmPhfglnX0O8s05p1pWU1YbDqCR4kIiAyv
5RKe1HJd3BxJ1kp+Y/soBjNYqXvigkn90TgQE9Ip1t+LR5kRMgRbwSvvFqXm0aId
7468g8LxyavjHRfJy7uuiVVCw8qCh7HBdsIn/p3oS7yi64qbooRc+VQv+/NLFLeL
0izENXgpzCJEK/4Yy2ttrP6LODqP3fGmbjy2eNcOV2JkWGio/Akcr0K1+JsvW4wO
GDn9Atr1hzMgo9lpjjiBkRfzxrYNYMiKLO+AKW6WApzQmWuoSAEEPs9yq4yua5UY
9wIDAQAB
-----END PUBLIC KEY-----
3、编写EncryptTest.java测试类
import static com.ulisesbocchio.jasyptspringboot.util.AsymmetricCryptography.KeyFormat.PEM;
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.boot.test.context.SpringBootTest;
import com.ulisesbocchio.jasyptspringboot.encryptor.SimpleAsymmetricConfig;
import com.ulisesbocchio.jasyptspringboot.encryptor.SimpleAsymmetricStringEncryptor;
import lombok.extern.slf4j.Slf4j;
@SpringBootTest
@Slf4j
public class EncryptTest {
@Test
public void testEncrypt() {
final SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
config.setPublicKeyFormat(PEM);
config.setPrivateKeyFormat(PEM);
config.setPublicKeyLocation("classpath:key/public.pem");
config.setPrivateKeyLocation("classpath:key/privatepkcs8.pem");
final StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
final String message = "admin";
final String encrypted = encryptor.encrypt(message);
log.info("Provided String {}", message);
log.info("Encrypted message {}\n", encrypted);
final String decrypted = encryptor.decrypt(encrypted);
Assertions.assertEquals(decrypted, message);
log.info("decrypted message {}", decrypted);
}
}
4、运行测试
![](https://i-blog.csdnimg.cn/blog_migrate/65a5e2a08e235bbd56d1c0a12741c554.png)
最终生成密码:
RjP7G81AO5rL+1CC/HYxFc832KdCOlnsSrBLS+ooqL5w99/nKHqn7WKrTEnHQabHg7QW7vGO8qA+Wsefjb74GjWMI+NKfGTsb5UOF432+0RmnP57FenaCsv32AN5Ax70CrjFpxTp/D+hWkQH6KXJ+FK7vLN/zTc04m9OhEgJXJU7630n0HQ3ZuPElqGT8Im6CNNB73HDagWFEUIJXIB/f0orwupYhmiCCMGrcz6ATauz7klwnfPwkIhNSZqh05XCWDx4abJYDRosv8pZG7B4F7eYfh+U2oRH+mewGX1/WGZuMeKhYPANVtjXPtLcaRa8IJtqxuDKkOWO+oj7kuz7kA==