[java] Jasypt를 활용하여 자바 애플리케이션에서의 파일 암호화 및 복호화 방법

파일 암호화는 애플리케이션에서 중요한 데이터를 안전하게 보호하기 위해 필수적인 작업입니다. Jasypt는 자바 기반의 암호화 도구로, 간편한 사용법과 강력한 보안 기능을 제공합니다. 이번 글에서는 Jasypt를 사용하여 자바 애플리케이션에서 파일을 암호화하고 복호화하는 방법을 알아보겠습니다.

1. Jasypt 라이브러리 추가

먼저, Jasypt 라이브러리를 프로젝트에 추가해야 합니다. Maven을 사용한다면, pom.xml 파일에서 다음 의존성을 추가하세요.

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>

Gradle을 사용한다면, build.gradle 파일의 dependencies 섹션에서 다음을 추가하세요.

implementation 'org.jasypt:jasypt:1.9.3'

2. 파일 암호화하기

암호화할 파일의 내용을 읽어들인 후, Jasypt를 사용하여 암호화해야 합니다. 다음은 파일을 암호화하는 예제 코드입니다.

import org.jasypt.util.text.BasicTextEncryptor;

public class FileEncryptionExample {

    public static void main(String[] args) {
        String password = "mySecretPassword";
        String inputFile = "/path/to/input/file.txt";
        String outputFile = "/path/to/output/encrypted_file.txt";
        
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword(password);
        
        try {
            String plainText = FileUtils.readFileToString(new File(inputFile), StandardCharsets.UTF_8);
            String encryptedText = encryptor.encrypt(plainText);
            FileUtils.writeStringToFile(new File(outputFile), encryptedText, StandardCharsets.UTF_8);
            System.out.println("File encrypted successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

위 코드에서 password 변수에는 암호화에 사용될 비밀번호를 설정합니다. inputFile 변수에는 암호화할 파일의 경로를, outputFile 변수에는 암호화된 파일이 저장될 경로를 설정합니다. encryptor.encrypt() 메서드를 사용하여 파일 내용을 암호화하고, FileUtils.writeStringToFile() 메서드를 사용하여 암호화된 내용을 파일로 저장합니다.

3. 파일 복호화하기

복호화할 파일의 내용을 읽어들인 후, Jasypt를 사용하여 복호화해야 합니다. 다음은 파일을 복호화하는 예제 코드입니다.

import org.jasypt.util.text.BasicTextEncryptor;

public class FileDecryptionExample {

    public static void main(String[] args) {
        String password = "mySecretPassword";
        String encryptedFile = "/path/to/encrypted_file.txt";
        String outputFile = "/path/to/output/decrypted_file.txt";
        
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword(password);
        
        try {
            String encryptedText = FileUtils.readFileToString(new File(encryptedFile), StandardCharsets.UTF_8);
            String decryptedText = encryptor.decrypt(encryptedText);
            FileUtils.writeStringToFile(new File(outputFile), decryptedText, StandardCharsets.UTF_8);
            System.out.println("File decrypted successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

위 코드에서 password 변수에는 복호화에 사용될 비밀번호를 설정합니다. encryptedFile 변수에는 복호화할 암호화된 파일의 경로를, outputFile 변수에는 복호화된 파일이 저장될 경로를 설정합니다. encryptor.decrypt() 메서드를 사용하여 암호화된 파일 내용을 복호화하고, FileUtils.writeStringToFile() 메서드를 사용하여 복호화된 내용을 파일로 저장합니다.

결론

Jasypt를 사용하여 자바 애플리케이션에서 파일을 암호화하고 복호화하는 방법을 살펴보았습니다. 파일에 저장된 중요한 정보를 안전하게 보호하기 위해 이런 암호화 기술을 활용할 수 있습니다. 더 많은 Jasypt 기능과 옵션을 알아보려면 Jasypt 공식 문서를 참고하세요. ```