using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;namespace Project1
{
/// <summary>
/// encrypt 的摘要说明。
/// </summary>
public class AsyEncrypt
{
public RSACryptoServiceProvider RSA;
public RSAParameters PrivateKey;
public RSAParameters PublicKey; public AsyEncrypt()
{
RSA = new RSACryptoServiceProvider();
PrivateKey = new RSAParameters();
PublicKey = new RSAParameters();
PublicKey = RSA.ExportParameters(false);
PrivateKey = RSA.ExportParameters(true);
}
public byte[] Encrypt(byte[] eData)
{
return RSA.Encrypt(eData,false);
} public byte[] Encrypt(byte[] eData,byte[] pubKey, byte[] pubIv)
{
PublicKey.Modulus = pubKey;
PublicKey.Exponent = pubIv;
RSA.ImportParameters(PublicKey); return RSA.Encrypt(eData,false);
}
public byte[] Decrypt(byte[] dData)
{
try
{
return RSA.Decrypt(dData,false);
}
catch(Exception e)
{
e.Message.ToString();
return null;
} } }
public class main
{
public static void Main(string[] args)
{
byte[] a = new byte[8],b = new byte[8],da,db,ea,eb; for (int i=0;i<8;i++)
{
a[i] = (byte)i;
b[i] = (byte)i;
} AsyEncrypt asy = new AsyEncrypt(); RSAParameters pubkey = asy.PublicKey;
RSAParameters priKey = asy.PrivateKey;
AsyEncrypt asy2 = new AsyEncrypt(); da = asy2.Encrypt(a,pubkey.Modulus,pubkey.Exponent);
db = asy2.Encrypt(b,pubkey.Modulus,pubkey.Exponent);
ea = asy.Decrypt(da); //这一块数据解密正常
eb = asy.Decrypt(db); //这一块就解的不对!!!
}
}}//附上源码,兄弟们将就着看一下!!!
using System.Security.Cryptography;
using System.IO;
using System.Text;namespace Project1
{
/// <summary>
/// encrypt 的摘要说明。
/// </summary>
public class AsyEncrypt
{
public RSACryptoServiceProvider RSA;
public RSAParameters PrivateKey;
public RSAParameters PublicKey; public AsyEncrypt()
{
RSA = new RSACryptoServiceProvider();
PrivateKey = new RSAParameters();
PublicKey = new RSAParameters();
PublicKey = RSA.ExportParameters(false);
PrivateKey = RSA.ExportParameters(true);
}
public byte[] Encrypt(byte[] eData)
{
return RSA.Encrypt(eData,false);
} public byte[] Encrypt(byte[] eData,byte[] pubKey, byte[] pubIv)
{
PublicKey.Modulus = pubKey;
PublicKey.Exponent = pubIv;
RSA.ImportParameters(PublicKey); return RSA.Encrypt(eData,false);
}
public byte[] Decrypt(byte[] dData)
{
try
{
return RSA.Decrypt(dData,false);
}
catch(Exception e)
{
e.Message.ToString();
return null;
} } }
public class main
{
public static void Main(string[] args)
{
byte[] a = new byte[8],b = new byte[8],da,db,ea,eb; for (int i=0;i<8;i++)
{
a[i] = (byte)i;
b[i] = (byte)i;
} AsyEncrypt asy = new AsyEncrypt(); RSAParameters pubkey = asy.PublicKey;
RSAParameters priKey = asy.PrivateKey;
AsyEncrypt asy2 = new AsyEncrypt(); da = asy2.Encrypt(a,pubkey.Modulus,pubkey.Exponent);
db = asy2.Encrypt(b,pubkey.Modulus,pubkey.Exponent);
ea = asy.Decrypt(da); //这一块数据解密正常
eb = asy.Decrypt(db); //这一块就解的不对!!!
}
}}//附上源码,兄弟们将就着看一下!!!
简单的fix: public byte[] Encrypt(byte[] eData,byte[] pubKey, byte[] pubIv)
{
PublicKey.Modulus = new byte[pubKey.Length];
PublicKey.Exponent = new byte[pubIv.Length];
Array.Copy(pubKey, PublicKey.Modulus, PublicKey.Modulus.Length);
Array.Copy(pubIv, PublicKey.Exponent, PublicKey.Exponent.Length);
RSA.ImportParameters(PublicKey);
return RSA.Encrypt(eData,false);
}
但是你的程序从结构上来说不是很好,变量PublicKey使用的比较混淆,建议修改一下。
另有一问题,为什么ImportParameters要改变输入参数呢?
再有就是说一下现在的msdn,关于ImportParameters,什么都没有说!什么帮助啊!越来越差了!
下面附上另一实现方法,与大家分享。我觉的也不错。
public class AsyEncrypt
{
public RSACryptoServiceProvider RSAProvider; // RSA加密算法
private RSAParameters pubKey; // 存储公钥
private string pubKeyString; // 公钥字符串 public AsyEncrypt()
{
RSAProvider = new RSACryptoServiceProvider();
pubKey = RSAProvider.ExportParameters(false);
pubKeyString = RSAProvider.ToXmlString(false);
} // 用已知公钥私钥对重新初始化RSAParameters
public void ReInitializeWithPubKey(RSAParameters keyInfo)
{
RSAProvider.ImportParameters(keyInfo); pubKey = RSAProvider.ExportParameters(false);
pubKeyString = RSAProvider.ToXmlString(false);
} // 用已知的公钥私钥对字符串重新初始化RSA
public void ReInitialize(string keyString)
{
RSAProvider.FromXmlString(keyString); pubKey = RSAProvider.ExportParameters(false);
pubKeyString = RSAProvider.ToXmlString(false);
} // 获取公钥和私钥对
public RSAParameters GetKey()
{
return RSAProvider.ExportParameters(true);
} // 取得公钥私钥对的字符串形式
public string GetKeyString()
{
return RSAProvider.ToXmlString(true);
} // 取得公钥
public RSAParameters GetPubKey()
{
return pubKey;
} // 取得公钥字符串
public string GetPubKeyString()
{
return pubKeyString;
} // 加密数据
public byte[] Encrypt(byte[] eData)
{
byte[] result;
try
{
result = RSAProvider.Encrypt(eData, false);
return result;
}
catch(CryptographicException e)
{
return null;
}
} // 解密数据
public byte[] Decrypt(byte[] dData)
{
byte[] result;
try
{
result = RSAProvider.Decrypt(dData, false);
return result;
}
catch(Exception e)
{
return null;
}
}
}
希望在下一版中能够Fix吧。