728x90
반응형
전자정부 프레임워크 암호화 모듈 사용 방법
egovframework에서 암호화 모듈을 사용하는 방법을 정리하도록 하겠다.
pom.xml 설정
pom.xml 파일을 열어 적당한 위치에 아래의 코드를 추가한 후 ctrl + S 를 누른다.
<!-- 암호화 모듈 -->
<dependency>
<groupId>egovframework.rte</groupId>
<artifactId>egovframework.rte.fdl.crypto</artifactId>
<version>${egovframework.rte.version}</version>
</dependency>
globals.properties 설정
암호화 알고리즘과 key값을 정의한다. 필자는 key 값을 hshi으로 하였다.
#default (SHA-256)
crypto.password.algorithm=SHA-256
#password = hshi
crypto.hashed.password=OFEE5cPfCmi0FEFZCIVIak4LmoGcWuCjlbYZqDSnG44=
key 암호화 방법 (Encryption.java)
key 암호화를 위해 Encryption.java 파일을 생성한다. 필자는 key 값을 hshi으로 하였다.
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import egovframework.rte.fdl.cryptography.EgovCryptoService;
import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;
public class Encryption {
@Resource(name = "ARIACryptoService")
private static EgovCryptoService egovCryptoService;
@Resource(name = "passwordEncoder")
private EgovPasswordEncoder passwordEncoder;
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
// 암호화/복호화에 사용될 key 생성
EgovPasswordEncoder pe = new EgovPasswordEncoder();
String str = pe.encryptPassword("hshi");
System.out.println(str);
System.out.println(pe.checkPassword("hshi", str));
}
}
context-ariacrypto.xml 작성
암호화 방식을 정의한다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="passwordEncoder" class="egovframework.rte.fdl.cryptography.EgovPasswordEncoder">
<property name="algorithm" value="#{props['crypto.password.algorithm']}" /><!-- default : SHA-256 -->
<property name="hashedPassword" value="#{props['crypto.hashed.password']}" />
</bean>
<bean id="ARIACryptoService" class="egovframework.rte.fdl.cryptography.impl.EgovARIACryptoServiceImpl">
<property name="passwordEncoder" ref="passwordEncoder" />
<property name="blockSize" value="1024" /><!-- default : 1024 -->
</bean>
<bean id="digestService" class="egovframework.rte.fdl.cryptography.impl.EgovDigestServiceImpl">
<property name="algorithm" value="SHA-256" /><!-- default : SHA-256 -->
<property name="plainDigest" value="false" /><!-- default : false -->
</bean>
</beans>
암호화, 복호화 서비스 생성 (CryptoAriaService.java)
암호화, 복호화 메서드가 있는 서비스를 생성한다.
package egovframework.cmmn.service;
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;
import egovframework.rte.fdl.cryptography.EgovCryptoService;
@Service("cryptoAriaService")
public class CryptoAriaService {
@Resource(name="ARIACryptoService")
EgovCryptoService cryptoService;
public String encryptData(String plainText) {
String encodeText = null;
try {
byte[] encrypted = cryptoService.encrypt(plainText.getBytes("UTF-8"), "hshi");
encodeText = Base64.encodeBase64String(encrypted);
} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
return encodeText;
}
public String decryptData(String encodeText) {
String plainText = null;
try {
byte[] base64dec = Base64.decodeBase64(encodeText);
byte[] decrypted = cryptoService.decrypt(base64dec, "hshi");
plainText = new String(decrypted, "UTF-8");
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
return plainText;
}
}
사용방법
// CryptoAria Service
@Resource(name = "cryptoAriaService")
private CryptoAriaService cryptoAriaService;
cryptoAriaService.encryptData("hello world!");
cryptoAriaService.decryptData(cryptoAriaService.encryptData("hello world!"));
728x90
반응형
'프로그래밍 > JAVA' 카테고리의 다른 글
eclipse 속도 향상 방법 (eclipse.ini) (0) | 2022.05.30 |
---|---|
spring에서 weblogic JNDI 설정 방법 (5) | 2022.05.26 |
javaScript window객체와 opener 사용방법 (0) | 2022.05.16 |
Object형 소수점 2자리까지 표현하기 (0) | 2022.04.25 |
댓글