【问题标题】:Get the key parameter is not a valid public key error in openssl_public_encrypt()在 openssl_public_encrypt() 中获取密钥参数不是有效的公钥错误
【发布时间】:2013-09-21 10:51:03
【问题描述】:

$publicKey = "../ssh/public/pub"; $plaintext = "要加密的字符串";

$pubKey = openssl_pkey_get_public($publicKey);

openssl_public_encrypt($plaintext, $encrypted, $pubKey);

echo $encrypted;   //encrypted string

以上代码生成以下错误

openssl_public_encrypt() [http://php.net/function.openssl-public-encrypt]:密钥参数不是有效的公钥 [APP/controllers/supportservice_controller.php,第 144 行]

我使用 openssl 创建了密钥:

生成一个 1024 位 rsa 私钥,要求输入密码以对其进行加密并保存到文件 openssl genrsa -des3 -out /path/to/privatekey 1024

为私钥生成公钥并保存到文件

openssl rsa -in /path/to/privatekey -pubout -out /path/to/publickey

【问题讨论】:

    标签: php encryption openssl public-key-encryption php-openssl


    【解决方案1】:

    就我而言,我将公钥拆分成多行,解决了问题。

    PHP 版本 7.1.17

        $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($publicKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
    
        $str = "str to be encrypted";
    
        $opensslPublicEncrypt = openssl_public_encrypt($str, $encrypted, $publicKey);
    

    【讨论】:

    【解决方案2】:

    在 PHP 7.x 和新版本的 phpseclib纯 PHP RSA 实现)并使用 composer 安装 phpseclib,您可以这样做:

        # Install the phpseclib from console
        composer require phpseclib/phpseclib:~2.0
    
        // In your php script:
    
        use phpseclib\Crypt\RSA;
    
        $rsa = new RSA();
        $rsa->loadKey($publicKey); # $publicKey is an string like "QEFAAOCAQ8AMIIBCgKCAQEAoHcbG....."
        $plaintext = '...';
        $ciphertext = $rsa->encrypt($plaintext);
    
        var_dump($ciphertext);
    
        #to decrypt:
        $rsa->loadKey('...'); // private key
        echo $rsa->decrypt($ciphertext);```
    
    
    
    

    【讨论】:

      【解决方案3】:

      这样你可以添加你的密钥并加密文本

        $data = json_decode(file_get_contents('php://input'), true);
        $enctext = $data['enctext'];
      
        $pubkey = '-----BEGIN PUBLIC KEY-----
                   PUBLIC  KEY PLACED HERE
                   -----END PUBLIC KEY-----';
      
        openssl_public_encrypt($enctext, $crypted, $pubkey);
        $data['enctext'] =  $enctext;
        $data['Encryption_text'] = base64_encode($crypted);
        echo json_encode($data);
        exit;
      

      或者你也可以调用公钥的 .cert 文件来代替它

        $fp=fopen("publickey.crt","r"); 
        $pub_key_string=fread($fp,8192); 
        fclose($fp); 
        $key_resource = openssl_get_publickey($pub_key_string); 
      
        openssl_public_encrypt($enctext, $crypted, $key_resource );
        $data['enctext'] =  $enctext;
        $data['Encryption_text'] = base64_encode($crypted);
        echo json_encode($data);
        exit;
      

      【讨论】:

        【解决方案4】:

        在 PHP 中使用 OpenSSL 的函数时,公钥必须封装在 X.509 证书中。您可以使用 CSR 创建它。或者你可以使用phpseclib, a pure PHP RSA implementation,直接使用原始公钥。例如。

        <?php
        include('Crypt/RSA.php');
        
        $rsa = new Crypt_RSA();
        $rsa->loadKey('...'); // public key
        
        $plaintext = '...';
        
        //$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
        $ciphertext = $rsa->encrypt($plaintext);
        

        【讨论】:

          猜你喜欢
          • 2012-04-10
          • 2013-10-15
          • 1970-01-01
          • 1970-01-01
          • 2017-10-01
          • 2016-04-06
          • 1970-01-01
          • 2012-07-13
          相关资源
          最近更新 更多