asp.net rsa解密问题
谁能帮我提供一份完整的asp.net读取私匙 RSA解密的代码我自己写了一个 总是提示字符无效
请大家帮我提供一个完整的代码 谢谢 --------------------编程问答-------------------- 没人回答吗 自己顶上去 --------------------编程问答-------------------- 没人回答吗 自己顶上去 --------------------编程问答-------------------- 下面的代码示例使用 RSACryptoServiceProvider 类将一个字符串加密为一个字节数组,然后将这些字节解密为字符串。
--------------------编程问答-------------------- 顶3楼 --------------------编程问答--------------------
using System;
using System.Security.Cryptography;
using System.Text;
class RSACSPSample
{
static void Main()
{
try
{
//文件编码
UnicodeEncoding ByteConverter = new UnicodeEncoding();
//将字符串转换成字节数组便于加密
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
//创建RSA加密类.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//加密
encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);
//解密
decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);
//显示解密效果.
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
}
catch(ArgumentNullException)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine("Encryption failed.");
}
}
//加密
static public 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 null;
}
}
//解密
static public 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.ToString());
return null;
}
}
}
不错! --------------------编程问答-------------------- 三楼的代码执行没有错, 但有个问题不明白, 想请教.
在Main()代码中加密后又直接解密,
//加密
encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);
//解密
decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);
在解密中所使用的encryptedData是刚加密完的字节, 但使用上并不会加密后又马上解密, 因此我将加密后的encryptedData, 通过ByteConverter.GetString(encryptedData)的方法转为字符串, 之后要将加密过的字符串再解密就一直不行.
我直觉的想法是将加密的字符串转为byte之后再解密, 但是执行上却出现"数据错误", 不知道是那里出了错.
try {
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes(encodeStr.Text); //encodeStr.Text 加密过后的字符串
byte[] decryptedData;
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
decryptedData = RSADecrypt(dataToEncrypt, RSA.ExportParameters(true), false);
decodeStr.Text = ByteConverter.GetString(decryptedData);
} catch (Exception er) {
MessageBox.Show("减密失败" + er.Message);
}
--------------------编程问答-------------------- http://www.cnblogs.com/sufei/archive/2010/07/22/1783168.html --------------------编程问答-------------------- 过来看看 是不是我能用一些
补充:.NET技术 , Web Services