使用RSA解密这两个系统之间的私钥 [英] RSA Decryption using private key between two systems

查看:138
本文介绍了使用RSA解密这两个系统之间的私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的加密解密软件。我用RSA加密encypt我的对称密钥。

I am developing an encryption decryption software. I used RSA encryption to encypt my symmetric key.

我在后面的演练:创建加密应用程序

我的加密和解密成功完成在同一台机器上。但是,当我试图从其他计算机解密,一个错误:坏数据发生(它可以从同一台机器进行解密。)

My encryption and decryption done successfully in same machine. But when I tried to decrypt from other computer, an error: bad data is occurring.(It can be decrypted from same machine.)

我认为这个问题是在获取。从KEYCONTAINER私钥。如何获得在第二台机器在第一台机器生成的私钥。

I think the problem is on getting private key from keycontainer. How to get the private key generated in first machine in the second machine.

我GOOGLE了很多,但在同一台机器上的一切。

I googled a lot but everything in same machine.

请帮帮我,给我一个主意,让其他专用键。

Please help me, give me an idea to get private key in other machine.

public void GetPrivateKey()
    {
        string c;

        cspp.KeyContainerName = keyName;

        rsa = new RSACryptoServiceProvider(cspp);
        rsa.PersistKeyInCsp = true;

        if (rsa.PublicOnly == true)
            c= "Key: " + cspp.KeyContainerName + " - Public Only";
        else
            c = "Key: " + cspp.KeyContainerName + " - Full Key Pair";
    }    


 public string decryptkey(string at)
    {
        byte[] KeyEncrypted;
        KeyEncrypted = File.ReadAllBytes(at);
        //System.IO.File.ReadAllBytes(at);//for good 

        objr.GetPrivateKey();
       byte[] KeyDecrypted = objr.rsa.Decrypt(KeyEncrypted, false);
        string skey = GetString(KeyDecrypted);
        return skey;
    }



错误数据的错误发生在这一行,

Bad data Error happens in this line,

字节[] = KeyDecrypted objr.rsa.Decrypt(KeyEncrypted,FALSE);

请..

推荐答案

使用的 RSACryptoServiceProvider.ToXmlString 方法导出私钥。你需要传递真正来此方法导出私钥。这将为您生成一个包含关键参数包括私营参数的XML文档

Use the RSACryptoServiceProvider.ToXmlString method to export the private key. You need to pass true to this method to export the private key. This will generate for you an XML document that contains the key parameters including the private parameters.

在第二台机器,使用的 RSACryptoServiceProvider.FromXmlString 导入私钥成的RSACryptoServiceProvider实例。

On the second machine, use RSACryptoServiceProvider.FromXmlString to import the private key into a RSACryptoServiceProvider instance.

然而,出于安全考虑,我建议,与其这样做,生成私钥一台机器上(机器,将做解密部分)然后用RSACryptoServiceProvider.ToXmlString并通过来它只是导出公共密钥。在其他计算机上(会做加密的部分),导入使用RSACryptoServiceProvider.FromXmlString方法公钥。

However, for security reasons, I recommend that instead of doing this, generate the private key on one machine (the machine that will do the decryption part), and then use the RSACryptoServiceProvider.ToXmlString and pass false to it to just export the public key. On the other machine (that will do the encryption part), import the public key using the RSACryptoServiceProvider.FromXmlString method.

单独使用公钥,可以做加密这一进程的一部分。

Using the public key alone, you can do the encryption part of the process.

这只是你需要的私钥解密。

It is only for decryption that you are required to have the private key.

下面是一些示例代码:

//Do this on one machine
RSACryptoServiceProvider rsa_machine1 = new RSACryptoServiceProvider(); //You might initialize this in a different way

var xml = rsa_machine1.ToXmlString(true); //or pass false to just export the public key

现在采取<$ C $的价值C> XML 变量到其他机器上(也许它保存到一个文件,然后手动复制该文件到第二台机器)

Now take the value of the xml variable to the other machine (maybe by saving it to a file and then manually copying that file to the second machine)

//This is done on the second machine
RSACryptoServiceProvider rsa_machine2 = new RSACryptoServiceProvider();

rsa_machine2.FromXmlString(xml);

这篇关于使用RSA解密这两个系统之间的私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆