PHP ASCII Armored PGP encrypted string

血红的双手。 提交于 2019-12-04 20:31:43

Ascii-armored output can be enabled using PHP's gnupg-functions. Have a look at setarmor.

Add this line, probably best directly after creating your $gpg object:

$gpg -> setarmor(1);

But the documentation says ascii armoring would be default; what output do you get and which do you want? Sending ascii armored is convenient when mailing; otherwise you usually choose the smaller binary format directly. Never seen ascii-armored OpenPGP with "headers" stripped.


To your smaller questions:

better to stay away from exec_shell() ... true?

If it is disabled anyway, there is no decision on that. As long as PHP's gnupg-functions have all functionality you need, prefer them; they save you from the hassle interfacing gpg (there is no direct API but the command line tools). Chance to introduce any exploits are smaller, too.

What user should have PGP public key stored in their .gnupg folder?

Choose an arbitrary folder readable (possible not writable?) for the webserver but non-reachable using HTTP (so nobody will be able to fetch your keys). It seems you already realized how to setup this path.

it seems to encrypt, now I just need to figure out if I need/can strip

[snip]

I'd use some regex for this.

preg_match('/[\n\r]([=\n\r[:alnum:]]+)[\n\r]/', $token, $matches);

should do; maybe its more elegant to strip all lines either empty or containing a slash or colon.

halfer

At a basic level, try the following code. It doesn't add ASCII armour, but it does turn it into a screen-friendly (non-binary) format that you could trivially armour yourself.

<?php
$data = 'Secret info';
$encrypted = null;
$ok = openssl_public_encrypt($data, $encrypted, $pubKey);

if ($ok) {
    // Optionally, encode it so you can echo it onscreen
    $encrypted = base64_encode($encrypted);
    echo $encrypted . "\n";
} else {
    echo "Failed to encrypt\n";
}

I don't know if you can import a PGP public key directly into OpenSSH, but this answer might help you convert between key formats if you need to do so.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!