본문 바로가기
개발/python, python frameworks

AES256/ base64 암호화 with python

by 개발자종혁 2020. 3. 30.
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)

 

AES256/ base64 암호화 with java

AES_Encryption.java import java.io.ByteArrayOutputStream; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; import java.util.Arrays; import java.util.Base64; impor..

jonghyeok-dev.tistory.com

 

728x90

댓글