2

ユーザーがメッセージを暗号化された形式で送信するメッセージ暗号化システムを構築したいと考えています。私は GnUPG を使用しています。GnUPGをインストールするには、http: //www.php.net/manual/en/gnupg.installation.php から助けを得ました。サーバーにインストールした後、次のコードで公開キーリングと秘密キーリングを作成します

$GeneratedKey = $gpg->GenKey($name, $comment, $email, $passphrase,$ExpireDate, $KeyType, $KeyLength,$SubkeyType, $SubkeyLength );

function GenKey($RealName, $Comment, $Email, $Passphrase = '', $ExpireDate = 0, $KeyType = 'DSA', $KeyLength = 1024, $SubkeyType = 'ELG-E', $SubkeyLength = 1024)
{
    // validates the keytype
    if (($KeyType != 'DSA') && ($KeyType != 'RSA')) {
        $this->error = 'Invalid Key-Type, the allowed are DSA and RSA';
        return false;
    }

    // validates the subkey
    if ((!empty($SubkeyType)) && ($SubkeyType != 'ELG-E')) {
        $this->error = 'Invalid Subkey-Type, the allowed is ELG-E';
        return false;
    }

    // validate the expiration date
    if (!preg_match('/^(([0-9]+[dwmy]?)|([0-9]{4}-[0-9]{2}-[0-9]{2}))$/', $ExpireDate)) {
        $this->error = 'Invalid Expire Date, the allowed values are <iso-date>|(<number>[d|w|m|y])';
        return false;
    }

    // generates the batch configuration script
    $batch_script  = "Key-Type: $KeyType\n" .
        "Key-Length: $KeyLength\n";
    if (($KeyType == 'DSA') && ($SubkeyType == 'ELG-E'))
        $batch_script .= "Subkey-Type: $SubkeyType\n" .
            "Subkey-Length: $SubkeyLength\n";
    $batch_script .= "Name-Real: $RealName\n" .
        "Name-Comment: $Comment\n" .
        "Name-Email: $Email\n" .
        "Expire-Date: $ExpireDate\n" .
        "Passphrase: $Passphrase\n" .
        "%commit\n" .
        "%echo done with success\n";

    // initialize the output
    $contents = '';

    // execute the GPG command
    if ( $this->_fork_process($this->program_path . ' --homedir ' . $this->home_directory .
            ' --batch --status-fd 1 --gen-key',
        $batch_script, $contents) ) {
        $matches = false;
        if ( preg_match('/\[GNUPG:\]\sKEY_CREATED\s(\w+)\s(\w+)/', $contents, $matches) )
            return $matches[2];
        else
            return true;
    } else
        return false;
}

次のコードで暗号化します

$gpg = new gnupg();
$gpg->addencryptkey($recipient);
$ciphertext = $gpg->encrypt($plaintext);

次のコードで復号化します

$gpg = new gnupg();
$gpg->adddecryptkey($recipient, $receiver_passphrase); 
$plain = $gpg->decrypt($encrypted_text, $plaintext);

これにより、ユーザー名のフォルダーを正常に作成し、そこに秘密鍵と公開鍵リングを生成してから、暗号化された方法でメッセージを送信し、受信者によって復号化します。しかし、私の主な懸念は、サーバーでユーザーの公開キーリングと秘密キーリングを生成したくないことです。代わりに、ユーザーのローカルコンピューターで公開キーリングと秘密キーリングを生成したい..

ローカル コンピューターで公開鍵と秘密鍵を生成することは可能ですか? ユーザーをサーバーのセキュリティに依存させたくないからです。受信者のみがメッセージを解読できます。他の人は解読できません。

ありがとう、

4

1 に答える 1

3

クライアントのブラウザーで実行されるOpenPGP.jsを使用して鍵を作成し、秘密鍵をクライアントのどこかに保存し、公開鍵だけをサーバーに送信できます。

// Create new key with RSA encryption (1), 4k length for
// John Doe with password "foobar"
var keys = openpgp.generate_key_pair(1, 4096,
             "John Doe john.doe@example.org", "foobar"); 
keys.privateKeyArmored; // Access private key
keys.publicKeyArmored;  // Access public key
于 2013-06-25T17:41:56.487 に答える