代码之家  ›  专栏  ›  技术社区  ›  Borja

如何在不知道密钥的情况下在PHP中解密?

  •  0
  • Borja  · 技术社区  · 5 年前

    你能告诉我解密是否容易,有什么方法,也许我应该改变加密方法?

    $text = "Hello this is my word";
    $method = "aes-256-ecb";
    $ivlen = openssl_cipher_iv_length($method);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $secretKey= "abcdefghilmnop10032001";
    
    $encrypted = openssl_encrypt($text, $method, $secretKey, $options=0, $iv);
    $decrypted = openssl_decrypt($encrypted, $method, $secretKey, $options=0, $iv);
    
    print_r($encrypted);
    print_r('<br>');    
    print_r($decrypted);
    

    加密文本是

    Ad4jgTNQlNiSBXGidMoAPZeJkUAxQrYPYKHwc9/80Z0=
    

    除此之外 openssl_encrypt 有可能

    0 回复  |  直到 5 年前
        1
  •  1
  •   Barmar    5 年前

    你可以在钥匙上加盐。

    $text = "Hello this is my word";
    $method = "aes-256-ecb";
    $ivlen = openssl_cipher_iv_length($method);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $secretKey= "abcdefghilmnop10032001";
    $salt_length = 10;
    $salt = make_salt($salt_length); // You need to define this function
    
    $encrypted = $salt . openssl_encrypt($text, $method, $salt . $secretKey, $options=0, $iv);
    $salt = substr($encrypted, 0, $salt_length);
    $decrypted = openssl_decrypt(substr($encrypted, $salt_length), $method, $salt . $secretKey, $options=0, $iv);
    
    print_r($encrypted);
    print_r('<br>');    
    print_r($decrypted);