【问题标题】:PHP encryption with multiple keys使用多个密钥的 PHP 加密
【发布时间】:2017-11-22 10:37:46
【问题描述】:

有没有办法以字符串格式生成两个相互依赖的键?

  1. 主密钥(用于解密数据)
  2. 从属密钥(依赖于主密钥,只能解密数据)

【问题讨论】:

  • 是的,它被称为“非对称加密”,虽然“从”密钥——这里的公钥——只能加密而不能解密。对于解密,您使用私钥。 en.wikipedia.org/wiki/Public-key_cryptography
  • @jeroen public key [...] can only encrypt and not decrypt 并不完全正确:两个密钥都可以加密和解密,但一个密钥只能解密用 other 密钥加密的内容。
  • @MatteoTassinari 这并不总是正确的,例如见cs.stackexchange.com/questions/59675/…

标签: php php-7


【解决方案1】:

没有什么比代码故事更能解释这个概念了;p

这是一个示例,其中 alice 仅使用 bobs 公钥向 bob 发送加密消息,然后 bob 使用仅使用 alices 公钥的加密消息进行响应。

在这两种情况下,他们自己的私钥都用于解密消息。

<?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;
}

【讨论】:

  • :) 太尴尬了!!!我总是把这个词弄错,它是我的克星!
【解决方案2】:

是的,它被称为非对称加密。使用公钥对数据进行加密,然后使用私钥对数据进行解密。这用于许多地方,例如在区块链、支付门户等领域。

你可以在这里找到一些有用的算法和理论来帮助理解:https://www.tutorialspoint.com/cryptography/public_key_encryption.htm

在 PHP 中,您可以使用 - openssl_encrypt()openssl_decrypt() - 来获得类似的结果,或者 - base64_encode()base64_decode() 或者您可以将两者混合使用以获得更安全的解决方案。

一个简单的例子可以是:

function my_simple_crypt( $string, $action = 'e' ) {
    // you may change these values to your own
    $secret_key = 'my_simple_secret_key';
    $secret_iv = 'my_simple_secret_iv';

    $output = false;
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'sha256', $secret_key );
    $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );

    if( $action == 'e' ) {
        $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
    }
    else if( $action == 'd' ){
        $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
    }

    return $output;
}

加密:

$encrypted = my_simple_crypt( 'Hello World!', 'e' );

解密:

$decrypted = my_simple_crypt( 'Hello World!', 'd' );

来源:https://nazmulahsan.me/simple-two-way-function-encrypt-decrypt-string/

【讨论】:

  • 嗨,谢谢你的回答!然而,这并不是我想要的。我正在寻找一种方法是用私钥创建公钥。更重要的是,公钥需要能够仅对内容进行解密,而不是加密。我的想法是拥有两台PC。仍然有一台 PC 拥有私钥,并且能够生成公钥,供其他 PC 使用,仅对内容进行解密。
  • 公钥实际上根本不允许加密
猜你喜欢
  • 2010-10-10
  • 2014-07-01
  • 2011-03-01
  • 2012-09-09
  • 2012-03-28
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多