대칭키 암호화 (Symmetric Encryption)

대칭키 암호화는 동일한 키를 사용하여 데이터를 암호화하고 복호화하는 방식.

구현 예시:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from cryptography.fernet import Fernet

class SymmetricEncryption:
    def __init__(self):
        # 대칭키 생성
        self.key = Fernet.generate_key()
        self.cipher_suite = Fernet(self.key)
        
    def encrypt(self, data):
        """데이터 암호화"""
        if isinstance(data, str):
            data = data.encode()
        return self.cipher_suite.encrypt(data)
        
    def decrypt(self, encrypted_data):
        """데이터 복호화"""
        decrypted_data = self.cipher_suite.decrypt(encrypted_data)
        return decrypted_data.decode()

# 사용 예시
encryptor = SymmetricEncryption()
message = "Hello, World!"
encrypted = encryptor.encrypt(message)
decrypted = encryptor.decrypt(encrypted)

주요 특징

혼돈(confusion)과 확산(diffusion)의 원리

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def confusion_example(input_data, key):
    # 혼돈(Confusion) 예시: XOR 연산을 사용하여 입력 데이터와 키를 결합
    # 각 문자를 키와 XOR 연산하여 암호화
    return [chr(ord(char) ^ key) for char in input_data]

def diffusion_example(input_data):
    # 확산(Diffusion) 예시: 간단한 순열 연산을 사용
    # 입력 데이터를 뒤집어 확산 효과를 시뮬레이션
    return input_data[::-1]

# 예시 데이터와 키
input_data = "HELLO"
key = 3

# 혼돈 적용
confused_data = confusion_example(input_data, key)

# 확산 적용
diffused_data = diffusion_example(confused_data)

# 결과 출력
print("원본 데이터:", input_data)
print("혼돈 적용 후:", ''.join(confused_data))
print("확산 적용 후:", ''.join(diffused_data))

라운드 반복 구조

치환과 순열 연산을 포함한 기본 구조(라운드)를 여러 번 반복하는 방식.

특징

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def simple_round_function(data, key):
    # 간단한 라운드 함수: 데이터와 키를 XOR 연산
    return [d ^ k for d, k in zip(data, key)]

# 초기 데이터와 키 (간단한 정수 리스트로 표현)
data = [1, 2, 3, 4]
key = [4, 3, 2, 1]

# 라운드 수
rounds = 3

# 라운드 반복 수행
for _ in range(rounds):
    data = simple_round_function(data, key)

# 라운드 후 결과
result = data

블록 암호와 스트림 암호

장점

  1. 빠른 처리 속도: 공개키 암호화 방식에 비해 암호화와 복호화 속도가 빠르다.
  2. 효율성: 대용량 데이터 처리에 적합하다.
  3. 구현의 용이성: 알고리즘 구조가 상대적으로 단순하여 구현이 쉽다.
  4. 적은 자원 사용: 연산 자원이 적게 소모되어 저전력 환경이나 IoT 장치에서도 사용 가능하다.

단점

  1. 키 분배 문제: 안전한 키 전달과 관리가 어렵다.
  2. 확장성 문제: 다수의 사용자 간 통신 시 관리해야 할 키의 수가 급증한다.
  3. 키의 안전성 의존: 키가 노출되면 모든 암호화된 데이터가 위험에 처할 수 있다.
  4. 디지털 서명 적용의 어려움: 비대칭키 방식에 비해 디지털 서명 기법 적용이 어렵다.

사용 사례

  1. 대용량 데이터 암호화: 효율성이 중요한 경우에 사용된다.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class FileEncryption:
    def __init__(self, key):
        self.cipher = AES.new(key, AES.MODE_CBC)
        
    def encrypt_file(self, input_file, output_file):
        with open(input_file, 'rb') as f:
            data = f.read()
        
        encrypted_data = self.cipher.encrypt(self._pad(data))
        
        with open(output_file, 'wb') as f:
            f.write(self.cipher.iv)
            f.write(encrypted_data)
  1. 폐쇄된 시스템 내 내부 통신 보호.
  2. 디스크 전체 암호화: Windows의 BitLocker, OS X의 FileVault 등.
  3. 통신 채널 보호: TLS 프로토콜에서 데이터 암호화에 사용된다.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class SecureChannel:
    def __init__(self):
        self.session_key = get_random_bytes(32)
        self.cipher = AES.new(self.session_key, AES.MODE_CBC)
        
    def send_message(self, message):
        """암호화된 메시지 전송"""
        encrypted = self.cipher.encrypt(self._pad(message.encode()))
        return self.cipher.iv + encrypted
        
    def receive_message(self, encrypted_message):
        """암호화된 메시지 수신 및 복호화"""
        iv = encrypted_message[:16]
        cipher = AES.new(self.session_key, AES.MODE_CBC, iv)
        decrypted = cipher.decrypt(encrypted_message[16:])
        return self._unpad(decrypted).decode()
  1. 실시간 데이터 처리: 라이브 동영상 스트리밍 서비스 등.

참고 및 출처