日期:2014-05-18 浏览次数:20977 次
function a() { var PublicKey="A5B55950369962C81804C0929C07776A671E505D746E0B2EA1EEE06762D4448E89342AD5725C1703F5146B 37444751EF4BEB2D0803AE7D1787FA21486E3609761A29C1DB827B8DBBA5D9A8358B6F2C6A8FD942D3A0DE C064263094663EFEB799EB635CBC64EA72A9EE4EC1983520F4795502277B9A53A122D7BDA2CB98FC7FB1"; var RSA = new RSAKey(); RSA.setPublic(PublicKey, "10001"); var v = RSA.encrypt("aaa"); }
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; }
------解决方案--------------------
参考下面的
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[];