简体   繁体   English

python中的AES加密在php中解密

[英]AES encrypt in python decrypt in php

I am asking this because I don't really know php but must to somehow manage with.我问这个是因为我并不真正了解 php,但必须以某种方式进行管理。 I have encrypted data in python and need to decrypt in php (serversite).我在python中加密了数据,需要在php(服务器站点)中解密。 python encryption:蟒蛇加密:

import hashlib
from base64 import b64encode, b64decode, urlsafe_b64encode, urlsafe_b64decode
from Crypto.Cipher import AES

text = "secret"
secret_key = 'This is my secret key'
secret_iv = 'This is my secret iv'

key = hashlib.sha256(secret_key.encode('utf-8')).hexdigest()[:32].encode("utf-8")
iv = hashlib.sha256(secret_iv.encode('utf-8')).hexdigest()[:16].encode("utf-8")

_pad = lambda s: s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)

txt = _pad(text)

cipher = AES.new(key, AES.MODE_CBC, iv)
output = urlsafe_b64encode(cipher.encrypt(str.encode(txt))).rstrip(b'=')

this gives 'rtVabOaDdf528T63xOhhww' output, which is correctly AES encrypted.这给出了 'rtVabOaDdf528T63xOhhww' 输出,这是正确的 AES 加密。

and php which encrypts and decrypts in other way:和 php以其他方式加密和解密:

<?php

$string="secret";
class CryptService{
    private static $encryptMethod = 'AES-256-CBC';
    private $key;
    private $iv;

    public function __construct(){
       echo  '<br>: '.$this->key = substr(hash('sha256', 'This is my secret key'), 0, 32);
       echo  '<br>: '.$this->iv = substr(hash('sha256', 'This is my secret iv'), 0, 16).'<br>';
    }

    public function decrypt($string){
    // $string = strtr($data, '-_', '+/');

        $string = base64_decode($string);
        return openssl_decrypt($string, self::$encryptMethod, $this->key, 0, $this->iv);
    }

    public function encrypt($string){
        $output = openssl_encrypt($string, self::$encryptMethod, $this->key, 0, $this->iv);
        $output = base64_encode($output);
        return $output;
    }
}

$a = new CryptService;
echo $ok=$a->encrypt('secret');
echo "\n";
echo 'TEST: '.$a->decrypt($string);
echo 'BACK ok: '.$a->decrypt($ok);
echo "\n\n";

There is some issue with openssl_decrypt() function because of "iv".由于“iv”,openssl_decrypt() 函数存在一些问题。 Can someone help me figure this out...有人能帮我解决这个问题吗...

You're assigning additional 4 characters <br> to your $this->iv.您正在为 $this->iv 分配额外的 4 个字符<br> This will fix it:这将修复它:

echo '<br>: ' . ($this->iv = substr(hash('sha256', 'This is my secret iv'), 0, 16)) . '<br>';

Basically, your . '<br>'基本上,您的. '<br>' . '<br>' is concatenating the <br> to your substr() . . '<br>'<br>连接到您的substr() I added () around the variable value assignment.我在变量值分配周围添加了() Now it works现在它起作用了

cnRWYWJPYURkZjUyOFQ2M3hPaGh3dz09 TEST: BACK ok: secret

I am not an expert on encryption, but... I think there's something in your code that doesn't quite belong in there.我不是加密方面的专家,但是...我认为您的代码中有一些不完全属于其中的内容。 When I remove these two lines:当我删除这两行时:

$string = base64_decode($string);
$output = base64_encode($output);

I get this output:我得到这个输出:

rtVabOaDdf528T63xOhhww==

在此处输入图片说明

Which, after a rtrim($ok, '=');其中,在rtrim($ok, '='); , would give you ,会给你

rtVabOaDdf528T63xOhhww

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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