현대 암호학

[현대 암호학] 05. 실습(3)

Uggjjini 2021. 6. 24. 11:04

[실습] 3DES(TDES) 사용하여 파일에 암복호화 저장하기

plaintext      -> E(msg)                          -> ciphertext

                     3DES|AES-CBC(iv)

                     3DES|AES-CTR(nonce)

                     3DES|AES-EAX(nonce, tag)

 

File1 -> plaintext -> E(msg) -> ciphertext -> File2

3DES|AES-CBC(iv)

3DES|AES-CTR(nonce)

3DES|AES-EAX(nonce, tag)

 

사용 시스템

kali

 

 

3DES 사용하여 파일에 암호화 하여 저장하고 복호화 하기

 

1) 준비사항(plain.txt 파일 준비)

 

2) 프로그램 작성

 

3DES-CBC

  • iv = get_random_bytes(8)
  • key = get_random_bytes(24)
  • pad, unpad

 

DES3FromFile.py

###!/usr/bin/python3
#
# DES3-CBC
# 1) DES3 Cipher Algorithm
#   * input : 8 bytes
#   * key   : 8 bytes(8|16|24)
#   * output: 8 bytes
# 2) CBC Cipher Mode
#   * 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_CBC:
    # argument:
    #
    # property:
    #   * self.iv:
    #   * self.key:
    # function:
    #   * enc()
    #       pfile -> Encryption -> cfile
    #   * dec()
    #       cfile -> Decryption -> pfile
    def __init__(self):
        self.key = get_random_bytes(24)
        self.iv = get_random_bytes(8)

    def enc(self, pfile):
        cfile = pfile + '.enc'

        fd1 = open(pfile, 'rb')
        fd2 = open(cfile, 'wb')
        plaintext = fd1.read()

        cipher1 = DES3.new(self.key, DES3.MODE_CBC, self.iv)
        ciphertext = cipher1.encrypt(pad(plaintext, 8))
        fd2.write(ciphertext)

        fd1.close()
        fd2.close()

        return cfile

    def dec(self, cfile):
        pfile = cfile + '.dec'

        fd1 = open(cfile, 'rb')
        ciphertext = fd1.read()

        cipher2 = DES3.new(self.key, DES3.MODE_CBC, self.iv)
        plaintext = unpad(cipher2.decrypt(ciphertext), 8)

        fd2 = open(pfile, 'wb')
        fd2.write(plaintext)
        fd2.close()

        return pfile


def main():
    myfile = 'plain'

    print("Original File(%s) Content:" % myfile)
    print(open(myfile).read())
    print('')

    des3 = MyDES3_CBC()

    encfile = des3.enc(myfile)
    print("Encrypted File(%s) Content:" % encfile)
    print(open(encfile, 'rb').read())
    print('')

    decfile = des3.dec(encfile)
    print("Decrypted File(%s) Content:" % decfile)
    print(open(decfile).read())


if __name__ == '__main__':
    main()

 

Original File Content:
Robin Monroe (Anne Heche) is a New York City journalist who works for Dazzle .....
 
Encrypted File Content:
b'\xb5\xb1\xcat\xda\xfa\xfb\xb7\x93\x007"\xb7\x87\t)\xeb$\x9cJT\xeei\xaf#jr\ .....
 
Decrypted File Content
Robin Monroe (Anne Heche) is a New York City journalist who works .....

 


[실습] AES 사용하여 파일에 암복호화 저장하기

 

사용 시스템

kali

 

AES 파일에 암호화 하여 저장하고 복호화 하기

1) 준비 사항 : 파일이 준비 되어 있어야 한다.(plain.txt )

 

AESFromFile.py

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


class MyAES_CBC:
    # argument:
    #
    # property:
    #   * self.iv:
    #   * self.key:
    # function:
    #   * enc()
    #       pfile -> Encryption -> cfile
    #   * dec()
    #       cfile -> Decryption -> pfile
    def __init__(self):
        self.key = get_random_bytes(16)
        self.iv = get_random_bytes(16)

    def enc(self, pfile):
        cfile = pfile + '.enc'

        fd1 = open(pfile, 'rb')
        fd2 = open(cfile, 'wb')
        plaintext = fd1.read()

        cipher1 = AES.new(self.key, AES.MODE_CBC, self.iv)
        ciphertext = cipher1.encrypt(pad(plaintext, 16))
        fd2.write(ciphertext)

        fd1.close()
        fd2.close()

        return cfile

    def dec(self, cfile):
        pfile = cfile + '.dec'

        fd1 = open(cfile, 'rb')
        ciphertext = fd1.read()
        fd1.close()

        cipher2 = AES.new(self.key, AES.MODE_CBC, self.iv)
        plaintext = unpad(cipher2.decrypt(ciphertext), 16)

        fd2 = open(pfile, 'wb')
        fd2.write(plaintext)
        fd2.close()

        return pfile


def main():
    myfile = 'plain'

    print("Original File(%s) Content:" % myfile)
    print(open(myfile).read())
    print('')

    aes = MyAES_CBC()

    encfile = aes.enc(myfile)
    print("Encrypted File(%s) Content:" % encfile)
    print(open(encfile, 'rb').read())
    print('')

    decfile = aes.dec(encfile)
    print("Decrypted File(%s) Content:" % decfile)
    print(open(decfile).read())


if __name__ == '__main__':
    main()

 

Original File Content:
Robin Monroe (Anne Heche) is a New York City journalist who works for Dazzle .....
 
Encrypted File Content:
b'@\x85\xa4\x8b\xaa\x84\xfe\xaa\xcbg\xfc\x9d\x18\x8d@\xc8\x87\x0c@.....
 
Decrypted File Content
Robin Monroe (Anne Heche) is a New York City journalist who works .....

 

 

 

'현대 암호학' 카테고리의 다른 글

[현대 암호학] 05. 실습(5)  (0) 2021.06.24
[현대 암호학] 05. 실습(4)  (0) 2021.06.24
[현대 암호학] 05. 실습(2)  (0) 2021.06.23
[현대 암호학] 05. 실습(1)  (0) 2021.06.23
[현대 암호학] 05-5. CTR 모드  (0) 2021.06.23