Skip to content

Instantly share code, notes, and snippets.

@mrroot5
Forked from swinton/AESCipher.py
Created June 5, 2018 12:48
Show Gist options
  • Select an option

  • Save mrroot5/c35c4f746a33ba117e86c651e76c567c to your computer and use it in GitHub Desktop.

Select an option

Save mrroot5/c35c4f746a33ba117e86c651e76c567c to your computer and use it in GitHub Desktop.

Revisions

  1. @swinton swinton revised this gist Jan 13, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    pycrypto==2.6.1
  2. @swinton swinton created this gist Jan 13, 2014.
    35 changes: 35 additions & 0 deletions AESCipher.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/usr/bin/env python

    import base64

    from Crypto import Random
    from Crypto.Cipher import AES

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


    class AESCipher:

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

    def encrypt( self, raw ):
    raw = pad(raw)
    iv = Random.new().read( AES.block_size )
    cipher = AES.new( self.key, AES.MODE_CBC, iv )
    return base64.b64encode( iv + cipher.encrypt( raw ) )

    def decrypt( self, enc ):
    enc = base64.b64decode(enc)
    iv = enc[:16]
    cipher = AES.new(self.key, AES.MODE_CBC, iv )
    return unpad(cipher.decrypt( enc[16:] ))


    cipher = AESCipher('mysecretpassword')
    encrypted = cipher.encrypt('Secret Message A')
    decrypted = cipher.decrypt(encrypted)
    print encrypted
    print decrypted