2012-06-10 71 views
2

我想在Erlang中做一些與公鑰有關的事情,他們需要我跟蹤公鑰。根據this page,我應該可以通過使用file:read_file/1public_key:decode_pem/1來導入PEM格式的密鑰。事情是,當我嘗試導入GPG公鑰時,我沒有得到結果。Erlang - 導入GPG公鑰

我有一個叫做inaimathi.pubkey,內容如下文件:

-----BEGIN PGP PUBLIC KEY BLOCK----- 
Version: GnuPG v1.4.11 (GNU/Linux) 

mQINBE9NBIQBEADMSzN6b0FaPP0rGiLDWKfH4ehN66Z0SAIynXm6lBHjmO69pNsm 
iIe4p1X9aXhr7EgEZKdbqevfXW7NuA/oL7Rbt+tzBT5nS2cYSHoZhUC/onVhJxUb 
drCR9NsBDZc1wZs+b95K3vWW91wMPYs4tn71DeeNgUVCcZOGofWltI7+etTVVIyG 
zMEW4lOiEwBgLee+5u3XyGsBbyhtWnbp8ZMewiXjo14w0UCMEQxvf55NjggAO/vR 
C+Czz5FQ7m9AnZwqIZJfaAw+aM81lmcXRZenGZ/H25H6WLq2DrVXnRjEyUvK+juj 
K6rTHcot6K4Cgvo3P47uZcze92c4KaI22jTePRB1qov8ygAQw8BPsaZ+cA3AQ2Zv 
4hjIyx2Qovs+vb1xWhk9Mgqyt0ToMZ6HNn386ICxaeYR8XY/rhf9ej0PJOS5c9Nx 
reeTdrYXeco27kp/x+nDSWzqy0MrSAAD19xCK1w8Eiwc9MmjAAX1yarQzUy/ICey 
eY6SaBfdK7x7A1ecUSu7mS7LmGFWB49Tx5u3ENXw9rU2hKbmGbbQsc+xCx5s7ZM1 
q0aATJsUO4fsNe+4/9cBXCSP+4P6irX1EfliqaySt0LEr0WYhIgNgYq8PB7yx4Cv 
0mOMv8lXBMYZs8NTrMJl2XHfi3egu8Ti14VChCklnWe95sZvKrk9/3BYxwARAQAB 
tCtMZW8gWm92aWMgKEluYWltYXRoaSkgPGxlby56b3ZpY0BnbWFpbC5jb20+iQI4 
BBMBAgAiBQJPTQSEAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQI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T8MAe/vB9kMl4N3p/3bJPuzmP4lnAGrLwzBZPkiQTNq8aj8 
5NsttvIOclBY5A== 
=Zqph 
-----END PGP PUBLIC KEY BLOCK----- 

這似乎是正確的格式,但基於對文檔的結果我得到的是

1> {ok, GpgBin} = file:read("inaimathi.pubkey"). 
{ok,<<"-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: GnuPG v1.4.11 (GNU/Linux)\n\nmQINBE9NBIQBEADMSzN6b0FaPP0rGiLDWKf"...>>} 
2> public_key:pem_decode(GpgBin). 
[] 

,我希望這會返回一個RSAEntry,然後我可以解碼成公鑰。我需要做些什麼來通過這個機制導入公鑰? GPG密鑰實際上是否是有效的PEM文件?我錯過了別的嗎?

回答

3

ASCII裝甲和PEM非常相似。您只需更改BEGIN/END標記,剝離PGP標頭和校驗和(校驗和是最後一行=Zqph),然後添加PEM標頭。因此,您必須將inaimathi.pubkey更改爲此文件:

-----BEGIN RSA PUBLIC KEY----- 
mQINBE9NBIQBEADMSzN6b0FaPP0rGiLDWKfH4ehN66Z0SAIynXm6lBHjmO69pNsm 
iIe4p1X9aXhr7EgEZKdbqevfXW7NuA/oL7Rbt+tzBT5nS2cYSHoZhUC/onVhJxUb 
drCR9NsBDZc1wZs+b95K3vWW91wMPYs4tn71DeeNgUVCcZOGofWltI7+etTVVIyG 
zMEW4lOiEwBgLee+5u3XyGsBbyhtWnbp8ZMewiXjo14w0UCMEQxvf55NjggAO/vR 
C+Czz5FQ7m9AnZwqIZJfaAw+aM81lmcXRZenGZ/H25H6WLq2DrVXnRjEyUvK+juj 
K6rTHcot6K4Cgvo3P47uZcze92c4KaI22jTePRB1qov8ygAQw8BPsaZ+cA3AQ2Zv 
4hjIyx2Qovs+vb1xWhk9Mgqyt0ToMZ6HNn386ICxaeYR8XY/rhf9ej0PJOS5c9Nx 
reeTdrYXeco27kp/x+nDSWzqy0MrSAAD19xCK1w8Eiwc9MmjAAX1yarQzUy/ICey 
eY6SaBfdK7x7A1ecUSu7mS7LmG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2TLpn 
zh82VgwRTDkelpYJG8R8eBXwbBik+WSeIprZ8IrMfzZNs7xwn+z9isB0P3+VObWj 
tBYKdMoF1mu40O1I7hsWV9UpJjRw2WrI8WpZMQTInXcaDWomWRgVhQjVhenYf6u2 
nVoN3MM47hR7OR6KtcQ59lwlBeLcK9ImFuXjPK1GG9LtHzkKVOTI6p51S6Ug5MPu 
7BuJxpxwDiohhVizBk3oZWInpNiXomk7Q6XwQ65mFTB11bX+wn0JIWO7BUWxK+dH 
E+8YuCe53OAvNU0BFT8MAe/vB9kMl4N3p/3bJPuzmP4lnAGrLwzBZPkiQTNq8aj8 
5NsttvIOclBY5A== 
-----END RSA PUBLIC KEY----- 
+0

它工作正常。後續問題,如果你知道的話,有沒有辦法讓'gpg'直接輸出PEM格式的公共密鑰,或者這意味着我必須在Erlang中做一些關鍵操作以接受來自GnuPG的? – Inaimathi