我们如何在PHP中使用RSA进行PGP加密/解密?
我已经安装了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该硬编码指纹。您还应该使用私钥进行解密。
You should not hard-code the fingerprint. You should also use the private key to decrypt.
这个问题是在某个时候发布的,因此一个新答案可能不再有用,但是当前版本的代码的最大问题(如我写这个答案的那天问题中的发布)是您正在添加您的解密密钥(私有)作为加密键(公共)偶然。在
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 changeaddencryptkey()
toadddecryptkey()
, andclearencryptkeys()
tocleardecryptkeys()
.You may also need a passphrase for the decryption key (private). That depends on whether the key is set up to require one, though.
以下代码对我有用:
This follow code working for me: