简体   繁体   中英

PHP encrypt and decrypt function to python

I'm doing an integration with some server and they have provided me with their APi endpoints and the code example of how to do the integration.

In some step in the integration they require to encrypt the data before send it and decrypt the response. They provided me with the following code in PHP 7.3:

// function use for encryption
function encrypt($text, $key, $type, $iv = "0123456789abcdef",
$size = 16) {
$pad = $size - (strlen($text) % $size);
$padtext = $text . str_repeat(chr($pad), $pad);
$crypt = openssl_encrypt($padtext, "AES-256-CBC", base64_decode($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
return base64_encode($crypt);

// function use for Decryption
function decrypt($crypt, $key, $type, $iv = "0123456789abcdef") {
$crypt = base64_decode($crypt);
$padtext = openssl_decrypt($crypt, "AES-256-CBC", base64_decode($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
$pad = ord($padtext
{
strlen($padtext) - 1});
if ($pad > strlen($padtext)) {
return false;
}

if (strspn($padtext, $padtext
{
strlen($padtext) - 1}, strlen($padtext) - $pad) != $pad) {
$text = "Error";
}

$text = substr($padtext, 0, -1 * $pad);
return $text;
}

My application is working with python and I'm trying to simulate the both functions in python, but i don't know much about PHP so here is what i did so far but i still commented some of the code that i don't know to to replace it in python.

Here is my python try:

from base64 import b64decode
from Crypto.Cipher import AES
def encrypt(text, key, type, iv = "0123456789abcdef",size = 16):
    pad = size - (len(text)%size)
    # padtext = text . str_repeat(chr(pad), pad)
    crypt=AES.new(padtext,AES.MODE_CBC,b64decode(key),iv)
    return b64decode(crypt)

def decrypt(crypt,key, type, iv = "0123456789abcdef"):
    crypt = b64decode(crypt)
    padtext = AES.new(crypt, AES.MODE_CBC, b64decode(key), iv)
    # $pad = ord($padtext
    # {
    #     strlen($padtext) - 1});
    # if ($pad > strlen($padtext)) {
    # return false;
    # }
    if pad > len(padtext):
        return False
    # if (strspn($padtext, $padtext
    # {
    # strlen($padtext) - 1}, strlen($padtext) - $pad) != $pad) {
    # $text = "Error";
    # }

    # text = substr(padtext, 0, -1 * pad)
    return text

After Many tries it was very simple and i manged to get it to work

import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad


class AESCipher(object):

    def __init__(self, key):
        self.key = base64.b64decode(key)

    def encrypt(self, raw):
        raw = pad(bytes(raw.encode('utf-8')), 16)
        iv = b'0123456789abcdef'
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(cipher.encrypt(raw))

    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        iv = b'0123456789abcdef'
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return cipher.decrypt(enc).decode('utf-8')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM