취약하거나 위험한 암호화 알고리즘 사용

취약한 암호화 알고리즘과 안전하지 않은 암호 모드

설명

DES는 키가 짧고 DES·3DES는 블록이 64비트로 작아 대량의 데이터를 처리할 때 충돌 위험이 커집니다. RC2·RC4·IDEA 같은 구형 암호도 교체하세요. ECB는 반복되는 평문 블록을 드러내고, MD5·SHA-1은 충돌에 취약합니다.

잠재적 영향

암호화한 데이터가 노출되거나 위조된 서명이 검증을 통과할 수 있습니다. 빠른 해시로 저장한 비밀번호는 추측하기 쉽습니다. 적용 대상인 NIST나 PCI DSS의 암호 요건을 충족하지 못할 수도 있습니다.

해결 방법

  • 암호화에는 AES-128 이상과 GCM 또는 CCM을 사용하세요. CBC를 유지해야 한다면 별도 키로 IV와 암호문을 함께 인증하고 복호화 전에 태그를 검증하세요. IV·nonce·솔트는 SecureRandom으로 생성하고 같은 키에서 IV나 nonce를 재사용하지 마세요.
  • 충돌 내성이 필요한 해시에는 SHA-256 이상을 사용하세요. SHA1withRSA와 MD5withRSA 서명은 2048비트 이상 키의 SHA256withRSA 또는 P-256 이상의 곡선을 사용하는 ECDSA로 교체하세요.
  • 비밀번호는 개별 솔트와 충분한 비용을 적용한 Argon2id, scrypt, bcrypt 또는 PBKDF2로 저장하세요. PBKDF2-HMAC-SHA256은 600,000회 이상으로 설정하고 서버에서 처리 시간을 확인하세요.
  • 코드와 설정에서 허용할 알고리즘과 키 길이를 제한하세요. 키를 안전하게 보관·교체하고 필요하면 KMS나 HSM을 사용하세요. 라이브러리와 공급자도 최신 상태로 유지하세요.

예시

변경 전

java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;

public class LegacyCrypto {
    // Insecure: ECB exposes plaintext patterns
    public byte[] encryptEcb(byte[] plaintext, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(plaintext);
    }

    // Insecure: MD5 is collision-prone and fast to brute-force
    public byte[] weakDigest(byte[] data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(data);
    }

    // Insecure: SHA1-based signature is deprecated
    public byte[] signSha1(byte[] data, PrivateKey privKey) throws Exception {
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(privKey);
        sig.update(data);
        return sig.sign();
    }
}

변경 후

java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import java.nio.ByteBuffer;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.util.Base64;

public class ModernCrypto {
    // Secure: AES-GCM (AEAD) with random 96-bit nonce, 128-bit tag
    public byte[] encryptGcm(byte[] plaintext, SecretKey key) throws Exception {
        byte[] nonce = new byte[12];
        SecureRandom.getInstanceStrong().nextBytes(nonce);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec spec = new GCMParameterSpec(128, nonce);
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] ct = cipher.doFinal(plaintext);
        return ByteBuffer.allocate(nonce.length + ct.length).put(nonce).put(ct).array();
    }

    // Secure password hashing with PBKDF2 (salt + high iteration)
    public String hashPassword(char[] password) throws Exception {
        byte[] salt = new byte[16];
        SecureRandom.getInstanceStrong().nextBytes(salt);
        PBEKeySpec spec = new PBEKeySpec(password, salt, 600_000, 256);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        byte[] hash = skf.generateSecret(spec).getEncoded();
        return Base64.getEncoder().encodeToString(salt) + ":" + Base64.getEncoder().encodeToString(hash);
    }

    // Strong signature with SHA-256
    public byte[] signStrong(byte[] data, PrivateKey privKey) throws Exception {
        Signature sig = Signature.getInstance("SHA256withRSA");
        sig.initSign(privKey);
        sig.update(data);
        return sig.sign();
    }
}

예시는 getInstanceStrong으로 생성한 12바이트 nonce를 태그가 포함된 GCM 암호문 앞에 붙입니다. 128비트 태그는 변조를 검출하며 재전송 방지는 프로토콜에서 처리해야 합니다. 비밀번호는 16바이트 솔트와 PBKDF2 해시를 함께 저장할 값으로 반환하고, 서명에는 SHA256withRSA를 사용합니다.

참조