Created
January 18, 2013 10:36
-
-
Save delicb/4563744 to your computer and use it in GitHub Desktop.
IronPython AES encrypt and decrypt
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
# NOTE: Change this to random numbers in range (0, 255) inclusive | |
_key = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
_vector = [0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0] | |
from System import Array, Byte | |
from System.Text import UTF8Encoding | |
from System.IO import MemoryStream | |
from System.Security.Cryptography import RijndaelManaged, CryptoStream, CryptoStreamMode | |
_key = Array[Byte](_key) | |
_vector = Array[Byte](_vector) | |
def encrypt(text, digest=True): | |
''' | |
Performs crypting of provided text using AES algorithm. | |
If 'digest' is True hex_digest will be returned, otherwise bytes of encrypted | |
data will be returned. | |
This function is simetrical with decrypt function. | |
''' | |
utfEncoder = UTF8Encoding() | |
bytes = utfEncoder.GetBytes(text) | |
rm = RijndaelManaged() | |
enc_transform = rm.CreateEncryptor(_key, _vector) | |
mem = MemoryStream() | |
cs = CryptoStream(mem, enc_transform, CryptoStreamMode.Write) | |
cs.Write(bytes, 0, len(bytes)) | |
cs.FlushFinalBlock() | |
mem.Position = 0 | |
encrypted = Array.CreateInstance(Byte, mem.Length) | |
mem.Read(encrypted, 0, mem.Length) | |
cs.Close() | |
l = map(int, encrypted) | |
return _to_hex_digest(l) if digest else _to_bytes(l) | |
if digest: | |
return _to_hex_digest(map(int, encrypted)) | |
else: | |
return _to_bytes(map(int, encrypted)) | |
def decrypt(data, digest=True): | |
''' | |
Performs decrypting of provided encrypted data. | |
If 'digest' is True data must be hex digest, otherwise data should be | |
encrtypted bytes. | |
This function is simetrical with encrypt function. | |
''' | |
data = Array[Byte](map(Byte, _from_hex_digest(data) if digest else _from_bytes(data))) | |
rm = RijndaelManaged() | |
dec_transform = rm.CreateDecryptor(_key, _vector) | |
mem = MemoryStream() | |
cs = CryptoStream(mem, dec_transform, CryptoStreamMode.Write) | |
cs.Write(data, 0, data.Length) | |
cs.FlushFinalBlock() | |
mem.Position = 0 | |
decrypted = Array.CreateInstance(Byte, mem.Length) | |
mem.Read(decrypted, 0, decrypted.Length) | |
cs.Close() | |
utfEncoder = UTF8Encoding() | |
return utfEncoder.GetString(decrypted) | |
def _to_bytes(lst): | |
return ''.join(map(chr, lst)) | |
def _from_bytes(bts): | |
return [ord(b) for b in bts] | |
def _to_hex_digest(encrypted): | |
return ''.join(map(lambda x: '%02x' % x, encrypted)) | |
def _from_hex_digest(digest): | |
return [int(digest[x:x+2], 16) for x in xrange(0, len(digest), 2)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment