[java] Java Cucumber에서 스텝 입력 정보 암호화하기

Java Cucumber는 행동 주도 개발(Behavior Driven Development, BDD)을 지원하는 테스트 프레임워크입니다. Cucumber를 사용하면 개발자와 비개발자들이 함께 테스트 시나리오를 작성하고 실행할 수 있습니다.

보안을 고려하여 Cucumber 스텝에 입력되는 정보를 암호화하려는 경우, Java를 사용하여 간단한 방법으로 구현할 수 있습니다. 아래의 가이드를 따라 진행해보세요.

1. 암호화 클래스 생성

먼저, 암호화 클래스를 생성해야 합니다. 다음은 암호화를 수행하는 EncryptionUtils 클래스의 예시입니다.

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import java.util.Base64;

public class EncryptionUtils {

    private static final String ENCRYPTION_ALGORITHM = "AES";
    private static final String SECRET_KEY_SPEC = "AES/ECB/PKCS5Padding";
    private static final String SECRET_KEY = "your_secret_key";
    private static final String SALT = "your_salt";

    public static String encrypt(String input) throws Exception {
        byte[] salt = SALT.getBytes();
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), salt, 65536, 256);
        SecretKey secretKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), ENCRYPTION_ALGORITHM);
        Cipher cipher = Cipher.getInstance(SECRET_KEY_SPEC);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(input.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedInput) throws Exception {
        byte[] salt = SALT.getBytes();
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), salt, 65536, 256);
        SecretKey secretKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), ENCRYPTION_ALGORITHM);
        Cipher cipher = Cipher.getInstance(SECRET_KEY_SPEC);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedInput);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes);
    }
}

위 예시 코드는 AES 알고리즘을 사용하여 입력 정보를 암호화하고 복호화하는 encryptdecrypt 메서드를 포함하는 EncryptionUtils 클래스입니다. SECRET_KEYSALT 값을 설정해야 합니다. 실제 사용 시에는 이 값을 적절히 변경하여 사용해야 합니다.

2. Cucumber 스텝에서 암호화 적용

이제 Cucumber 스텝에서 입력 정보를 암호화해야 합니다. 다음은 ‘Given’ 스텝에서 암호화를 적용하는 예시입니다.

import io.cucumber.java.en.Given;

public class StepDefinitions {

    @Given("I enter encrypted credentials")
    public void enterEncryptedCredentials() throws Exception {
        String username = "myusername";
        String password = "mypassword";
        
        // 암호화 적용
        String encryptedUsername = EncryptionUtils.encrypt(username);
        String encryptedPassword = EncryptionUtils.encrypt(password);
        
        // 사용된 정보 출력
        System.out.println("Encrypted username: " + encryptedUsername);
        System.out.println("Encrypted password: " + encryptedPassword);
        
        // 로직 작성
        // ...
    }
    
    // ...
}

위의 예시 코드에서는 EncryptionUtils.encrypt 메서드를 사용하여 사용자 이름과 비밀번호를 암호화합니다. 그리고 암호화된 사용자 이름과 비밀번호를 출력하여 확인할 수 있습니다.

결론

Java Cucumber에서 스텝 입력 정보를 암호화하는 방법을 알아보았습니다. EncryptionUtils 클래스를 사용하여 암호화와 복호화를 수행하고, Cucumber 스텝에서 암호화된 정보를 사용하는 방법을 살펴보았습니다. 암호화를 적용하여 민감한 정보를 보호할 수 있고, 테스트 시나리오를 안전하게 실행할 수 있습니다.

참고 자료: