gnupg get_key在php

时间:2015-10-04 18:53:09

标签: php gpgpu gnupg

我在gnupg中使用php数字签名文件。之前工作正常。突然间我收到了这个错误:

  

PHP致命错误:未捕获的异常'异常',消息'get_key failed'

putenv("GNUPGHOME=/tmp");
$publicKey = file_get_contents("./media/public.key");

$gpg = new gnupg();
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
$info = $gpg->import($publicKey);
$gpg->addsignkey($info['fingerprint'], DIGITAL_FILE_PASS);
$signed = $gpg->sign($data_to_sign);

可能是什么原因?

信息:

$info变量如下所示:

Array
(
[imported] => 0
[unchanged] => 2
[newuserids] => 0
[newsubkeys] => 0
[secretimported] => 0
[secretunchanged] => 0
[newsignatures] => 0
[skippedkeys] => 0
[fingerprint] => 622FC43AB6E1FC175C08E9C0048EAEBF6E607FA3
)

公钥:

-----开始PGP公共钥匙块----- 版本:GnuPG v1

mQENBFXvNq0BCADAgEoJ9yHbgJp2h86eUqwqP8s4bKa4nFodx4dmUL3dJfrOhibP jiaG9eDyzyT3 + avQVc4 + Z3knYBxanFx1lchDS3EnjLwvfShbtU2X0oiuqoYIM76B 03pCNpAiNsICVtZCYUBPe8Xjl8BNquK1RRdzrQRm29t9qa6TvCMIdc3rl0hOVHeX IgVzyYjE2Je9ICI2MRE7XK76GrdQ9N1nvoTOf6K3QA + wSQjOY4FUk95 / tLUXiGss SQKqkczro4l7PEBCcX99QvY + BBTV4ykJ4lg6cdQabFXSeNfPYb56v62J0S8VswS9 AofwkQGt1TplcFvpoziQ1ecCj2DGYBD6rvzRABEBAAG0KEJoYXNrZXIgUmVkZHkg PGJiaGFza2VyLnJlZGR5QHBuYi5jby5pbj6JAT4EEwECACgFAlXvNq0CGwMFCQlm AYAGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEASOrr9uYH + jgmMIAI9Sg8Kl xbschHKIvndQzfQ / TZyc4LpKRMgJsxw8utluq1ksbnHsmvF4rdgZ2hfnrAxN3ldu vt6lddqi9 + LLQD8pWMqHBb7KmroKND8g7yVSc + ABTBsXzCI1JDBfy + B55lK0lCr + X7dgO5tQmqp5036dGU3PJlYrjiODBLoiOBgRo1VGTnb942a8DuLt + 1EMKDp6swtp Av30SfgA727fnt8DNZRHvkAszBru / TssrU0SjsAhDTRl4vVrUijJlIej0Kp6SsDS RPoIfoBU / HXTNPTRmKxUPX2nyBYmoyW3DV0j1Qr5eTNOQZmAqCiHR2rvTvSxsN1K nYV1dXlavunGUIm5AQ0EVe82rQEIAPqcUwWtCDKwuadXC0a / f5zFVVgQKaSTe8WS D6ErR1Hg8BWHDIJjTIsS0PnoEJTnq72gj / H35tTO4p1NR / JsoemRq0bmyituuNAA uJPDBpMb0HirB / 0Q19E4u + utakZEzVd2jOZqfhLUxplGbScTKN4DLfZsWPfU / MSR 62S / 510JwmzpVFXzbyA82RdD6OsCeExMwNifLUjkARReiFp9tFIUGMI5HqIgOOE3 qeXsmrWGp5pq4BR5umOC6J6XjCi1LVUj7uarlygntBJprKzVbUIPiL3d9 / nLc4Ho Yw0vphIvgoEDQO5It5Ydvo6gxHwdtd + VUi7KZQJi88sUda7n + 5cAEQEAAYkBJQQY AQIADwUCVe82rQIbDAUJCWYBgAAKCRAEjq6 / BMB / oyOCB / 4gV4VXGCYimUqWMw0 / PTFQwECyhlzyhDrGcppT4xj8U3nOo4h9fMdNQ6NGdlG6ej1rq5 / pMXR3z1 / 6I5s + cnEY7L0LiNlKksT4JJ26ziqFGUhBmlCEnivaHxSkuG0nSylXQDlOnh / njzMb / V9P rAlu0a3Qxc10KokowxKO4QipA5cf3jEjjAwlL + pnW6DHqbO54 / amkqKtqNRpfZD9 d0fpZKtLCcCuqAX3Pd6LblEcTFLaD8DBRK6Vjmfm0UXH0aaae8TPFaX4GpI5OdE0 B2IeslOqolzDgbJ / Jd0UMs2lpmX3YE5 / 96I4XpSoM7bWGS + 2DFeOmeImgiYyalKl S7N7mQENBFZTbLABCACj + EWqmkpfmsPOkoTnEM / HuKNIrGXm4ad + v6HuLkh3 / QOV 7hAJcPDMGtp4EMn1UQ1v3NW1WCYlKg7R + 1COaS5O / TaW6QTmfJzrfK / XtZJOgtyH ScJWUOVT06VfXPgWWQdXgOJ8NZbD632O2uVv4oaoMiniURI060GK2gbI915limAy 17w1jlHiHa3edNQeF4LwCPNeJwFLgEz3pGIFsa3Y / UnO194AIKLOz16WxwOds / QY pzQKleDXeuV92tdNb2UAj5KXV / jSeDMqnzRaCPG7WcM3vqJl0jUjZAB68nPtmlLf JhNrO50 / mXGJTxcFJkUsiY5MC2qemQET4n2zJdR5ABEBAAG0KEJoYXNrZXIgUmVk ZHkgPGJiaGFza2VyLnJlZGR5QHBuYi5jby5pbj6JAT4EEwECACgFAlZTbLACGwMF CQlmAYAGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEP3CFUXaMfjxqaUH / 10 3 t89sStKatzNxocqW6g7HrrLn2QFjAW8ud0GHxsTenpgiC6paly3fwtAGD3O3XPIb 8TUXR // ZLVrsXRwU7MLMfQNdjuUAkzAs30OqFyyPmi0v1OgK0r + E + oLoSrNVOFb + m0xOEPljnUTJJBpCHulcGqo8TRu / Bw0b1Tkishe6QbGZr8E691W28vYRYcuz8ftn LrZeFB2Ac6wSaeaT531BAT35qyFO8mhRjZYwlvkIDzFsji5zFygk0h3WQUmb4On9 mXA1reVe2NqECLu7MKKZ1IVrN8ZreWD2273ZQoQUOtE + tKrv + IG5snREmK / f3Zta PrOnPk6xwMfltKbSJIS5AQ0EVlNssAEIAKFa0tX5nHSFVfoQ7zy + / yYzp / YJX / XV +蛟/ qF7MsYnoDzw + 0w2cm / ZYZ + 79xAZMgSlixXDmnHKLmNHsuY6QN6eztXfTog7 / egDJ3raQYUUQrl4 + jW3SBbbPlvbgYoFPHNe / I3Ivf6WNXaXXBkWcK + AfDNX2ag5 8RIL2HvvgQ9l8z + W + PBlZ3Joh7xLyESPVlE0Wxrf + Pcu4a5VXix + T3YgvEYsR0r 0ZKbk7V78JrWvUZEjZw0o8t6kk8ibzFhqvI1YV0EOO0Ng425XsCjJA + sfjvFZ8zF usO6oZcfFDLOORQAmxOKK7dztUxCDWLIuwUrmjrr / u5uO38f5jAygEcAEQEAAYkB JQQYAQIADwUCVlNssAIbDAUJCWYBgAAKCRD9whVF2jH48eETCAChvI2tDT3E /世维会 xHNSLYSHo4xFi9GKOy6MfhkyhDQtdkapwsrv + 4kun / U87qzQ3X / 36K4rIpsoxGRS r6Q9gzE5 + 8HBQGIlz5Waverlamsh9iMxYfYzROyJURrJOpG9bXl3pqB20ZW5Ry1H 4sa5uIlz5 / LWnjIJqLo3E81 + HDO6Gwml3KujiY5NlrFEltX9bU9UoM / u6Os3dlgl BQH / DZpqOPxL3CgKRb7cWCT3xeXIsFFWbreDFDvRndaOSFE3Z8bAYx17deJFf9EE BTRSTRPGS6WZ9Oc2IvsWqs8w / + mq7EVxKZ3T8CWy8d8wxzDzxg9ZNwVCDzbqDCw5 a1xG5T03 = Zji / -----结束PGP公共密钥块-----

2 个答案:

答案 0 :(得分:4)

虽然您的代码是正确的,但我不知道为什么file_get_contents在您的代码中无法正常工作,请使用cURL尝试一下:

此函数是file_get_contents的替代。

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

答案 1 :(得分:0)

将下面的代码添加到代码的第二行。

$publicKey = file_get_contents(getenv('GNUPGHOME')."./media/public.key");