在编写数据通讯应用中要面对一个主要的问题就是数据加密,这段时间查看资料后发现一般通讯采用的加密手段是对称加密和非对称加密相结合。通过非对称来加密对称的key和iv,而对称则用于加密信息本从而使加密数据更安全和高效。以下是简单两中加密的方式进行简单的封装,把非对称加直接封装到对称加密类的内部方便使用。
非对称加密类
public class RasCrypto
{
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024);
public RasCrypto()
{
}
public string PublicKey
{
get
{
return rsaProvider.ToXmlString(false);
}
set
{
rsaProvider.FromXmlString(value);
}
}
public void CopyRSAParameters(bool includePrivateParameters, RasCrypto rsa)
{
RSAParameters parameters = rsaProvider.ExportParameters(includePrivateParameters);
rsa.rsaProvider.ImportParameters(parameters);
}
public RSAParameters ExportParameters(bool includePrivateParameters)
{
return rsaProvider.ExportParameters(includePrivateParameters);
}
public void ImportParameters(RSAParameters parameters)
{
rsaProvider.ImportParameters(parameters);
}
public string PrivateKey
{
get
{
return rsaProvider.ToXmlString(true);
}
set
{
rsaProvider.FromXmlString(value);
}
}
public string Sign(string data)
{
return Convert.ToBase64String(Sign(System.Text.Encoding.ASCII.GetBytes(data)));
}
public byte[] Sign(byte[] data)
{
return rsaProvider.SignData(data, "MD5");
}
public bool Verify(string data, string signature)
{
return Verify(System.Text.Encoding.ASCII.GetBytes(data), Convert.FromBase64String(signature));
}
public bool Verify(byte[] data, byte[] Signature)
{
return rsaProvider.VerifyData(data, "MD5", Signature);
}
public string Encrypt(string data)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(data)));
}
public string Decrypt(string data)
{
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data)));
}
public byte[] Encrypt(byte[] data)
{
return rsaProvider.Encrypt(data, false);
}
public byte[] Decrypt(byte[] data)
{
return rsaProvider.Decrypt(data, false);
}
public string GetRSAParametersData(bool includePrivateParameters)
{
RSAParameters parameters = rsaProvider.ExportParameters(includePrivateParameters);
StringBuilder sb = new StringBuilder();
sb.Append(parameters.D!=null?Convert.ToBase64String(parameters.D):"").Append("\n");
sb.Append(parameters.DP != null ? Convert.ToBase64String(parameters.DP) : "").Append("\n");
sb.Append(parameters.DQ != null ? Convert.ToBase64String(parameters.DQ) : "").Append("\n");
sb.Append(parameters.Exponent != null ? Convert.ToBase64String(parameters.Exponent) : "").Append("\n");
sb.Append(parameters.InverseQ != null ? Convert.ToBase64String(parameters.InverseQ) : "").Append("\n");
sb.Append(parameters.Modulus != null ? Convert.ToBase64String(parameters.Modulus) : "").Append("\n");
sb.Append(parameters.P != null ? Convert.ToBase64String(parameters.P) : "").Append("\n");
sb.Append(parameters.Q != null ? Convert.ToBase64String(parameters.Q) : "").Append("\n");
return sb.ToString();
}
public void SetRSAParameters(string data)
{
RSAParameters rsa = new RSAParameters();
string[] values = data.Split('\n');
rsa.D = string.IsNullOrEmpty(values[0]) ? null : Convert.FromBase64String(values[0]);
rsa.DP = string.IsNullOrEmpty(values[1]) ? null : Convert.FromBase64String(values[1]);
rsa.DQ = string.IsNullOrEmpty(values[2]) ? null : Convert.FromBase64String(values[2]);
rsa.Exponent = string.IsNullOrEmpty(values[3]) ? null : Convert.FromBase64String(values[3]);
rsa.Modulus = string.IsNullOrEmpty(values[4]) ? null : Convert.FromBase64String(values[4]);
rsa.Modulus = string.IsNullOrEmpty(values[5]) ? null : Convert.FromBase64String(values[5]);
rsa.P = string.IsNullOrEmpty(values[6]) ? null : Convert.FromBase64String(values[6]);
rsa.Q = string.IsNullOrEmpty(values[7]) ? null : Convert.FromBase64String(values[7]);
rsaProvider.ImportParameters(rsa);
}
}
对称加密类
public class DESCrypto
{
private RasCrypto mPublicRas = new RasCrypto();
private RasCrypto mPrivateRas = new RasCrypto();
private DESCryptoServiceProvider key = new DESCryptoServiceProvider();
public DESCrypto()
{
}
public DESInfo KeyInfo
{
get
{
DESInfo info = new DESInfo();
info.Key = mPublicRas.Encrypt(key.Key);
info.IV = mPublicRas.Encrypt(key.IV);
return info;
}
set
{
key.Key = mPrivateRas.Decrypt(value.Key);
key.IV = mPrivateRas.Decrypt(value.IV);
}
}
public void CopyRasParameters(DESCrypto descrypto)
{
mPrivateRas.CopyRSAParameters(false, descrypto.PublicRas);
}
public RasCrypto PrivateRas
{
get
{
return mPrivateRas;
}
}
public RasCrypto PublicRas
{
get
{
return mPublicRas;
}
}
public class DESInfo
{
public byte[] Key { get; set; }
public byte[] IV { get; set; }
}
public string Encrypt(string data)
{
return Convert.ToBase64String(Encrypt(System.Text.Encoding.UTF8.GetBytes(data)));
}
public byte[] Encrypt(byte[] data)
{
MemoryStream ms = new MemoryStream();
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.Close();
byte[] buffer = ms.ToArray();
ms.Close();
return buffer;
}
public string Decrypt(string data)
{
return System.Text.Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data)));
}
public byte[] Decrypt(byte[] data)
{
IList<byte> result = new List<byte>();
int value;
MemoryStream ms = new MemoryStream(data);
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
value = encStream.ReadByte();
while (value >= 0)
{
result.Add((byte)value);
value = encStream.ReadByte();
}
encStream.Close();
ms.Close();
return result.ToArray<byte>();
}
}
类的使用
class Program
{
static void Main(string[] args)
{
TestRas();
TestDes();
Console.Read();
}
static void TestDes()
{
DESCrypto des1 = new DESCrypto();
DESCrypto des2 = new DESCrypto();
des1.CopyRasParameters(des2);
des2.CopyRasParameters(des1);
string value = "henry";
string evalue, dvalue;
des2.KeyInfo = des1.KeyInfo;
evalue = des1.Encrypt(value);
dvalue = des2.Decrypt(evalue);
Console.WriteLine("1to2 Value=" + value);
Console.WriteLine("1to2 Encrypt=" + evalue);
Console.WriteLine("1to2 Decrypt=" + dvalue);
value = "bbq";
evalue = des2.Encrypt(value);
dvalue = des1.Decrypt(evalue);
Console.WriteLine("2to1 Value=" + value);
Console.WriteLine("2to1 Encrypt=" + evalue);
Console.WriteLine("2to1 Decrypt=" + dvalue);
}
static void TestRas()
{
RasCrypto ras1 = new RasCrypto();
RasCrypto ras2 = new RasCrypto();
ras1.CopyRSAParameters(false, ras2);
string value = "henry";
string evalue, dvalue;
evalue = ras2.Encrypt(value);
dvalue = ras1.Decrypt(evalue);
Console.WriteLine("RAS Value=" + value);
Console.WriteLine("RAS Encrypt=" + evalue);
Console.WriteLine("RAS Decrypt=" + dvalue);
}
}