各位好,
这几天遇到难题了,.NET的System.Security.Cryptography命名空间下有一个类DSACryptoServiceProvider,我现在需要用这个类进行加密操作,私钥我已经有了,可是每次赋值都报错,程序如下,可是每次执行到ImportParameters()时就会报错:不正确的数据。有知道的吗,望指点下。
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();
DSAParameters para1 = DSA.ExportParameters(true); string strP = "XX"; //10个字符
para1.X = Encoding.ASCII.GetBytes(strP); strP = "XX"; //128个字符
para1.P = Encoding.ASCII.GetBytes(strP); strP = "XX"; //10个字符
para1.Q = Encoding.ASCII.GetBytes(strP); strP = "XX"; //128个字符
para1.G = Encoding.ASCII.GetBytes(strP); strP = "XX"; //128个字符
para1.Y = Encoding.ASCII.GetBytes(strP); DSA.ImportParameters(para1);
这几天遇到难题了,.NET的System.Security.Cryptography命名空间下有一个类DSACryptoServiceProvider,我现在需要用这个类进行加密操作,私钥我已经有了,可是每次赋值都报错,程序如下,可是每次执行到ImportParameters()时就会报错:不正确的数据。有知道的吗,望指点下。
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();
DSAParameters para1 = DSA.ExportParameters(true); string strP = "XX"; //10个字符
para1.X = Encoding.ASCII.GetBytes(strP); strP = "XX"; //128个字符
para1.P = Encoding.ASCII.GetBytes(strP); strP = "XX"; //10个字符
para1.Q = Encoding.ASCII.GetBytes(strP); strP = "XX"; //128个字符
para1.G = Encoding.ASCII.GetBytes(strP); strP = "XX"; //128个字符
para1.Y = Encoding.ASCII.GetBytes(strP); DSA.ImportParameters(para1);
其实我就是想知道怎么给这个类的Key赋值,就是找不到Key属性在哪。
试试用base64编码,或者16进制编码。
/// 使用对称加密技术 DES 加密字符串。
/// </summary>
/// <param name="s">要加密的字符串。</param>
/// <param name="k">加密密钥(长度必须为 8 位)。</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string s, string k)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.Default.GetBytes(s);
provider.Key = Encoding.ASCII.GetBytes(k);
provider.IV = Encoding.ASCII.GetBytes(k);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
builder.ToString();
stream2.Close();
stream.Close();
return builder.ToString();
}
http://topic.csdn.net/u/20120226/19/3f8f13b8-85cd-4948-bdbd-5aa83a49eb08.html