public class DASymmetrihelper
{
private string _signData;
private string _priateKey;
private string _publicKey;
public string SignData
{
get
{
return _signData;
}
set
{
this._signData = value;
}
}
public string PublicKey
{
get
{
return this._publicKey;
}
set { this._publicKey = value; }
}
public string PrivateKey
{
get
{
return this._priateKey;
}
}
/// <summary>
/// 产生私钥和公钥
/// </summary>
/// <param name="xmlKeys">私钥</param>
/// <param name="xmlPublicKey">公钥</param>
private void GenerateKey(DSACryptoServiceProvider dsa)
{
this._priateKey = dsa.ToXmlString(true);
this._publicKey = dsa.ToXmlString(false);
}
public string DSADecrypt(string dencryptString)
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider();
dsa.FromXmlString(this.PublicKey);
byte[] fileHashValue = new SHA1CryptoServiceProvider().ComputeHash(Convert.FromBase64String(dencryptString));//产生散列
string[] splitstr = this.SignData.Split('-');
byte[] signedHash = new byte[splitstr.Length];
for (int i = 0; i < signedHash.Length; i++)
signedHash[i] = byte.Parse(splitstr[i], System.Globalization.NumberStyles.AllowHexSpecifier);
bool flage = dsa.VerifySignature(fileHashValue, signedHash);
if (flage)
{
return "解密后数据";//如何实现
}
return false;
}
public string DSAEncrypt(string encryptstring)
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider();
GenerateKey(dsa);//产生私钥和公钥
byte[] source = System.Text.UTF8Encoding.UTF8.GetBytes(encryptstring);
byte[] fileHashValue = new SHA1CryptoServiceProvider().ComputeHash(source);//产生散列
this._signData = BitConverter.ToString(dsa.SignData(fileHashValue)); //生成摘要[数字签名]
dsa.FromXmlString(this.PrivateKey);//私钥对消息摘要加密
return "都返回那些值?,如何对消息进行加密?";//怎么返回值呀
}
}