jaquarh/phpencrypter

轻松使用PHP加密和签名数据

dev-master 2020-12-20 15:10 UTC

This package is auto-updated.

Last update: 2024-09-21 00:24:32 UTC


README

PHPEncrypter 是一个开源库,利用 LibSodium 加密参与方之间的数据并签名数据。

当前版本

1.0.2 - 添加了 base64 互译器,以便加密数组和对象。

安装

composer require jaquarh/phpencrypter

LibSodium 安装需要在你的 php.ini 配置中启用扩展,或者通过编译 PHP 源代码并使用 LibSodium 配置选项。

自 PHP 7.2.0 版本起,此扩展已包含在 PHP 中。对于旧版本的 PHP,此扩展可通过 PECL 提供。

一旦启用了扩展,你现在可以克隆存储库。只需创建一个类并使用 Cipher。

class MyFirstCipher
{
    use \Cipher\Cipher;
}

示例场景

这里是一个现实生活中的例子。Bob 想给 Alice 发送一个秘密消息,他加密并签名了消息。然后 Alice 读取了消息。

    public function demo()
    {
        # Issue keys (would be stored in the database and retrieved as needed)
        $bob   = $this->issueKeys();
        $alice = $this->issueKeys();

        # Encrypt a message from Bob to Alice
        $cipher = $this->encrypt($alice->public, $bob->private, 'This is a test message');

        # Sign the message and send
        $bobSig    = $this->issueSignatureKeys();
        $signature = $this->signMessage($cipher->cipher, $bobSig->private);

        # Alice now verifies the message using the signature sent
        if($this->verifySignature($signature, $bobSig->public))
        {
            # Decrypt the message that was also sent along with the signature and nonce
            echo $this->decrypt($alice->private, $bob->public, $cipher->cipher, $cipher->nonce);
        }
    }

生成你的密钥

为了生成密钥对,你必须使用 issueKeys() 方法。每个用户或参与方都必须有一个密钥对,以便于使用,分为公钥和私钥。

class MyFirstCipher
{
    use \Cipher\Cipher;
    
    private $userOne = [], $userTwo = [];
    
    public function __construct()
    {
        $this->userOne['kp'] = $this->issueKeys();
        $this->userTwo['kp'] = $this->issueKeys();
    }
}

加密消息

为了加密消息,你必须知道消息是要发送给哪个用户或参与方。我们使用第三方的公钥来加密数据,并使用我们的私钥来签名。例如,如果 userOne 想给 userTwo 发送消息,他可以这样操作。

class MyFirstCipher
{
    use \Cipher\Cipher;
    
    private $userOne = [], $userTwo = [];
    
    public function __construct()
    {
        $this->userOne['kp'] = $this->issueKeys();
        $this->userTwo['kp'] = $this->issueKeys();
    }
    
    public function sendMessage()
    {
        return $this->encrypt($this->userTwo['kp']->public, $this->userOne['kp']->private, 'User ones secret message to user two');
    }
}

解密消息

为了解密 userOne 发送的消息,我们必须知道谁发送了消息。使用我们的私钥和第三方的公钥,我们能够像这样解密消息。

class MyFirstCipher
{
    use \Cipher\Cipher;
    
    private $userOne = [], $userTwo = [];
    
    public function __construct()
    {
        $this->userOne['kp'] = $this->issueKeys();
        $this->userTwo['kp'] = $this->issueKeys();
    }
    
    public function sendMessage()
    {
        # Returns an object ->cipher & ->nonce
        return $this->encrypt($this->userTwo['kp']->public, $this->userOne['kp']->private, 'User ones secret message to user two');
    }
    
    public function readMessage($cipher, $nonce)
    {
        return $this->decrypt($this->userTwo['kp']->private, $this->userOne['kp']->public, $cipher, $nonce);
    }
}

签名加密消息

为了验证消息来自用户,我们可以在发送消息之前对消息进行签名。

class MyFirstCipher
{
    use \Cipher\Cipher;
    
    private $userOne = [], $userTwo = [];
    
    public function __construct()
    {
        $this->userOne['kp'] = $this->issueKeys();
        $this->userTwo['kp'] = $this->issueKeys();
        
        # Lets issue signature keys
        $this->userOne['skp'] = $this->issueSignatureKeys();
    }
    
    public function sendMessageAndSign()
    {
        return (object) [
            'cipher'    => ($cipher = $this->encrypt($this->userTwo['kp']->public, $this->userOne['kp']->private, 'User ones secret message to user two')),
            'signature' => $this->signMessage($cipher->cipher, $this->userOne['skp']->private)
        ];
    }
}

验证签名

class MyFirstCipher
{
    use \Cipher\Cipher;
    
    private $userOne = [], $userTwo = [];
    
    public function __construct()
    {
        $this->userOne['kp']  = $this->issueKeys();
        $this->userTwo['kp']  = $this->issueKeys();
        $this->userOne['skp'] = $this->issueSignatureKeys();
    }
    
    public function verifyEncryption($signature)
    {
        return $this->verifySignature($signature, $this->userOne['skp']->public);
    }
}