Study Record

[암호 프로그래밍] 3DES-CBC 암호화 / 복호화 프로그램 본문

암호/프로그래밍

[암호 프로그래밍] 3DES-CBC 암호화 / 복호화 프로그램

초코초코초코 2021. 12. 2. 17:20
728x90

- API 참고

# TDES-CBC
# Block Cipher : DES3
# input     : 8 bytes
# output    : 8 bytes
# key       : 8 bytes x 3 = 24 bytes(k1, k2, k3)
# Block Cipher Mode : CBC
# IV        : 8 bytes
# pad, unpad

from Crypto.Cipher import DES3
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad


class MyDES3:
    # variable : msg, key, iv
    # function : encrypt, decrypt

    def __init__(self, key, iv):
        self.key = key
        self.iv = iv

    def enc(self, msg):
        return DES3.new(self.key, DES3.MODE_CBC, self.iv).encrypt(pad(msg.encode(), 8))

    def dec(self, encMsg):
        return unpad(DES3.new(self.key, DES3.MODE_CBC, self.iv).decrypt(encMsg), 8).decode()


"""
plaintext = "We are no longer the knights who say ni!"
key = get_random_bytes(24)
iv = get_random_bytes(8)

des = MyDES3(key, iv)
encMsg = des.enc(plaintext)
decMsg = des.dec(encMsg)

print(plaintext)
print(encMsg)
print(decMsg)
"""

 

728x90