Skip to content

Instantly share code, notes, and snippets.

@a7t0fwa7
Forked from wowkin2/aes_example_in_python.py
Created July 28, 2022 08:39
Show Gist options
  • Save a7t0fwa7/76c485cf2b771f83c7b29266dc31d4c1 to your computer and use it in GitHub Desktop.
Save a7t0fwa7/76c485cf2b771f83c7b29266dc31d4c1 to your computer and use it in GitHub Desktop.

Revisions

  1. @dokenzy dokenzy revised this gist Apr 12, 2015. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions aes_example_in_python.py
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,8 @@ class AESCipher(object):
    """

    def __init__(self, key):
    self.key = hashlib.sha256(key.encode()).digest()
    self.key = key
    #self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, message):
    """
    @@ -44,7 +45,7 @@ def encrypt(self, message):
    raw = pad(message)
    cipher = AES.new(self.key, AES.MODE_CBC, iv())
    enc = cipher.encrypt(raw)
    return base64.b64encode(enc)
    return base64.b64encode(enc).decode('utf-8')

    def decrypt(self, enc):
    enc = base64.b64decode(enc)
    @@ -56,13 +57,14 @@ def decrypt(self, enc):
    key = 'abcdefghijklmnopqrstuvwxyz123456'

    message = '한글을 테스트 합니다.'
    enc = 'gOXlygE+qxS+69zN5qC6eKJvMiEoDQtdoJb3zjT8f/E='
    _enc = 'gOXlygE+qxS+69zN5qC6eKJvMiEoDQtdoJb3zjT8f/E='
    #message = 'imcore.net'
    #enc = 'kWyuTmUELRiREWIPpLy3ZA=='
    #message = 'Test English...'
    #answer = 'rvs4H8x4Q8sblGG1jkOHFQ=='

    enc = AESCipher(key).encrypt(message)
    dec = AESCipher(key).decrypt(enc)
    dec = AESCipher(key).decrypt(_enc)

    print(message == dec)
    print(_enc == enc)
    print(message == dec)
  2. @dokenzy dokenzy renamed this gist Apr 8, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @dokenzy dokenzy revised this gist Apr 8, 2015. No changes.
  4. @dokenzy dokenzy created this gist Apr 8, 2015.
    68 changes: 68 additions & 0 deletions aes_example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #-*- coding: utf-8 -*-

    # Python 3.4
    # author: http://blog.dokenzy.com/
    # date: 2015. 4. 8

    # References
    # http://www.imcore.net/encrypt-decrypt-aes256-c-objective-ios-iphone-ipad-php-java-android-perl-javascript/
    # http://stackoverflow.com/questions/12562021/aes-decryption-padding-with-pkcs5-python
    # http://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256
    # http://www.di-mgt.com.au/cryptopad.html
    # https://github.com/dlitz/pycrypto

    import base64
    import hashlib
    from Crypto import Random
    from Crypto.Cipher import AES

    BS = 16
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode()
    unpad = lambda s: s[:-ord(s[len(s)-1:])]

    def iv():
    """
    The initialization vector to use for encryption or decryption.
    It is ignored for MODE_ECB and MODE_CTR.
    """
    return chr(0) * 16

    class AESCipher(object):
    """
    https://github.com/dlitz/pycrypto
    """

    def __init__(self, key):
    self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, message):
    """
    It is assumed that you use Python 3.0+
    , so plaintext's type must be str type(== unicode).
    """
    message = message.encode()
    raw = pad(message)
    cipher = AES.new(self.key, AES.MODE_CBC, iv())
    enc = cipher.encrypt(raw)
    return base64.b64encode(enc)

    def decrypt(self, enc):
    enc = base64.b64decode(enc)
    cipher = AES.new(self.key, AES.MODE_CBC, iv())
    dec = cipher.decrypt(enc)
    return unpad(dec).decode('utf-8')


    key = 'abcdefghijklmnopqrstuvwxyz123456'

    message = '한글을 테스트 합니다.'
    enc = 'gOXlygE+qxS+69zN5qC6eKJvMiEoDQtdoJb3zjT8f/E='
    #message = 'imcore.net'
    #enc = 'kWyuTmUELRiREWIPpLy3ZA=='
    #message = 'Test English...'
    #answer = 'rvs4H8x4Q8sblGG1jkOHFQ=='

    enc = AESCipher(key).encrypt(message)
    dec = AESCipher(key).decrypt(enc)

    print(message == dec)