[英]Save Public Key Bytes as String using Bouncy Castle PGP
I'm able to generate a public/private key pair, and encrypt/decrypt between them. 我能够生成一个公共/专用密钥对,并在它们之间进行加密/解密。 Now, I need to send a stringified version of the PGP public key to someone else, but I'm struggling with how best to do that. 现在,我需要将字符串化版本的PGP公钥发送给其他人,但是我在努力做到最好。
To get my public key, I use: 要获取我的公钥,我使用:
inputStream = PgpUtilities.GetDecoderStream(inputStream);
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);
foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
{
return k;
}
}
}
I'm able to access k
, which is an instance of the PgpPublicKey
class. 我可以访问k
,它是PgpPublicKey
类的实例。
The file that this pulls from is basically jibberish, it's the byte representation of the public key. 这从中提取的文件基本上是乱码,是公钥的字节表示。 I need the string version that you would normally copy and paste between people. 我需要您通常在人与人之间复制和粘贴的字符串版本。
How can I do that here? 我该怎么办?
Thanks! 谢谢!
To encode a key in textual representation with Base64, just wrap the output stream in an ArmoredOutputStream
before calling PgpPublicKey.Encode
. 要使用Base64以文本表示形式对键进行编码,只需在调用PgpPublicKey.Encode
之前将输出流包装在ArmoredOutputStream
PgpPublicKey.Encode
。
Your example extended to get the key as Base64-encoded string: 您的示例扩展为将密钥作为Base64编码的字符串获取:
foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
{
MemoryStream memStream = new MemoryStream();
ArmoredOutputStream armoredStream = new ArmoredOutputStream(memStream);
k.Encode(armoredStream);
armoredStream.Close();
string keyString = Encoding.ASCII.GetString(memStream.ToArray());
//...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.