ios RSA 加密 解密

展开阅读全文

RSA加密解密问题?????

04-27

客户端使用Windows 应用程序,服务端使用WebService.rn1、客户端发送一个加密的字符串数组给服务端,该字符数组包括RSA对象的Public Key和要加密的字符;rn2、服务端先用传过来的Public Key解密,在使用Private key 加密该字符串,然后返回给客户端。rn同样也传递Public Key.rn现在按照下面的代码执行,总是解密不了,报rn"An unhandled exception of type 'System.Web.Services.Protocols.SoapException' occurred in system.web.services.dllrnrnAdditional information: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Security.Cryptography.CryptographicException: Bad Key.rnrn at System.Security.Cryptography.RSACryptoServiceProvider._DecryptPKWin2KEnh(IntPtr hPubKey, Byte[] rgbKey, Boolean fOAEP)rn at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)rn at RegisterServer.CRSA.DecryptData(Object[] oData) in c:\inetpub\wwwroot\registerserver\crsa.cs:line 40rn at RegisterServer.Service1.GetLicense(Object[] s_cpu_number) in c:\inetpub\wwwroot\registerserver\service1.asmx.cs:line 59rn --- End of inner exception stack trace ---rn"错误,请问是什么原因呢????rnrn部分代码如下:rn客户端:rn。。。。。rnobject oServiceData = null;rnrnSender oSender = new Sender();//创建加密的RSA封装对象rn//加密函数:rnprivate void btn_encrypt_Click(object sender, System.EventArgs e)rn rn Localhost.Service1 oService = new Service1();//创建Webservice对象 rn object[] oData = new object[2];rn oData[0] = oSender.PublicKeyXML();//获取包括Public Key的RSA XML字符串rn oData[1] = oSender.EncryptData(this.textbox1.text.trim());//加密字符串rn oServiceData = oService.GetLicense(oData);//传给服务端,服务端处理后也返回一个object数组对象 rnrnprivate void btn_decrypt_Click(object sender, System.EventArgs e)rnrn oSender.DecryptData(oServiceData); rnrn。。。。。rn//发送类rnclass Senderrn rn RSAParameters rsaPubParams;rn RSAParameters rsaPrivateParams;rnrn public Sender()rn rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rn rn //Generate public and private key data.rn rsaPrivateParams = rsaCSP.ExportParameters(true);rn rsaPubParams = rsaCSP.ExportParameters(false);rn rn rn public string PublicKeyXML()rn rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rnrn rsaCSP.ImportParameters(rsaPubParams); rn return rsaCSP.ToXmlString(false); rn rnrn //Encrypts using only the public key data.rn public byte[] EncryptData(string s_data)rn rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rn ASCIIEncoding myAscii = new ASCIIEncoding();rnrn rsaCSP.ImportParameters(rsaPrivateParams);rn rn return rsaCSP.Encrypt(myAscii.GetBytes(s_data), false);rn rnrn //Decrypt using the private key data.rn public void DecryptData(object[] oData)rn rn byte[] fromEncrypt;rn string roundTrip;rn ASCIIEncoding myAscii = new ASCIIEncoding();rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rnrn rsaCSP.FromXmlString((string)oData[0]);//从传来的XML字符串重新创建RSArn fromEncrypt = rsaCSP.Decrypt((byte[])oData[1], false);rn roundTrip = myAscii.GetString(fromEncrypt);rnrn System.Windows.Forms.MessageBox.Show(roundTrip);rn rn rnrn服务端:rn。。。。rn[WebMethod]rnpublic object[] GetLicense(object[] dataObject)rnrn RegisterServer.CRSA o_rsa = new CRSA();rnrn return o_rsa.DecryptData(dataObject);rnrn。。。。。rn//接收类rn class CRSArn rn RSAParameters rsaPubParams;rn RSAParameters rsaPrivateParams;rnrn public CRSA()rn rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rn rn //Generate public and private key data.rn rsaPrivateParams = rsaCSP.ExportParameters(true);rn rsaPubParams = rsaCSP.ExportParameters(false);rn rnrn public string PublicKeyXML()rn rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rnrn rsaCSP.ImportParameters(rsaPubParams); rn return rsaCSP.ToXmlString(false); rn rnrn //Decrypt using the private key data.rn public object[] DecryptData(object[] oData)rn rn byte[] fromEncrypt;rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rnrn rsaCSP.FromXmlString((string)oData[0]);//从传来的XML字符串重新创建RSArn fromEncrypt = rsaCSP.Decrypt((byte[])oData[1], false); rn rn return EncryptData(fromEncrypt); rnrn rnrn //Encrypts using only the public key data.rn public object[] EncryptData(byte[] byte_data)rn rn object[] oData = new object[2];rn RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();rn ASCIIEncoding myAscii = new ASCIIEncoding();rnrn rsaCSP.ImportParameters(rsaPrivateParams);rn oData[0] = rsaCSP.ToXmlString(false); rn oData[1] = rsaCSP.Encrypt(byte_data,false);rn return oData;rn rn rnrnrnrn 论坛

没有更多推荐了,返回首页