生成pgp密钥的问题?

时间:2009-05-21 10:24:15

标签: c# bouncycastle

我正在使用RSACryptoServiceProvider我已经生成了公钥和私钥。它生成的密钥采用以下格式:

公钥:

<RSAKeyValue>
    <Modulus>m9bAoh2...eGNKYs=</Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue> 

私钥:

<RSAKeyValue>
    <Modulus>m9bAo...ZAIeGNKYs=</Modulus>
    <Exponent>AQAB</Exponent>
    <P>xGj/UcXs...R1lmeVQ==</P>
    <Q>yx6e18aP...GXzXIXw==</Q>
    <DP>NyxvnJ...1xAsEyQ==</DP>
    <DQ>La17Jycd...FhApEqwznQ==</DQ>
    <InverseQ>JrG7WCT...Hp3OWA==</InverseQ>
    <D>RdWsOFn....KL699Vh6HK0=</D>
</RSAKeyValue>

但是使用PGP桌面我生成了这样的键 -

公钥:

mQCNBEoOlp8BBACi/3EvBZ83ZduvG6YHu5F0P7Z3xOnpIsaPvTk0q+dnjwDUa5sU 
lEFbUZgDXSz7ZRhyiNqUOy+IG3ghPxpiKGBtldVpi33qaFCCEBiqsxRRpVCLgTUK 
HP2kH5ysrlFWkxTo 
=a4t9 

私钥:

lQHgBEoOlp8BBACi/3EvBZ83ZduvG6YHu5F0P7Z3xOnpIsaPvTk0q+dnjwDUa5sU 
lEFbUZgDXSz7ZRhyiNqUOy+IG3ghPxpiKGBtldVpi33qaFCCEBiqsxRRpVCLgTUK 
waBnEitQti3XgUUEZnz/rnXcQVM0QFBe6H5x8fMDUw== 
=CVPD 

因此,当我传递PGP Desktop生成的密钥时,它能够完美地进行加密和解密,但是当我传递RSACryptoServiceProvider生成的密钥时,我无法加密和解密?

有谁能告诉我如何在PGP生成的模式中生成密钥?

3 个答案:

答案 0 :(得分:8)

使用bouncycastle c#库这就是我生成密钥对的方式。



public void GenerateKey(string username, string password, string keyStoreUrl)
        {
            IAsymmetricCipherKeyPairGenerator kpg = new RsaKeyPairGenerator();
            kpg.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(0x13), new SecureRandom(), 1024, 8));
            AsymmetricCipherKeyPair kp = kpg.GenerateKeyPair();

            FileStream out1 = new FileInfo(string.Format("{0}secret.asc", keyStoreUrl)).OpenWrite();
            FileStream out2 = new FileInfo(string.Format("{0}pub.asc", keyStoreUrl)).OpenWrite();

            ExportKeyPair(out1, out2, kp.Public, kp.Private, username, password.ToCharArray(), true);

        }

private static void ExportKeyPair(
            Stream secretOut,
            Stream publicOut,
            AsymmetricKeyParameter publicKey,
            AsymmetricKeyParameter privateKey,
            string identity,
            char[] passPhrase,
            bool armor)
        {
            if (armor)
            {
                secretOut = new ArmoredOutputStream(secretOut);
            }

            PgpSecretKey secretKey = new PgpSecretKey(
                PgpSignature.DefaultCertification,
                PublicKeyAlgorithmTag.RsaGeneral,
                publicKey,
                privateKey,
                DateTime.Now,
                identity,
                SymmetricKeyAlgorithmTag.Cast5,
                passPhrase,
                null,
                null,
                new SecureRandom()
                //                ,"BC"
                );

            secretKey.Encode(secretOut);

            secretOut.Close();

            if (armor)
            {
                publicOut = new ArmoredOutputStream(publicOut);
            }

            PgpPublicKey key = secretKey.PublicKey;

            key.Encode(publicOut);

            publicOut.Close();
        }

它以armor ASCII格式生成私钥和公钥,例如。

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG v1.32

mIsEShU7ywEEAKtxKTtGTyUaVxFuWBpziA2l7qDKhe6jznre3DMPuzDnN4Ax573a
7s/bPOkzkK9tEUGFw+BW6F4DkKydv8SQfSN5Vvc0RFMha8X1E8jki1oXTIPA8bKK
dg8ZewZt8+Zwpt5IPAkIydmxDhMjwd71ay3p1ypOfROFPOfc2dBPx/0JAAUTtAdo
YW1zbWFuiJwEEAECAAYFAkoVdAsACgkQEz/ESPB1tojuIQP8CjAzJx8PoIN33pxQ
AfGF+fMCZx8/m7dDBE113aiio25BCvNKOpFwye2UK4ioKN70k24pzkyi8AZO22/s
u6GL7XEiiBZLPynBxJR4A7PzvD3KNqdQUqesu9IkPFyXz3UFH3clR0hnZtZtgnbk
L9dvj5RYVuGiS3Dcf1zoLMOiCdc=
=dFfG
-----END PGP PUBLIC KEY BLOCK-----

答案 1 :(得分:5)

RSA和PGP不同。

你基本上要问的是我如何在柴油上运行汽油车?答案是你不能。


您应该查看PGP CommandLine - 这允许您从命令行执行PGP命令。

答案 2 :(得分:1)

感谢好的方法,很长一段时间以来,他们都在为网络做好准备,

有些纠正......

致电

ExportKeyPair(out1, out2, kp.Public, kp.Private, username, password.ToCharArray(), true);

也请致电

out1.Close();
out2.Close();

从上述例程生成的示例私钥

-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: BCPG C# v1.6.1.0

lQHqBEvhYOcBBACa5HjZ14ULRvZLKj+rwscQh63Zd9rnfZmVKN5fNsK+ocxxV8rc
TWa6P3knIWCra1lqZ1onNtM8tL1XRuK9pagu7XPjjHUR37ajYv/e0/w9IHlOBtM4
1T3oMM48FBH5WGZswOQnTCHoHpZilx7zeJgcyoUayM9uaWGWVLb6PqAE2QAFE/8D
AwL4BEE4Y+mD6GB0ctnbN4tZuctydODYJUNOK3p+UdgEh5rj5nOelB5h3kqFWdwq
8F3EBxB4guRnLOxIeSjzvFoFpqb4QSrM4brdisaHoK0qgrsyCaQhdepVlz662hSD
5bxJPQqmJ4c9X7x6tTMwDAseoz+VUK9U91iN9jv72hp2dXJYjJvBbkTX9euz2i4P
HrlLV2DuOeGWipsb+sIOuYqpZoK5aMq5AAGTTmmApYam/+0d2lXTt+Cw0FkoN1U+
CWriVFE+x58/MmpqKttlrHyp/c7aRmdxeKheY7QAnKPmTRFZAu6HR/DnLSya6+Qz
MqsYBZAocDs7Bg9U6N79Ynl7mkChoVR4ZwJE/OXgHDRSYlcBwT++frHI06cn1l8t
f0CWU/Z6EuYj/rmgL/v5ln7EQ2H+SRypPxYJKK8dTcu2uk5Ev1x4EOQ2nF1BxBB1
CceSVEimc2TDcVj0K7QIc3BhcmVraDOInAQQAQIABgUCS+FS1wAKCRBHXl7hbFhY
BLInA/9TPwmivxofs7/y4xmrl0X+Ruish0l+KC45/MQGU1bT2MYCR5jefyaUIHMK
gH8bX4DHobBFxtkTXoTgpZmm5JNHGiSQoOXqW7iqHOkp6q6rkwV9BYvfsbjMsfAB
bV7l29CMoMDj1qB3k9CJDP4MuorEI5Qx30x07Mm2+uwcYC2+Ag==
=jh9t
-----END PGP PRIVATE KEY BLOCK-----

再次感谢上述方法。

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG C# v1.6.1.0

mIsES+Fg5wEEAJrkeNnXhQtG9ksqP6vCxxCHrdl32ud9mZUo3l82wr6hzHFXytxN
Zro/eSchYKtrWWpnWic20zy0vVdG4r2lqC7tc+OMdRHftqNi/97T/D0geU4G0zjV
PegwzjwUEflYZmzA5CdMIegelmKXHvN4mBzKhRrIz25pYZZUtvo+oATZAAUTtAhz
cGFyZWtoM4icBBABAgAGBQJL4VLXAAoJEEdeXuFsWFgEsicD/1M/CaK/Gh+zv/Lj
GauXRf5G6KyHSX4oLjn8xAZTVtPYxgJHmN5/JpQgcwqAfxtfgMehsEXG2RNehOCl
mabkk0caJJCg5epbuKoc6SnqrquTBX0Fi9+xuMyx8AFtXuXb0IygwOPWoHeT0IkM
/gy6isQjlDHfTHTsybb67BxgLb4C
=ZyOZ
-----END PGP PUBLIC KEY BLOCK-----