본문 바로가기
Framework/Spring

[암호화,복호화] Spring Jasypt(Java Simplified Encryption)

by Cocopop 2023. 7. 31.
반응형

Jasypt 란

  • 개발자가 암호화 작동 방식 자신의 프로젝트에 기본 암호화 기능을 추가할 수 있도록 하는 Java 라이브러리.

 

 

사용 방법

 

1. 라이브러리 추가

  • Maven → pom.xml → 라이브러리 추가
<!-- jasypt 1.9.3 for spring 4 -->
<!-- com.melloware.jasypt의 경우 StringFixedSaltGenerator를 사용하면 오류가 발생하고 있어 org.jasypt.jasypt를 사용해야 한다. -->
<dependency>
      <groupId>org.jasypt</groupId>
      <artifactId>jasypt-spring4</artifactId>
      <version>1.9.3</version>
</dependency>

 

2. 기본 설정

  • 자바 : class를 생성해서 Console로 확인한다.

 

(공통 - jasypt 복호화 테스트 클래스)

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// 내용 : 공통 - jasypt 복호화 테스트 클래스 
public class JasyptDecryptTest {

	private final static Logger log = LoggerFactory.getLogger(JasyptDecryptTest.class);
	
	public static void main(String[] args) {
		
		String inputStr = "uKS1vgcBMAih7jOPelAk7L0yC7l7hLbd";
	
		StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor();
		pbeEnc.setAlgorithm("PBEWithMD5AndDES");
		pbeEnc.setPassword("1wax3rdv");
		
		log.info("InputStr : "+inputStr);
		String decResult = pbeEnc.decrypt(inputStr);
		log.info("Decrypted: "+decResult);
		
	} 
		
}

 

 

 

 

(공통 - jasypt 암호화 테스트 클래스)

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// * 내용 : 공통 - jasypt 암호화 테스트 클래스 
public class JasyptEncryptTest {

	private final static Logger log = LoggerFactory.getLogger(JasyptEncryptTest.class);
	
	public static void main(String[] args) {
		String inputStr = "jdbc:log4jdbc:oracle:thin:@localhost:1521:STUDY";
		
		StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor();
		pbeEnc.setAlgorithm("PBEWithMD5AndDES");
		pbeEnc.setPassword("cocopop1234");
		
		String encResult = pbeEnc.encrypt(inputStr);
		log.info("Encrypted: "+encResult);
		String decResult = pbeEnc.decrypt(encResult);
		log.info("Decrypted: "+decResult);
		
	}
	
}

 

 

Mybatis 설정

mybatis-context.xml , database-config.properties

 

 

 

 

mybatis-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
   
    <!-- jasypt 라이브러리 사용  -->
    <!-- jasypt : java, Spring 등에서 암복호화를 지원해주는 오픈소스.  -->
    
    <!-- Properties file Encrypt bean start -->
    <!-- Jasypt 설정 -->
    <bean	id="encryptorConfig" class="org.jasypt.encryption.pbe.config.EnvironmentPBEConfig">
    	<property name="algorithm" value="PBEWithMD5AndDES" /> 	<!-- 알고리즘 암호값 -->
   		<property name="password" value="패스워드" />		<!-- 패스워드 -->
    </bean> 
   
    <!-- Password-Based-Encryption 알고리즘을 사용하여 문자열을 암호화하고 복호화하는 클래스 -->
    <bean	id="encryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
    	<property name="config" ref="encryptorConfig" />
    </bean>
    
    <!-- Spring 애플리케이션의 프로퍼티를 암호화하고 복호화하는데 사용할 수 있는 Srping Bean -->
    <bean class="org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer">
    	<constructor-arg ref="encryptor" />
    	<property name="locations">
    		<list>
    			<value>classpath:config/database-config.properties</value>
    		</list>
    	</property>
    </bean>
    <!-- Properties file Encrypt bean end -->
   
   	<!-- database connection info, properties -->
   	<!-- 이 태그를 사용하면 외부 properties 파일 로딩한다. (데이터베이스 properties) -->
	<context:property-placeholder location="classpath:config/database-config.properties" />

<beans>

 

 

 

database-config.properties

jdbc.coco.driver=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
jdbc.coco.url=ENC(JLtL55cLQlbxFr2VLA57aXiKUxHlDmOsUjjl3mAAwK7L2zdpkBYtsdlfmskfmlsfHuqg=)
jdbc.coco.username=ENC(dUe99ojghCR532BnVrer4w==)
jdbc.coco.password=ENC(sTekldkc2vRSe6rgj/4676Q==)

→ java 암호화로 인한 값을 ENC() 안에 감싸 작성

300x250