急,为什么RSA解密总报"Bad key" 错误,请高手帮忙?????

GavinLi 2004-04-30 03:08:39
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LicenseMachine
{
/// <summary>
/// Summary description for Encrypt.
/// </summary>
public class Encrypt
{
private const string FILE_NAME = "License.key";
private const string s_public_key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";

public Encrypt()
{}
/// <summary>
/// encrypt method
/// </summary>
/// <param name="Source">to encrypt</param>
/// <returns>the encypted string</returns>
private string EncrypData(string Source)
{
byte[] bytIn = ASCIIEncoding.ASCII.GetBytes(Source);
byte[] bytPulicKey = ASCIIEncoding.ASCII.GetBytes(s_public_key);
byte[] Exponent = {1,0,1};

//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Create a new instance of RSAParameters.
RSAParameters RSAKeyInfo = new RSAParameters();

//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = bytPulicKey;
RSAKeyInfo.Exponent = Exponent;

//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);

byte[] bytOut = RSA.Encrypt(bytIn,false);
return Convert.ToBase64String(bytOut);
}

/// <summary>
/// save encrypt string to a file
/// </summary>
/// <param name="s_to_encrypt"></param>
public void SaveToFile(string s_to_encrypt)
{
FileStream fs = null;
BinaryWriter bw_writer = null;

if ( System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory()+"\\license.key")== true )
{
fs = new FileStream(FILE_NAME,FileMode.Open);
bw_writer = new BinaryWriter(fs);
bw_writer.Write(EncrypData(s_to_encrypt));
}
else
{
fs = new FileStream(FILE_NAME,FileMode.CreateNew);
bw_writer = new BinaryWriter(fs);

bw_writer.Write(EncrypData(s_to_encrypt));
}

bw_writer.Close();
fs.Close();
}

public bool DecryptData(string s_comparet)
{
byte[] bytPulicKey = ASCIIEncoding.ASCII.GetBytes(s_public_key);
byte[] Exponent = {1,0,1};
byte[] byt_data;
int i_len = 0;
string s = "";
FileStream fs = null;
BinaryReader bw_reader = null;

if ( System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory()+"\\license.key")== true )
{
fs = new FileStream(FILE_NAME,FileMode.Open);
bw_reader = new BinaryReader(fs);
i_len = (int)fs.Length;
byt_data = new byte[i_len];

bw_reader.Read(byt_data,0,i_len);
}
else
{
bw_reader.Close();
fs.Close();
return false;
}

bw_reader.Close();
fs.Close();

//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Create a new instance of RSAParameters.
RSAParameters RSAKeyInfo = new RSAParameters();

//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = bytPulicKey;
RSAKeyInfo.Exponent = Exponent;

//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);

s = ASCIIEncoding.ASCII.GetString(RSA.Decrypt(byt_data,false));
if ( s != s_comparet )
{
return false;
}

return true;

}
}
}
...全文
86 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
佣工7001 2004-06-08
呵呵,我也正在疑惑这个问题,看到有书上说,.net的RSA算法,总是需要RSAParameters的那些辅助字段,可是如果和别的程序交换,只有D、E、N的时候,是不是就没法使用呢?请高人来说说吧!
  • 打赏
  • 举报
回复
GavinLi 2004-04-30
上面的加密类实现 使用RSA加密,使用公钥解密,私钥加密(这里我还比较含糊,因为我并不知道是不是执行Ecrypt()函数时系统自动使用私钥加密)。
我现在设置s_public_key 为公钥,为什么用同样的RSAParameters加密可以,但是解密不行,请高手指点如何使用RSA公钥解密,私钥加密????
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-04-30 03:08
社区公告

让您成为最强悍的C#开发者