def test_AES(self): cipher = AESCipher() string = "The quick brown fox jumps over the lazy dog." ct = cipher.encrypt(string) self.assertNotEqual(ct, string) pt = cipher.decrypt(ct) self.assertEqual(pt, string)
class EncryptedCharField(models.CharField): cipher = AESCipher() __metaclass__ = models.SubfieldBase def to_python(self, value): if value is None: return value if value.startswith("$AES$"): return self.cipher.decrypt(value[len("$AES$"):]) # Warning: This strips all unicode chars. This is a hack because I # can not figure out how to get the validation to work right... return value.encode('ascii', 'ignore') def get_prep_value(self, value): return "$AES$" + self.cipher.encrypt(value)
def test_AES_error(self): cipher = AESCipher() string = "The quick brown fox jumps over the lazy dog." ct = cipher.encrypt(string)[:10] + "randomgarbage" self.assertRaises(TypeError, cipher.decrypt, ct)