[JASYPT] 프로퍼티 암호화
프로그래밍 개발을 할때엔 다양한 방법의 암호화가 있는데 오늘 다뤄볼 주제는 Java라이브러리인 Jasypt 이다.
발음이 정말 애매하긴한데 나는 자시프트라고 읽곤한다.
공식 홈페이지에 JASYPT 에 대한 설명은 아래와 같다.
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
Jasypt: Java simplified encryption - Jasypt: Java simplified encryption - Main
Jasypt 1.9.3 RELEASED! (May 25th, 2019) [DOWNLOAD and ChangeLogs] [WHAT'S NEW IN JASYPT 1.9] Java Simplified Encryption Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and wi
www.jasypt.org
https://github.com/jasypt/jasypt
GitHub - jasypt/jasypt: Jasypt (Java Simplified Encryption) is a java library which allows the developer to add basic encryption
Jasypt (Java Simplified Encryption) is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep kn...
github.com
사용 방법은 다른 라이브러리 적용처럼 의존성 추가해주고 사용을 하면 된다.
- implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:사용할버전'
그리고 config class 파일 작성이 필요하다.
import lombok.RequiredArgsConstructor; import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @RequiredArgsConstructor @Configuration public class JasyptConfig { // @Value("${jasypt.encryptor.password}") 환경변수 사용 시 주석 처리된 부분 이용 // private String encryptKey; @Bean("jasyptStringEncryptor") public StringEncryptor stringEncryptor() { PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); SimpleStringPBEConfig config = new SimpleStringPBEConfig(); // config.setPassword(encryptKey); config.setPassword("password"); config.setAlgorithm("PBEWithMD5AndDES"); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator"); config.setStringOutputType("base64"); encryptor.setConfig(config); return encryptor; } } |
그리고 프로퍼티스를 추가해준다.
jasypt.encryptor.bean=jasyptStringEncryptor jasypt.encryptor.algorithm=PBEWithMD5AndDES jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator |
사용은 아래처럼 테스트를 하면 된다.
String plainText = "hello"; PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword("password"); config.setAlgorithm("PBEWithMD5AndDES"); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator"); config.setStringOutputType("base64"); encryptor.setConfig(config); String encryptText = encryptor.encrypt(plainText); System.out.println(encryptText); String decryptText = encryptor.decrypt(encryptText); System.out.println(decryptText); assertThat(plainText).isEqualTo(decryptText); |