728x90

[관리자 페이지] 암호화 유틸리티 만들기



    // https://mvnrepository.com/artifact/org.mindrot/jbcrypt
    implementation group: 'org.mindrot', name: 'jbcrypt', version: '0.4'

Bcrypt 를 사용하기 위한 dependency 추가 !

package com.hiio.adminserver.utils;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

@Component
public class CryptoUtil {

    public String encrypt(String value) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Key key = this.getKey();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.toString().substring(0, 16).getBytes()));
        return new String(Base64.getEncoder().encode(cipher.doFinal(value.getBytes())));
    }

    public String decrypt(String encryptValue) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Key key = this.getKey();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.toString().substring(0, 16).getBytes()));

        return new String(cipher.doFinal(Base64.getDecoder().decode(encryptValue)));
    }

    public String bcrypt(String value){
        return  BCrypt.hashpw(value,BCrypt.gensalt());
    }

    public boolean validBcrypt(String value,String hashedValue){
        return BCrypt.checkpw(value,hashedValue);
    }


    public Key getKey() {
        return "12345678901234567890123456789012";

    }


}

메소드 생성

encrypt | decrypt | bcrypt | validBcrypt |
양방향암호화 | 복호화 | 단방향암호화 | 검증 |

AES/CBC/PKCS5Padding 알고리즘 사용

양방향 암호화의 경우 개인정보(이메일, 휴대폰 번호 등)이나 암호화가 필요한 정보를 DB에 저장 시 암호화해서 저장 하기 위해 사용
단방향 암호화의 경우 비밀번호 암호화에 사용

static 키워드를 사용한 객체 사용보다는 @Component 를 통해 Bean 등록 사용

// Crypto Util Test Code

package com.hiio.adminserver.utils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class CryptoUtilTest {
    private final Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private CryptoUtil cryptoUtil;
    @Test
    public void encrypt(){
        try{
            String value = "test@test.com";
            String encrypted = cryptoUtil.encrypt(value);
            log.info(encrypted);
        }catch(Exception e){
            log.error(e.getMessage());
        }
    }


    @Test
    public void decrypt(){
        try{
            String value = "4pzZnfHbutpNFvnWISGfzA==";
            String decrypted = cryptoUtil.decrypt(value);
            log.info(decrypted);
        }catch(Exception e){
            log.error(e.getMessage());
        }
    }

    @Test
    public void bcrypt(){
        String value="password";
        String bcrypted = cryptoUtil.bcrypt(value);
        log.info(bcrypted);
    }

    @Test
    public void validBcrypt(){
        String bcrypted = "$2a$10$jqOVKXzru5.okuYI8A13HeGvxZv3Zt9rVliaFLegTcamh6u4GTzU2";
        String plain = "password";
        boolean valided = cryptoUtil.validBcrypt(plain,bcrypted);
        Assertions.assertTrue(valided);


    }
}
728x90

+ Recent posts