현대 암호학

[현대 암호학] 03. 실습(1)

Uggjjini 2021. 6. 21. 14:38

[실습] 시저암호(Caesar Cipher) 만들기

# 카이사르 암호화/복호화 - 1st version
#
# (1) 입력 : messages (Input: This is my secret plaintext.)
# (2) 출력 : translated (Ouput: guv6Jv6Jz! J6rp5r7Jzr66ntrM)
# (3) 기능 :
# * plaintext : This is my secret plaintext.
# * 암호화/복호화 키 : 13
# * mode : encrypt | decrypt
# * symbols : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !?.'
  • C = (P + K) % 26
  • P = (P - K) % 26
  • C = (P + K) % len(symbols)
  • P = (C - K) % len(symbols)
암호시스템 : 시저 암호(C = (P + K) % len(symbols), P = (C - K) % len(symbols))
암호 키 : 13
심볼 : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !?."
입력 평문 : "This is my secret plaintext."
출력 암호문: ?
 
 암호화

'T' => symbols.index('T') + 13  => symbols[34]='g'
          ------------------
                   21           + 13 = 34

 복호화

'g' => symbols.index('g') - 13 => symbols[21]='T'
          ------------------
                 34             - 13 = 21

 

caesarCipher.py

import sys

symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !?.'


def main():
    # input : str(mymsg), int((key(13)), str(mode=encrypt|decrypt)2
    # output: str(myencrypted), str(mydecrypted)
    # function:
    #   * C = (P + K) mod len(symbols)
    #   * P = (C - K) mod len(symbols)
    mymsg = 'This is my secret plaintext.'
    mykey = 13
    print('My messages : |%s| ' %mymsg)

    mytranslated = caesarCipher('encrypt', mymsg, mykey)
    print('My Encrypted: |%s|' %mytranslated)

    mytranslated = caesarCipher('decrypt', mytranslated, mykey)
    print('My Decrypted: |%s|' %mytranslated)


def caesarCipher(m, msg, k):
    # input : str(m), str(msg), int(k)
    # output: translated
    # function: Caesar Cipher
    #   * C = (P + K) % len(symbols)
    #   * P = (C - K) % len(symbols)
    translated = ''
    for c in msg:
        c_index = symbols.find(c)
        if c in symbols:
            if m == 'encrypt':
                translated_index = (c_index + k) % len(symbols)
                translated += symbols[translated_index]
            elif m == 'decrypt':
                translated_index = (c_index - k) % len(symbols)
                translated += symbols[translated_index]
            else:
                sys.exit('Error: mode must be "encrypt|decrypt".')
        else:
            translated += c

    return translated


if __name__ == '__main__':
    main()

 

 


[실습] 시저 암호 공격(Caesar Cipher Attack)

시저(Caesar) 암호 공격 프로그램 만들기

  • 무차별 대입법(Brute Force Attack)으로 시저(caesar) 암호 크랙 하기
  • P = (C - K) % 26
  • P = (C - K) % len(symbols)

 

# 1. 카이사르 암호 크래킹 - 1st version
#
# (1) 입력 : 메세지 (Input: guv6Jv6Jz!J6rp5r7Jzr66ntrM)
# (2) 출력 : 카이사르 암호화 후 메세지 (Ouput: )
# (3) 기능 :
# * 카이사르 암호화된 메세지를 입력으로 받아
# * burute force 공격을 진행하는 프로그램

 

이전에 만들었던 caesarCipher.py 파일의 caesarCipher()를 사용해도 된다.

 

() 모듈 파일을 import 하는 예

import caesarCipher

decrypted = caesarCipher.caesarCipher('decrypt', mymsg, key)

 

또는 caesarCipher.py 파일의 caesarCipher() 함수를 복사해서 붙이고 작업해도 될것이다.

 

caesarCipherHack.py

messages = 'guv6Jv6Jz!J6rp5r7J3ynv17r 7M'
symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'

def main():
    for key in range(len(symbols)):
        translated = ''
        for c in messages:
            c_index = symbols.find(c)
            if c in symbols:
                translated_index = (c_index - key) % len(symbols)
                translated += symbols[translated_index]
            else:
                translated += c
        print("key %d:%s - [%s]" % (key, symbols[key], translated));

if __name__ == '__main__':