我们如何在PHP中使用RSA进行PGP加密/解密?

发布于 2025-01-19 11:18:00 字数 1730 浏览 2 评论 0原文

我已经安装了gnupg库,并在以下代码中使用了加密和解密:


$public_key = '/path/0xC6235F66-pub.asc';
$private_key = '/path/0xC6235F66-sec.asc';


function encryptText($public_key, $text)
{
    // Set GnuPG homedir to /tmp
    putenv("GNUPGHOME=/tmp");

    $public_key = file_get_contents($public_key); 

    // Create new GnuPG instance
    $gpg = new gnupg();
    // Import given public key
    $key = $gpg->import($public_key);
    // Add imported key for encryption
    $gpg->addencryptkey($key['fingerprint']);
    // Encrypt the secret to a PGP message
    $enc = $gpg->encrypt($text);
    // Clear the encryption key
    $gpg->clearencryptkeys();
    // Return  the PGP message

    return $enc;
}


function decryptText($private_key, $encryptedText)
{
    // Set GnuPG homedir to /tmp
    putenv("GNUPGHOME=/tmp");

    $private_key = file_get_contents($private_key); 

    // Create new GnuPG instance
    $gpg = new gnupg();
    // Import given public key
    $key = $gpg->import($private_key);
    // Add imported key for encryption
    $gpg->addencryptkey($key['fingerprint']);
    // Encrypt the secret to a PGP message
    $decText = $gpg->decrypt($encryptedText);
    // Clear the encryption key
    $gpg->clearencryptkeys();
    // Return  the PGP message

    return $decText;
}


$encrypted = encryptText($public_key, $input = 'just an example');
echo 'Encrypted text: '.$encrypted;

$decrypted = decryptText($private_key, $encrypted);
echo 'Decrypted text: '.$decrypted;

echo 'Match: ';
var_dump($input === $decrypted);

使用上面的EncryptText()函数我获得了加密的文本,但无法与function decrypttext()解密。众所周知,使用RSA的PGP加密可与Private&公钥。我既有钥匙,也可以使用公共密钥进行加密,这给出了一些加密的字符串输出,但无法解密加密的字符串。

请在这里帮忙。

I have installed GnuPG library and used below code for encryption and decryption:


$public_key = '/path/0xC6235F66-pub.asc';
$private_key = '/path/0xC6235F66-sec.asc';


function encryptText($public_key, $text)
{
    // Set GnuPG homedir to /tmp
    putenv("GNUPGHOME=/tmp");

    $public_key = file_get_contents($public_key); 

    // Create new GnuPG instance
    $gpg = new gnupg();
    // Import given public key
    $key = $gpg->import($public_key);
    // Add imported key for encryption
    $gpg->addencryptkey($key['fingerprint']);
    // Encrypt the secret to a PGP message
    $enc = $gpg->encrypt($text);
    // Clear the encryption key
    $gpg->clearencryptkeys();
    // Return  the PGP message

    return $enc;
}


function decryptText($private_key, $encryptedText)
{
    // Set GnuPG homedir to /tmp
    putenv("GNUPGHOME=/tmp");

    $private_key = file_get_contents($private_key); 

    // Create new GnuPG instance
    $gpg = new gnupg();
    // Import given public key
    $key = $gpg->import($private_key);
    // Add imported key for encryption
    $gpg->addencryptkey($key['fingerprint']);
    // Encrypt the secret to a PGP message
    $decText = $gpg->decrypt($encryptedText);
    // Clear the encryption key
    $gpg->clearencryptkeys();
    // Return  the PGP message

    return $decText;
}


$encrypted = encryptText($public_key, $input = 'just an example');
echo 'Encrypted text: '.$encrypted;

$decrypted = decryptText($private_key, $encrypted);
echo 'Decrypted text: '.$decrypted;

echo 'Match: ';
var_dump($input === $decrypted);

Using the above encryptText() function I got the encrypted text but unable to decrypt the same with function decryptText(). As i know, PGP encryption using rsa works with private & public key. I have both the keys in place and using public key for encryption which is giving some encrypted string output but unable to decrypt the encrypted string.

Please help here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚气少女 2025-01-26 11:18:01

您不应该硬编码指纹。您还应该使用私钥进行解密。

function encryptText($public_key, $data)
{
    $gpg = gnupg_init();
    ['fingerprint' => $fingerprint] = gnupg_import($gpg, $public_key);
    gnupg_addencryptkey($gpg, $fingerprint);

    return base64_encode(gnupg_encrypt($gpg, $data));
}

function decryptText($private_key, $data)
{
    $gpg = gnupg_init();
    ['fingerprint' => $fingerprint] = gnupg_import($gpg, $private_key);
    gnupg_addencryptkey($gpg, $fingerprint);

    return gnupg_decrypt($gpg, base64_decode($data));
}

print $encrypted = encryptText($public_key, $input = 'just an example');
print $decrypted = decryptText($private_key, $encrypted);
var_dump($input === $decrypted);

You should not hard-code the fingerprint. You should also use the private key to decrypt.

function encryptText($public_key, $data)
{
    $gpg = gnupg_init();
    ['fingerprint' => $fingerprint] = gnupg_import($gpg, $public_key);
    gnupg_addencryptkey($gpg, $fingerprint);

    return base64_encode(gnupg_encrypt($gpg, $data));
}

function decryptText($private_key, $data)
{
    $gpg = gnupg_init();
    ['fingerprint' => $fingerprint] = gnupg_import($gpg, $private_key);
    gnupg_addencryptkey($gpg, $fingerprint);

    return gnupg_decrypt($gpg, base64_decode($data));
}

print $encrypted = encryptText($public_key, $input = 'just an example');
print $decrypted = decryptText($private_key, $encrypted);
var_dump($input === $decrypted);
萝莉病 2025-01-26 11:18:01

这个问题是在某个时候发布的,因此一个新答案可能不再有用,但是当前版本的代码的最大问题(如我写这个答案的那天问题中的发布)是您正在添加您的解密密钥(私有)作为加密键(公共)偶然。在decrypttext()中,您需要将addenCryptkey()更改为adddecryptkey()clear> clear> clearencryptkeys() ClearDecryptKeys()

您可能还需要一个密码用于解密密钥(私有)。但是,这取决于是否设置了密钥。

This question was posted some time ago, so a new answer may not be useful any longer, but the biggest issue with the current version of your code (as posted in the question on the day I write this answer) is that you're adding your decryption key (private) as an encryption key (public) by accident. In decryptText(), you'll need to change addencryptkey() to adddecryptkey(), and clearencryptkeys() to cleardecryptkeys().

You may also need a passphrase for the decryption key (private). That depends on whether the key is set up to require one, though.

银河中√捞星星 2025-01-26 11:18:00

以下代码对我有用:

    putenv("GNUPGHOME=/tmp");
    $gpg = new gnupg();
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION);
    $publicData = file_get_contents('/var/www/html/web/resources/keys/public.asc');
    $privateData = file_get_contents('/var/www/html/web/resources/keys/SECRET.asc');
    $publicKey = $gpg->import($publicData);
    $privateKey = $gpg->import($privateData);
    $gpg->addencryptkey($publicKey['fingerprint']);
    $gpg->adddecryptkey($privateKey['fingerprint'],"YOUR_PASSPHASE");
    $encrypt = ($gpg->encrypt('Data to encrypt'));
    echo $encrypt;
    echo '<pre>';
    print_r($gpg->decrypt($encrypt));
    echo '</pre>';die;

This follow code working for me:

    putenv("GNUPGHOME=/tmp");
    $gpg = new gnupg();
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION);
    $publicData = file_get_contents('/var/www/html/web/resources/keys/public.asc');
    $privateData = file_get_contents('/var/www/html/web/resources/keys/SECRET.asc');
    $publicKey = $gpg->import($publicData);
    $privateKey = $gpg->import($privateData);
    $gpg->addencryptkey($publicKey['fingerprint']);
    $gpg->adddecryptkey($privateKey['fingerprint'],"YOUR_PASSPHASE");
    $encrypt = ($gpg->encrypt('Data to encrypt'));
    echo $encrypt;
    echo '<pre>';
    print_r($gpg->decrypt($encrypt));
    echo '</pre>';die;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文