728x90
1. install pycrytpodome
pip install pycryptodome
2. python class
import base64
from Crypto import Random
from Crypto.Cipher import AES
class AES256():
def __init__(self, key):
self.bs = 128
self.key = key.encode('utf-8')
self.key = AES256.str_to_bytes(key)
@staticmethod
def str_to_bytes(data):
u_type = type(b''.decode('utf8'))
if isinstance(data, u_type):
return data.encode('utf8')
return data
def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * AES256.str_to_bytes(chr(self.bs - len(s) % self.bs))
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
def encrypt(self, raw):
raw = self._pad(AES256.str_to_bytes(raw))
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8')
def decrypt(self, enc):
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
java 암복호화
AES256/ base64 암호화 with java (tistory.com)
728x90
'개발 > python, python frameworks' 카테고리의 다른 글
PyInstaller shell 없애기 (0) | 2020.04.21 |
---|---|
python(flask)에서 DTO 활용하기(예제) (0) | 2020.04.03 |
카카오 로그인 (python에 붙이기) (0) | 2020.03.23 |
python flask - blueprint (0) | 2020.03.12 |
댓글