Last active
November 8, 2024 18:31
-
-
Save swinton/8409454 to your computer and use it in GitHub Desktop.
Encrypt & Decrypt using PyCrypto AES 256 From http://stackoverflow.com/a/12525165/119849
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pycrypto==2.6.1 |
i can't seem to find how to do aes256
Please provide the JAVA code equivalent to above which is in python.
i think this is aes 128, we have a standard blocksize of 16 bytes (128bit)
Key size decides if it is aes 256 or 128, block size is 16 in both of em
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i think this is aes 128, we have a standard blocksize of 16 bytes (128bit)