๐ Jasypt(Java Simplified Encryption)
- ๊ฐ๋ฐ์๊ฐ ์ํธํ ์๋ ๋ฐฉ์์ ๋ํ ๊น์ ์ง์์ด ์์ด๋ ์ต์ํ์ ๋
ธ๋ ฅ์ผ๋ก ํ๋ก์ ํธ์ ๊ธฐ๋ณธ ์ํธํ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์๋ ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ํ๋กํผํฐ๋ก ๊ด๋ฆฌํ๋ DB ๊ณ์ ์ ๋ณด์ ๊ฐ์ ์ค์ ๊ฐ์ ํ๋ฌธ์ด ์๋ ์ํธ๋ฌธ์ผ๋ก ๊ด๋ฆฌ
๐ก Jasypt Spring Stater ๋์ ๋ฐฉ์
@SpringBootApplication
- ์ ํ๋ฆฌ์ผ์ด์
๊ตฌ๋ ๋จ๊ณ์์ ENC(์ํธํ ๋ ๊ฐ) ํ์์ ์์ฑ์ ์ฐพ์ ๋ณตํธํ ์ํ ํ ๋ณตํธํ ๋ ๊ฐ์ผ๋ก ์๋์ ์ํธํ๋ ์์ฑ ๊ฐ ๋์ฒด
๐งฉ Jasypt ์ฃผ์ ๋ฉ์๋
Key | Required | Default Value |
jasypt.encryptor.password | True | - |
jasypt.encryptor.algorithm | False | PBEWITHHMACSHA512ANDAES_256 |
jasypt.encryptor.key-obtention-iterations | False | 1000 |
jasypt.encryptor.pool-size | False | 1 |
jasypt.encryptor.provider-name | False | SunJCE |
jasypt.encryptor.provider-class-name | False | null |
jasypt.encryptor.salt-generator-classname | False | org.jasypt.salt.RandomSaltGenerator |
jasypt.encryptor.iv-generator-classname | False | org.jasypt.iv.RandomIvGenerator |
jasypt.encryptor.string-output-type | False | base64 |
jasypt.encryptor.proxy-property-sources | False | false |
jasypt.encryptor.skip-property-sources | False | empty list |
๋๋ค ์ํธ ์์ฑ๊ธฐ - ๋๋ค ์ํธ ์์ฑ๊ธฐ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ๋์ผํ ๋ฉ์์ง์ ๋ํ ๋๊ฐ์ ์ํธํ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฆ
๋ฌด์์ IV ์์ฑ๊ธฐ - IV๋ ๋ฌด์์์ฌ์ผ ํ๊ณ ํ ๋ฒ๋ง ์ฌ์ฉํด์ผ ํ๋ฏ๋ก org.jasypt.RandomIvGenerator๊ฐ ๊ถ์ฅ ๋จ.
๐จ ๋ํ์ ์ธ Jasypt ์ํธํ ์๊ณ ๋ฆฌ์ฆ
PBEWithMD5AndDES
- MD5 ํด์ ํจ์์ DES ๋์นญํค ์ํธํ๋ฅผ ์กฐํฉํ์ฌ ๋ฐ์ดํฐ ์ํธํ
- ๋น ๋ฅธ ์๋ณตํธํ ์๋๋ฅผ ๊ฐ์ง์ง๋ง ์๋์ ์ผ๋ก ๋ณด์์ ์ทจ์ฝ
PBES2WithHmacSHA512AndAES_256 (default)
- HmacSHA512 : Hmac์ ์ฌ์ฉํ์ฌ SHA-512 ํด์ ์๊ณ ๋ฆฌ์ฆ ๊ตฌํ
- HmacSHA512๋ก ๋ถํฐ ์์ฑ๋ ํค๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ AES-256 ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ์ฌ ์ํธํ
๐โโ๏ธ ์ฌ์ฉ ์์
build.gradle ์์กด์ฑ ์ถ๊ฐ
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'
application.yml
jasypt:
encryptor:
key: ${jasypt-key}
JasyptConfig.java
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;
@Configuration
public class JasyptConfig {
@Value("${jasypt.encryptor.key}")
private String key;
@Bean(name = "jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(key); // ์ํธํ ํค
config.setAlgorithm("PBEWithHMACSHA512AndAES_256"); // ์ํธํ ์๊ณ ๋ฆฌ์ฆ
config.setIvGenerator(new RandomIvGenerator()); // PBE-AES ๊ธฐ๋ฐ ์๊ณ ๋ฆฌ์ฆ์ ๊ฒฝ์ฐ IV ์์ฑ ํ์
config.setKeyObtentionIterations("1000"); // ๋ฐ๋ณตํ ํด์ฑ ํ์
config.setPoolSize("1"); // ์ธ์คํด์ค pool
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); // salt ์์ฑ ํด๋์ค
config.setStringOutputType("base64"); // ์ธ์ฝ๋ฉ
encryptor.setConfig(config);
return encryptor;
}
}
- application.yml์ jasypt.encryptor.key ๊ฐ์ด ๋
ธ์ถ๋๋ฉด ์๋๋ฏ๋ก ํ๊ฒฝ ๋ณ์๋ก ํ ๋น
class JasyptConfigTest {
private static final String SECRET_KEY = "my_secret_key";
@Test
void string_encryption() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(SECRET_KEY);
config.setAlgorithm("PBEWithHMACSHA512AndAES_256");
config.setIvGenerator(new RandomIvGenerator());
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
String originalString = "my_password";
// ์ํธํ
String encryptedString = encryptor.encrypt(originalString);
System.out.println("Encrypted String ::: ENC(" + encryptedString + ")");
// ๋ณตํธํ
String decryptedString = encryptor.decrypt(encryptedString);
System.out.println("Decrypted String ::: " + decryptedString);
assertEquals(originalString, decryptedString);
}
}
ํ
์คํธ ์ฝ๋๋ฅผ ์คํํด ๋์จ enc ๊ฐ์ผ๋ก ๋์ฒด
๐ง reference
https://github.com/ulisesbocchio/jasypt-spring-boot#use-you-own-custom-encryptor
http://www.jasypt.org/encrypting-texts.html
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] Service ServiceImpl ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ฐ๋์งํ๊ฐ (0) | 2023.03.12 |
---|---|
[Spring] Java ๋ฌธ์์ด null, ๋น๊ฐ, ๊ณต๋ฐฑ์ฒดํฌ ๋ฐฉ๋ฒ StringUtils.hasText (0) | 2022.10.19 |