两次 DSACryptoServiceProvider dsac2 = new DSACryptoServiceProvider();
产生的 dsac 并不一样。还需要指定一些参数。看下面msdn的范例:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemsecuritycryptographydsacryptoserviceproviderclasssigndatatopic.htmusing System;
using System.Security.Cryptography;class DSACSPSample
{
static void Main()
{
try
{
//Create a new instance of DSACryptoServiceProvider to generate
//a new key pair.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //The data to sign.
byte[] Data = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
//The value to hold the signed value.
byte[] SignedValue = DSASignData(Data, DSA.ExportParameters(true)); //Verify the data and display the results.
if(DSAVerifyData(Data, SignedValue, DSA.ExportParameters(false)))
{
Console.WriteLine("The hash value was verified.");
}
else
{
Console.WriteLine("The hash value was not verified.");
}
}
catch(ArgumentNullException e)
{
Console.WriteLine(e.Message);
}
} public static byte[] DSASignData(byte[] DataToSign, DSAParameters DSAKeyInfo)
{
try
{
//Create a new instance of DSACryptoServiceProvider.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information.
DSA.ImportParameters(DSAKeyInfo); //Compute hash value, sign the hash, and return the signed hash.
return DSA.SignData(DataToSign);
}
catch(CryptographicException e)
{
Console.WriteLine(e.Message); return null;
} } public static bool DSAVerifyData(byte[] Data, byte[] SignedData, DSAParameters DSAKeyInfo)
{
try
{
//Create a new instance of DSACryptoServiceProvider.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information.
DSA.ImportParameters(DSAKeyInfo); //Verify the signature and return the result.
return DSA.VerifyData(Data, SignedData); }
catch(CryptographicException e)
{
Console.WriteLine(e.Message); return false;
}
}}
产生的 dsac 并不一样。还需要指定一些参数。看下面msdn的范例:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemsecuritycryptographydsacryptoserviceproviderclasssigndatatopic.htmusing System;
using System.Security.Cryptography;class DSACSPSample
{
static void Main()
{
try
{
//Create a new instance of DSACryptoServiceProvider to generate
//a new key pair.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //The data to sign.
byte[] Data = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
//The value to hold the signed value.
byte[] SignedValue = DSASignData(Data, DSA.ExportParameters(true)); //Verify the data and display the results.
if(DSAVerifyData(Data, SignedValue, DSA.ExportParameters(false)))
{
Console.WriteLine("The hash value was verified.");
}
else
{
Console.WriteLine("The hash value was not verified.");
}
}
catch(ArgumentNullException e)
{
Console.WriteLine(e.Message);
}
} public static byte[] DSASignData(byte[] DataToSign, DSAParameters DSAKeyInfo)
{
try
{
//Create a new instance of DSACryptoServiceProvider.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information.
DSA.ImportParameters(DSAKeyInfo); //Compute hash value, sign the hash, and return the signed hash.
return DSA.SignData(DataToSign);
}
catch(CryptographicException e)
{
Console.WriteLine(e.Message); return null;
} } public static bool DSAVerifyData(byte[] Data, byte[] SignedData, DSAParameters DSAKeyInfo)
{
try
{
//Create a new instance of DSACryptoServiceProvider.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information.
DSA.ImportParameters(DSAKeyInfo); //Verify the signature and return the result.
return DSA.VerifyData(Data, SignedData); }
catch(CryptographicException e)
{
Console.WriteLine(e.Message); return false;
}
}}
我觉得在实际问题中也必须将加密和认证分开处理。请指点一下,怎样处理这个问题?
顺便说一下我用得是vs.net 2002,是不是与vs.net 2003 在加密的过程中有区别?
2、找找 DSACryptoServiceProvider 的构造函数说明,把一些信息初始化构造进取,这样就可以了。 我估计你是有些初始化信息没有配置,造成 两次 new 出来的不一样。出现的结果当然不一样了。
)会使用当前系统的钥匙,重装而没有备份证书的话就对不上号建议瞧瞧班班的FAQhttp://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=35935