使用RSA加密XML [英] Encrypt XML with RSA

查看:438
本文介绍了使用RSA加密XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用公共客户端密钥在服务器端加密一些XML元素。

 

 private string password = "地狱犬"; 



//获取字节

UnicodeEncoding getBytes = new UnicodeEncoding();

passwordAsByte = getBytes.GetBytes(password);



// crypt

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

rsa.FromXmlString(publicClientKey);

encryptedPassword = rsa.Encrypt(passwordAsByte,false);



//转换为字符串

EncryptedPassword = Convert.ToBase64String(encryptedPassword);



//添加XML

xmlelem = xmldoc.CreateElement("PasswordPhrase");

xmltext = xmldoc.CreateTextNode(EncryptedPassword);

xmlelem.AppendChild(xmltext);

xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);



 


我在客户端发送的这个XML文档,在客户端,我想要解密元素PasswordPhrase

 < pre lang ="xc#"> 

 //加载私钥

RSACryptoServiceProvider rsaObj =(RSACryptoServiceProvider)cert.PrivateKey;



//获取加密元素

EncryptedPassword = xmlDoc.GetElementsByTagName("PasswordPhrase")。Item(0).InnerText;



//获取字节

UnicodeEncoding getBytes = new UnicodeEncoding();

byte [] passwordAsByte = getBytes.GetBytes(EncryptedPassword);


//解密

byte [] decryptedPassword = rsaObj.Decrypt(passwordAsByte,false);



但它完成了这个错误:


要解密的数据超过这个模数的最大值为128字节。


有什么问题?我将加密数据转换为XML格式的Base64,在客户端发送XML,在客户端我无法解密Xml元素。

解决方案

为了帮助我你解决了这个问题,我需要看一个说明问题的完整例子。我需要查看客户端,服务器端以及您尝试加密和解密的XML。

在创建这样的示例时,请考虑将客户端和服务代码放在同一个过程(同一段代码)。这将消除网络作为问题的一部分。


I crypt some XML element on server side with public client key.

          private string  password="cerberus";

          

          //get bytes

          UnicodeEncoding getBytes  = new UnicodeEncoding();

          passwordAsByte = getBytes.GetBytes(password);



          //crypt

          RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

          rsa.FromXmlString(publicClientKey);

          encryptedPassword = rsa.Encrypt(passwordAsByte, false);



         //convert to string

          EncryptedPassword  = Convert.ToBase64String(encryptedPassword);

         

         //add in XML

          xmlelem = xmldoc.CreateElement("PasswordPhrase");

          xmltext = xmldoc.CreateTextNode(EncryptedPassword);

          xmlelem.AppendChild(xmltext);

          xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);


 

this XML document I send on client side, on client side I want decrypt element PasswordPhrase

 

//load private key          

RSACryptoServiceProvider rsaObj = (RSACryptoServiceProvider)  cert.PrivateKey;



//get encrypt element            

EncryptedPassword = xmlDoc.GetElementsByTagName  ("PasswordPhrase").Item(0).InnerText;



//get bytes

UnicodeEncoding getBytes  = new UnicodeEncoding();

byte[] passwordAsByte = getBytes.GetBytes(EncryptedPassword);


//decrypt 

byte[] decryptedPassword = rsaObj.Decrypt(passwordAsByte, false);

But it finish with this error:

The data to be decrypted exceeds the maximum for this modulus of 128 bytes.

What is problem ? I convert encrypt data to Base64 in XML, send XML on client , and on client side I can not decrypt Xml element.

解决方案

In order to help you resolve this problem, I would need to see a complete example that illustrates the problem. I would need to see the client side, the server side, and the XML that you're trying to encrypt and decrypt.

In creating such an example, consider placing the client and service code in the same process (same piece of code). This will eliminate the networking as being part of the problem.


这篇关于使用RSA加密XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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