如何从 pem 文件或 xml 文件读取 RSA 公钥
我有一件简单的事情要做:我想使用 AES 算法和 pem 文件中包含的密钥来加密数据,如页面所示:http://msdn.microsoft.com/en-us/library/sb7w85t6.aspx
在此示例中,每次执行该函数时都会创建一个新的加密密钥正在运行。 但我需要从 pem 文件或 xml 文件读取此密钥,但我找不到方法。
有没有一种简单的方法可以从 pem 文件读取密钥并将其转换为字节数组 (byte[]) ?
我使用的是 C# - .net Framework 3.5,文件中的密钥是我们合作伙伴的 RSA 公钥。
I have a simple thing to do : I want to encryt data using AES algorythm and a key contained in a pem file, like shown on the page : http://msdn.microsoft.com/en-us/library/sb7w85t6.aspx
In this example, a new encryption key is created every time the function is run.
But I need to read this key from either a pem file or an xml file but I can't find a way to do it.
Is there a simple way to read a key from a pem file and convert it into a byte array (byte[]) ?
I am using C# - .net Framework 3.5 and the key in the file is the RSA public key of our partner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RSA 密钥位于哪种 XML 文件中?
.Net 的
RSACryptoServiceProvider
类可以使用 < 从 XML 读取公钥code>FromXmlString 方法,格式如下:编辑
据我了解您的过程,您正在使用 RSA 公钥作为 AES 对称密钥。 不要这样做!它增加了安全的假象,但没有采取任何措施来保护您的数据。打个比方,这就像发送一个保险箱及其钥匙,但首先将钥匙放入粉红色的盒子中。如果您这样做,任何获得公共 RSA 密钥的人都将能够解密您的数据; RSA 私钥根本不会被使用。
如果第三方强迫您这样做,请向他们展示这个答案,或者询问任何半正派的密码学家。
不允许他们这样做
您应该做的是创建一个随机 AES 密钥,使用 RSA 公钥对其进行加密,然后将加密的密钥与加密的数据一起发送。这样,只有拥有 RSA 密钥的人才能读取数据,因为其他人无法解密对称 AES 密钥。
What kind of XML file is the RSA key in?
.Net's
RSACryptoServiceProvider
class can read public keys from XML using theFromXmlString
method in the following format:EDIT
As I understand your procedure, you're using the RSA public key as an AES symmetric key. DO NOT DO THIS! It adds the illusion of security without doing anything to protect your data. As an analogy, it's like sending a safe along with its key, but putting the key in a pink box first. If you do it this way, anyone who gets the public RSA key will be able to decrypt your data; the private RSA key wouldn't be used at all.
If a third party is forcing you to do it this way, show them this answer, or ask any half-decent cryptographer.
DO NOT ALLOW THEM TO DO IT THIS WAY
What you should be doing is creating a random AES key, encrypting it with the RSA public key, and then sending the encrypted key along with the encrypted data. This way, the data will only be readable by people who have the private RSA key, as anyone else wouldn't be able to decrypt the symmetric AES key.