[Python] I made a function that decrypts AES encryption just by throwing it with pycrypto.

What you can do -When executed with plaintext, password, and vector as arguments, data encrypted in base64 format is returned. -Similarly, if the plaintext is replaced with the data encrypted in the base64 format and executed, the plaintext is returned.

AES encryption conditions The key must be 16 bytes, 24 bytes, or 32 bytes long. Plaintext must be a multiple of 16 bytes. Use iv to make it harder to guess. Regarding iv, I think it is appropriate to use the same one for one application.

I think it's okay to copy and use it, but I can't guarantee that the code is correct because I don't fully understand AES. If you have any vulnerabilities, please let us know in the comments.

Installation is

$ pip install pycrypto

Encryption

get_encrypt_data.py



from Crypto.Cipher import AES
import hashlib
import base64

def get_encrypt_data(raw_data, key, iv):
    raw_data_base64 = base64.b64encode(raw_data)
    # 16byte
    if len(raw_data_base64) % 16 != 0:
        raw_data_base64_16byte = raw_data_base64
        for i in range(16 - (len(raw_data_base64) % 16)):
            raw_data_base64_16byte += "_"
    else:
        raw_data_base64_16byte = raw_data_base64
    secret_key = hashlib.sha256(key).digest()
    iv = hashlib.md5(iv).digest()
    crypto = AES.new(secret_key, AES.MODE_CBC, iv)
    cipher_data = crypto.encrypt(raw_data_base64_16byte)
    cipher_data_base64 = base64.b64encode(cipher_data)
    return cipher_data_base64

Plaintext data as an argument. key. vector. First of all, change to base64 (to make double-byte characters and images ok) Then the encrypted statement must be a multiple of 16 bits, so Basically, "_" that is not used in base64 is complemented with a ketsu and converted to a multiple of 16 bits. If it is a multiple of 16 bytes, it is as it is Change the key to 32 bit length with sha256. Change the vector to 16-bit length with MD5. Encrypt it, convert it to base64 for easy transmission, and return it.

Decryption

get_decrypt_data.py



from Crypto.Cipher import AES
import hashlib
import base64

def get_decrypt_data(cipher_data_base64, key, iv):
    cipher_data = base64.b64decode(cipher_data_base64)
    secret_key = hashlib.sha256(key).digest()
    iv = hashlib.md5(iv).digest()
    crypto = AES.new(secret_key, AES.MODE_CBC, iv)
    raw_data_base64_16byte = crypto.decrypt(cipher_data)
    raw_data_base64 = raw_data_base64_16byte.split("_")[0]
    raw_data = base64.b64decode(raw_data_base64)
    return raw_data

Cryptographic data converted to base64 as an argument. key. vector. First, convert the encrypted data converted to base64 into encrypted data. Then change the key to 32 bits with sha256. Decrypt and extract only valid base64 with split ("_") [0] and return it as a converted return.

Execution result

main.py


if __name__ == "__main__":
    message = "114514"
    password = "This is password"
    iv = "hoge"
    encrypt_data = get_encrypt_data(message, password, iv)
    print encrypt_data
    decrypt_data = get_decrypt_data(encrypt_data, password, iv)
    print decrypt_data

cmd.sh


$ r64FMFs04APfNQo2d6uFpQ==
$ 114514

Recommended Posts

[Python] I made a function that decrypts AES encryption just by throwing it with pycrypto.
I made a familiar function that can be used in statistics with Python
I made a fortune with Python.
I made a daemon with Python
I made a package that can compare morphological analyzers with Python
[Python] I made an image viewer with a simple sorting function.
I made a shuffle that can be reset (reverted) with Python
I made a segment tree with python, so I will introduce it
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a Discord bot in Python that translates when it reacts
I made a tool that makes decompression a little easier with CLI (Python3)
I made a module PyNanaco that can charge nanaco credit with python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
A story that I was addicted to when I made SFTP communication with python
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a net news notification app with Python
I made a VM that runs OpenCV for Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
A memo that I touched the Datastore with python
I made a web application that maps IT event information with Vue and Flask
I made a system that allows you to tweet just by making a phone call
A story that stumbled when I made a chatbot with Transformer
I made a simple typing game with tkinter in Python
I made a LINE BOT that returns parrots with Go
I made a package to filter time series with python
I made blackjack with python!
Associate Python Enum with a function and make it Callable
I made a simple book application with python + Flask ~ Introduction ~
I made a class to get the analysis result by MeCab in ndarray with python
I made a program to collect images in tweets that I liked on twitter with Python
I made a server with Python socket and ssl and tried to access it from a browser
[I made it with Python] XML data batch output tool
I made a function to crop the image of python openCV, so please use it.
I made a puzzle game (like) with Tkinter in Python
I made a rigid Pomodoro timer that works with CUI
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made a plug-in that can "Daruma-san fell" with Minecraft
Life game with Python [I made it] (on the terminal & Tkinter)
[Python] I made a Line bot that randomly asks English words.
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a library to easily read config files with Python
[Python3] I made a decorator that declares undefined functions and methods.
I want to use a wildcard that I want to shell with Python remove
[Python] A memo that I tried to get started with asyncio
I made a Nyanko tweet form with Python, Flask and Heroku