Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AES在python中加密在php中解密_Python_Php_Encryption_Cryptography_Aes - Fatal编程技术网

AES在python中加密在php中解密

AES在python中加密在php中解密,python,php,encryption,cryptography,aes,Python,Php,Encryption,Cryptography,Aes,我问这个问题是因为我不太懂php,但必须以某种方式管理它。 我用python加密了数据,需要用php(serversite)解密。 python加密: import hashlib from base64 import b64encode, b64decode, urlsafe_b64encode, urlsafe_b64decode from Crypto.Cipher import AES text = "secret" secret_key = 'This is my

我问这个问题是因为我不太懂php,但必须以某种方式管理它。 我用python加密了数据,需要用php(serversite)解密。 python加密:

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'=')
这将提供“rtvaboadf52863xohhww”输出,该输出已正确加密

和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";

您正在为$this->iv分配额外的4个字符

。这将解决此问题:

echo '<br>: ' . ($this->iv = substr(hash('sha256', 'This is my secret iv'), 0, 16)) . '<br>';
我不是加密专家,但是。。。我认为你的代码中有些东西不太适合那里。当我删除这两行时:

$string = base64_decode($string);
$output = base64_encode($output);
我得到这个输出:

rtVabOaDdf528T63xOhhww==

rtrim($ok,'=')之后,将给您

rtVabOaDdf528T63xOhhww

使用当前代码,您应该会收到关于IV太长的警告(传递的是20字节,例外的是16字节)。如果您没有收到此错误消息,请尝试通过放置
error\u reporting(-1)来启用它在你的脚本的第一行谢谢EdvinasPocius,Martin Toms已经帮了我这个忙,但是还有一个问题。。基本上,我无法解密用python加密的内容
rtVabOaDdf528T63xOhhww