반응형

전자정부프레임워크에서 제공하는 암호화 라이브러리를 활용하여 파일 암호화와 복호화를 구현하는 방법에 대해 설명합니다. 파일 보안은 중요한 정보가 유출되는 것을 방지하기 위한 필수 요소입니다.


1. 암호화와 복호화의 필요성

암호화는 데이터를 보호하기 위해 원본 데이터를 변환하는 기술입니다.
복호화는 암호화된 데이터를 다시 원래의 형태로 되돌리는 기술입니다.
전자정부프레임워크는 AES와 같은 강력한 알고리즘을 지원하여 안전한 데이터 보호를 제공합니다.


2. 전자정부프레임워크 암호화 도구 설정

전자정부프레임워크는 CryptoUtils를 통해 암호화와 복호화를 간단히 처리할 수 있습니다.

2-1. 의존성 추가

pom.xml 또는 build.gradle 파일에 암호화 라이브러리를 추가합니다.

<dependency>
    <groupId>egovframework.rte</groupId>
    <artifactId>egovframework.rte.fdl.cryptography</artifactId>
    <version>3.10.0</version>
</dependency>

3. 파일 암호화 구현

3-1. 암호화 유틸리티 클래스 작성

import egovframework.rte.fdl.cryptography.EgovCryptoService;
import egovframework.rte.fdl.cryptography.impl.EgovARIAEngine;
import egovframework.rte.fdl.cryptography.impl.EgovCryptoARIAServiceImpl;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;

@Component
public class FileEncryptionUtil {

    private final EgovCryptoService cryptoService;

    public FileEncryptionUtil() {
        EgovCryptoARIAServiceImpl cryptoServiceImpl = new EgovCryptoARIAServiceImpl();
        cryptoServiceImpl.setCrypto(new EgovARIAEngine());
        this.cryptoService = cryptoServiceImpl;
    }

    public void encryptFile(File inputFile, File encryptedFile, String key) throws Exception {
        byte[] fileData = Files.readAllBytes(inputFile.toPath());
        byte[] encryptedData = cryptoService.encrypt(fileData, key);
        try (FileOutputStream fos = new FileOutputStream(encryptedFile)) {
            fos.write(encryptedData);
        }
    }

    public void decryptFile(File encryptedFile, File decryptedFile, String key) throws Exception {
        byte[] fileData = Files.readAllBytes(encryptedFile.toPath());
        byte[] decryptedData = cryptoService.decrypt(fileData, key);
        try (FileOutputStream fos = new FileOutputStream(decryptedFile)) {
            fos.write(decryptedData);
        }
    }
}

3-2. Controller 작성

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileEncryptionUtil fileEncryptionUtil;

    private static final String ENCRYPTION_KEY = "mySecretKey12345"; // 16자 키

    @PostMapping("/encrypt")
    public String encryptFile(@RequestParam("file") MultipartFile file) throws Exception {
        File inputFile = new File(file.getOriginalFilename());
        file.transferTo(inputFile);

        File encryptedFile = new File("encrypted_" + inputFile.getName());
        fileEncryptionUtil.encryptFile(inputFile, encryptedFile, ENCRYPTION_KEY);

        return "파일이 암호화되었습니다: " + encryptedFile.getAbsolutePath();
    }

    @PostMapping("/decrypt")
    public String decryptFile(@RequestParam("file") MultipartFile file) throws Exception {
        File encryptedFile = new File(file.getOriginalFilename());
        file.transferTo(encryptedFile);

        File decryptedFile = new File("decrypted_" + encryptedFile.getName());
        fileEncryptionUtil.decryptFile(encryptedFile, decryptedFile, ENCRYPTION_KEY);

        return "파일이 복호화되었습니다: " + decryptedFile.getAbsolutePath();
    }
}

4. 암호화 및 복호화 테스트

4-1. 암호화 요청

  1. Postman 또는 curl 명령어로 파일 암호화를 요청합니다.
    • URL: /file/encrypt
    • Method: POST
    • Body: 파일 업로드

4-2. 복호화 요청

  1. 암호화된 파일을 복호화 요청으로 전달합니다.
    • URL: /file/decrypt
    • Method: POST
    • Body: 파일 업로드

4-3. 결과 확인

  • 암호화된 파일은 원본과 달리 변환된 데이터를 포함합니다.
  • 복호화된 파일은 원본 데이터와 동일합니다.

5. 보안 고려사항

  1. 암호화 키 보호
    • 키를 코드에 하드코딩하지 말고 환경 변수나 별도의 키 관리 시스템(KMS)을 사용하세요.
  2. 암호화 알고리즘 선택
    • AES-256과 같은 강력한 암호화를 사용하는 것이 좋습니다.
  3. 파일 접근 제한
    • 파일에 대한 접근 권한을 제한하여 무단 접근을 방지하세요.
  4. 로그 처리
    • 로그에 민감한 정보(암호화 키, 데이터 등)를 기록하지 않도록 유의하세요.

6. 마무리

오늘은 전자정부프레임워크의 암호화 도구를 활용하여 파일 암호화 및 복호화를 구현해 보았습니다. 데이터를 안전하게 보호하는 기술을 통해 애플리케이션의 보안 수준을 높일 수 있습니다.
다음 학습에서는 암호화와 복호화 기술을 데이터베이스와 통합하는 방법을 다룰 예정입니다.

반응형

+ Recent posts