传递私钥和公钥有问题吗?

发布于 2024-07-21 04:31:15 字数 2353 浏览 1 评论 0原文

我需要以字符串格式传递公钥和私钥,以便在pgp中进行加密和解密。 我已经生成了这样的密钥,但我无法使用它们。 那么谁能告诉我如何从中获取字符串格式的公钥和私钥。 而且 rsakeygenerator 也没有给出私钥的密码。 那么我在哪里可以获得私钥的密码呢?

private void button2_Click(object sender, EventArgs e)
{
    // keyPair = createASymRandomCipher();
    //CipherPublicKey publicKey = getCipherPublicKey(keyPair);
    AsymmetricCipherKeyPair keyPair = createASymRandomCipher();
    Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters pubkey = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)keyPair.Public;
    Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters privkey = (Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters)keyPair.Private;
    CipherPublicKey pbkey = getCipherPublicKey(pubkey);
    CipherPrivateKey prvkey = getCipherPrivateKey(privkey);

}

private static AsymmetricCipherKeyPair createASymRandomCipher() 
{
    RsaKeyPairGenerator r = new RsaKeyPairGenerator();
    r.Init(new KeyGenerationParameters(new SecureRandom(),
          1024));
    AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
    return keys;
}

[Serializable]
private struct CipherPrivateKey
{
    public byte[] modulus; 
    public byte[] publicExponent; 
    public byte[] privateExponent; 
    public byte[] p; 
    public byte[] q; 
    public byte[] dP; 
    public byte[] dQ; 
    public byte[] qInv;
}

[Serializable]
private struct CipherPublicKey 
{ 
    public bool isPrivate; 
    public byte[] modulus; 
    public byte[] exponent;
}

private static CipherPublicKey getCipherPublicKey(Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters cPublic) 
{ 
    CipherPublicKey cpub = new CipherPublicKey(); cpub.modulus = cPublic.Modulus.ToByteArray(); 
    cpub.exponent = cPublic.Exponent.ToByteArray(); 
    return cpub; 
}

private static CipherPrivateKey getCipherPrivateKey(Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters cPrivate)
{
    CipherPrivateKey cpri = new CipherPrivateKey(); 
    cpri.dP = cPrivate.DP.ToByteArray(); 
    cpri.dQ = cPrivate.DQ.ToByteArray(); 
    cpri.modulus = cPrivate.Modulus.ToByteArray(); 
    cpri.p = cPrivate.P.ToByteArray(); 
    cpri.privateExponent = cPrivate.Exponent.ToByteArray(); 
    cpri.publicExponent = cPrivate.PublicExponent.ToByteArray(); 
    cpri.q = cPrivate.Q.ToByteArray(); 
    cpri.qInv = cPrivate.QInv.ToByteArray(); 
    return cpri;
}

I need to pass the public key and private key in string format for encryption and decryption in pgp. I've generated the keys like this but I am not able to use those. So can anyone tell me how to get the public key and private key in string format from this. And also the rsakeygenerator has not given the passphrase for private key. So where do I get passphrase for private key?

private void button2_Click(object sender, EventArgs e)
{
    // keyPair = createASymRandomCipher();
    //CipherPublicKey publicKey = getCipherPublicKey(keyPair);
    AsymmetricCipherKeyPair keyPair = createASymRandomCipher();
    Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters pubkey = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)keyPair.Public;
    Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters privkey = (Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters)keyPair.Private;
    CipherPublicKey pbkey = getCipherPublicKey(pubkey);
    CipherPrivateKey prvkey = getCipherPrivateKey(privkey);

}

private static AsymmetricCipherKeyPair createASymRandomCipher() 
{
    RsaKeyPairGenerator r = new RsaKeyPairGenerator();
    r.Init(new KeyGenerationParameters(new SecureRandom(),
          1024));
    AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
    return keys;
}

[Serializable]
private struct CipherPrivateKey
{
    public byte[] modulus; 
    public byte[] publicExponent; 
    public byte[] privateExponent; 
    public byte[] p; 
    public byte[] q; 
    public byte[] dP; 
    public byte[] dQ; 
    public byte[] qInv;
}

[Serializable]
private struct CipherPublicKey 
{ 
    public bool isPrivate; 
    public byte[] modulus; 
    public byte[] exponent;
}

private static CipherPublicKey getCipherPublicKey(Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters cPublic) 
{ 
    CipherPublicKey cpub = new CipherPublicKey(); cpub.modulus = cPublic.Modulus.ToByteArray(); 
    cpub.exponent = cPublic.Exponent.ToByteArray(); 
    return cpub; 
}

private static CipherPrivateKey getCipherPrivateKey(Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters cPrivate)
{
    CipherPrivateKey cpri = new CipherPrivateKey(); 
    cpri.dP = cPrivate.DP.ToByteArray(); 
    cpri.dQ = cPrivate.DQ.ToByteArray(); 
    cpri.modulus = cPrivate.Modulus.ToByteArray(); 
    cpri.p = cPrivate.P.ToByteArray(); 
    cpri.privateExponent = cPrivate.Exponent.ToByteArray(); 
    cpri.publicExponent = cPrivate.PublicExponent.ToByteArray(); 
    cpri.q = cPrivate.Q.ToByteArray(); 
    cpri.qInv = cPrivate.QInv.ToByteArray(); 
    return cpri;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-07-28 04:31:15

您需要向用户询问密码。 拥有密码的全部意义在于,如果没有它,您将无法计算出私钥,并且只有用户可以提供它。

(我没有看过你的其余代码,不熟悉 BouncyCastle API。不过,我确实质疑具有大量字节数组的可变结构的智慧......)

You need to ask the user for the passphrase. The whole point of having a passphrase is that you won't be able to work out the private key without it, and only the user can supply it.

(I haven't looked at the rest of your code, not being familiar with the BouncyCastle API. I do question the wisdom of a mutable struct with lots of byte arrays though...)

莫言歌 2024-07-28 04:31:15

您的转换问题的答案是将它们转换为 Base64Strings

如果您想要十六进制形式(以便用户可以更轻松地输入),您可以使用 System.Runtime.Remoting.Metadata.W3cXsd2001 命名空间来转换为 Base64Strings 或从十六进制代表。 这是一个 C# 示例< /a>.

我还会说,您的处理过程中可能存在安全缺陷,但我不确定我是否有资格解决它。 (参见乔恩的帖子)

The answer to just your converting question is to convert them to Base64Strings

If you want it in hex (so a user can enter it easier), you can use the System.Runtime.Remoting.Metadata.W3cXsd2001 namespace to get convert to/from a HEX rep. Here is an example in C#.

I will also say that there may be a security flaw in your though process, but I am not sure that I am qualified to address it. (See Jon's post)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文