Hello world. Today i am gonna show you how to encryption and descryption string with AES. Today i've task to create link odoo with encryption, so automatically i must create decryption too. On this case i just use AES for encryption and decryption data, cause i think AES is the best algorithm encryption for now.
Here we go, this is my python function to encryption and decryption data. You can check it, if get wrong with my code. Lol
def _default_key(self): default_key = b'no surprises guy' return default_key def _key(self, key): if len(key) >= 32: key = key[:32] else: key = self._pad(key) return key def bs(self): bs = 32 return bs def _encrypt(self, raw, key): raw = self._pad(raw) iv = Random.new().read(AES.block_size) cipher = AES.new(key, AES.MODE_CBC, iv) return base64.b64encode(iv + cipher.encrypt(raw)) def _decrypt(self, enc, key): enc = base64.b64decode(enc) iv = enc[:AES.block_size] cipher = AES.new(key, AES.MODE_CBC, iv) return self._unpad(cipher.decrypt(enc[AES.block_size:])) def _pad(self, s): s = str(s) return s + (self.bs() - len(s) % self.bs()) * chr(self.bs() - len(s) % self.bs()) def _unpad(self, s): s = str(s) return s[:-ord(s[len(s)-1:])]
Yes, to use this function you just call function _encrypt and _decrypt with the parameter like this.
self._encrypt("Dedy Yuristiawan", self.key(self._default_key)) # parameter string and key self._decrypt(encrypted_data, self.key(self._default_key)) #parameter data encrypted and key
and for now don't forget to import library python on the top.
from Crypto.Cipher import AES from Crypto import Random import base64 from gdata.tlslite.utils.rijndael import encrypt
So the conclusion is use this function if you want to encrypt or decrypt data, if you confuse about my code, you can fill on the comment bellow, i hope this is can help you little bit.
Great article! Thanks for sharing this informative article. Looking for the best Application Development Company in Australia? Reach IDYA Technology.
ReplyDelete