drj14664 2017-11-22 10:37
浏览 100
已采纳

带有多个密钥的PHP加密

Is there a way to produce two keys in string format, that are dependent on each other?

  1. Master key (to decrypt data)
  2. Slave key (dependent on the Master key, can only decrypt data)
  • 写回答

2条回答 默认 最新

  • duanmie9741 2017-11-22 12:11
    关注

    Nothing like a code story to explain the concept ;p

    Here is an example where alice sends an encrypted message to bob using only bobs public key, bob then responds with an encrypted message using only alices public key.

    In both cases their own private keys are used to decrypt the messages.

    <?php
    
    // define an example, our people, messages and their keys
    $people = [
        'alice' => [
            'keys' => gen_keys(),
            'msg' => 'Hi Bob, I\'m sending you a private message'
        ],    
        'bob' => [
            'keys' => gen_keys(),
            'msg' => 'Thanks Alice, message received'
        ]  
    ];
    
    //
    $encrypted = $decrypted = [
        'alice' => '',
        'bob'   => ''
    ];
    
    // public keys get exchanged, not private
    
    // alice encrypts her message to bob
    $encrypted['bob'] = encrypt(
        $people['alice']['msg'],         // message to encrypt
        $people['bob']['keys']['public'] // bobs public key, which he sent to alice
    );
    
    // message sent to bob
    
    // bob decrypts his message
    $decrypted['bob'] = decrypt(
        $encrypted['bob'],                // message to decrypt
        $people['bob']['keys']['private'] // bob's private key, which he uses to decrypt the message
    );
    
    // bob now responds
    
    // bob encrypts his message to alice
    $encrypted['alice'] = encrypt(
        $people['bob']['msg'],             // message to encrypt
        $people['alice']['keys']['public'] // alice public key, which she sent to bob
    );
    
    // alice decrypts her message
    $decrypted['alice'] = decrypt(
        $encrypted['alice'],                // message to decrypt
        $people['alice']['keys']['private'] // alice's private key, which she uses to decrypt the message
    );
    
    //
    print_r($decrypted);
    
    /*
    Array
    (
        [alice] => Thanks Alice, message received
        [bob] => Hi Bob, I'm sending you a private message
    )
    */
    
    /**
     * Functions - wraps for openssl operations
     */
    // generate public and private key pair
    function gen_keys() {
        $res = openssl_pkey_new(array('private_key_bits' => 2048));
    
        /* Extract the private key */
        openssl_pkey_export($res, $privateKey);
    
        /* Extract the public key */
        $publicKey = openssl_pkey_get_details($res);
    
        return ['public' => $publicKey["key"], 'private' => $privateKey];
    }
    
    // encrypt using public key
    function encrypt($msg, $key) {
        $ret = '';
        openssl_public_encrypt(
            $msg, // message to encrypt
            $ret, // &encrypted message
            $key  // public key
        );
        return $ret;
    }
    
    // decrypts using private key
    function decrypt($msg, $key) {
        $ret = '';
        openssl_private_decrypt(
            $msg, // message to decrypt
            $ret, // &decrypted message
            $key  // private key
        );
        return $ret;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改