RSAKey的实现
- JScript code
function a(){var PublicKey="A5B55950369962C81804C0929C07776A671E505D746E0B2EA1EEE06762D4448E89342AD5725C1703F5146B 37444751EF4BEB2D0803AE7D1787FA21486E3609761A29C1DB827B8DBBA5D9A8358B6F2C6A8FD942D3A0DE C064263094663EFEB799EB635CBC64EA72A9EE4EC1983520F4795502277B9A53A122D7BDA2CB98FC7FB1";var RSA = new RSAKey();RSA.setPublic(PublicKey, "10001");var v = RSA.encrypt("aaa");} 这段js代码的RSAKey 用c#怎么实现?
引用 http://aq.qq.com/unionverify/js/secrsa.js 文件
[解决办法]
- C# code
using System.Security.Cryptography; /// <summary> /// RSA加密 /// </summary> /// <param name="xmlPublicKey">公钥</param> /// <param name="m_strEncryptString">加密的数据</param> /// <returns>RSA公钥加密后的数据</returns> static public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { string outstr = ""; try { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(xmlPublicKey); byte[] bytes; bytes = provider.Encrypt(Encoding.UTF8.GetBytes(m_strEncryptString), false); outstr = Convert.ToBase64String(bytes); } catch (Exception exception) { throw exception; } return outstr; }
[解决办法]
参考下面的
- C# code
using <mscorlib.dll>using namespace System;using namespace System::Security::Cryptography;using namespace System::Text;Byte RSAEncrypt(Byte DataToEncrypt[], RSAParameters RSAKeyInfo, bool DoOAEPPadding)[]{ try { //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider* RSA = new RSACryptoServiceProvider(); //Import the RSA Key information. This only needs //toinclude the public key information. RSA->ImportParameters(RSAKeyInfo); //Encrypt the passed byte array and specify OAEP padding. //OAEP padding is only available on Microsoft Windows XP or //later. return RSA->Encrypt(DataToEncrypt, DoOAEPPadding); } //Catch and display a CryptographicException //to the console. catch(CryptographicException* e) { Console::WriteLine(e->Message); return 0; }}Byte RSADecrypt(Byte DataToDecrypt[], RSAParameters RSAKeyInfo,bool DoOAEPPadding)[]{ try { //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider* RSA = new RSACryptoServiceProvider(); //Import the RSA Key information. This needs //to include the private key information. RSA->ImportParameters(RSAKeyInfo); //Decrypt the passed byte array and specify OAEP padding. //OAEP padding is only available on Microsoft Windows XP or //later. return RSA->Decrypt(DataToDecrypt, DoOAEPPadding); } //Catch and display a CryptographicException //to the console. catch(CryptographicException* e) { Console::WriteLine(e); return 0; }}int main(){ try { //Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding* ByteConverter = new UnicodeEncoding(); //Create byte arrays to hold original, encrypted, and decrypted data. Byte dataToEncrypt[] = ByteConverter->GetBytes(S"Data to Encrypt"); Byte encryptedData[]; Byte decryptedData[]; //Create a new instance of RSACryptoServiceProvider to generate //public and private key data. RSACryptoServiceProvider* RSA = new RSACryptoServiceProvider(); //Pass the data to ENCRYPT, the public key information //(using RSACryptoServiceProvider.ExportParameters(false), //and a boolean flag specifying no OAEP padding. encryptedData = RSAEncrypt(dataToEncrypt,RSA->ExportParameters(false), false); //Pass the data to DECRYPT, the private key information //(using RSACryptoServiceProvider.ExportParameters(true), //and a boolean flag specifying no OAEP padding. decryptedData = RSADecrypt(encryptedData,RSA->ExportParameters(true), false); //Display the decrypted plaintext to the console. Console::WriteLine(S"Decrypted plaintext: {0}", ByteConverter->GetString(decryptedData)); } catch(ArgumentNullException*) { //Catch this exception in case the encryption did //not succeed. Console::WriteLine(S"Encryption failed."); }}
[解决办法]
[解决办法]
或者说你的意思是想让人帮你按 http://aq.qq.com/unionverify/js/secrsa.js 中定义的 RSAKey 类重写一个C#的非对称加解密类?
[解决办法]
[解决办法]
RSAKey这个js代码中用的类,你能拿到吗? reply by 阿捷