#region RSA加密 解密
/// <summary>RSA加密
///
/// </summary>
/// <param name="plaintext">明文</param>
/// <param name="publicKey">公钥</param>
/// <returns>密文字符串</returns>
public static string EncryptByRSA(string plaintext, string publicKey)
{
try
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes(plaintext);
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKey);
byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false);
return Convert.ToBase64String(encryptedData);
}
}
catch (Exception)
{
return null;
}
}
/// <summary> RSA解密
///
/// </summary>
/// <param name="ciphertext">密文</param>
/// <param name="privateKey">私钥</param>
/// <returns>明文字符串</returns>
public static string DecryptByRSA(string ciphertext, string privateKey)
{
try
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKey);
byte[] encryptedData = Convert.FromBase64String(ciphertext);
byte[] decryptedData = RSA.Decrypt(encryptedData, false);
return byteConverter.GetString(decryptedData);
}
}
catch (Exception)
{
return null;
}
}
/// <summary>生成RSA加密 解密的 密钥
/// 生成的key就是 方法EncryptByRSA与DecryptByRSA用的key了
/// </summary>
/// <param name="path">要生成的密钥文件的路径(文件夹)</param>
public static void getRSAKey(string path)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string datetimestr = System.DateTime.Now.ToString("yyyyMMddHHmmss");
using (StreamWriter writer = new StreamWriter("RSA解密_PrivateKey_" + datetimestr + ".xml")) //这个文件要保密...
{
writer.WriteLine(rsa.ToXmlString(true));
}
using (StreamWriter writer = new StreamWriter("RSA加密_PublicKey_" + datetimestr + ".xml"))
{
writer.WriteLine(rsa.ToXmlString(false));
}
}
#endregion
#region Base64加密解密
/// <summary>
/// Base64是一種使用64基的位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。
/// 這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 ,
/// 這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的
/// 系統中而不同。
/// Base64加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Base64Encrypt(string str)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(encbuff);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Base64Decrypt(string str)
{
byte[] decbuff = Convert.FromBase64String(str);
return System.Text.Encoding.UTF8.GetString(decbuff);
}
#endregion
#region MD5
/// <summary>
/// 获得32位的MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetMD5_32(string input)
{
MD5 md5 = MD5.Create();
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.AppendFormat("{0:X2}", data[i]);
}
return sb.ToString();
}
/// <summary>
/// 获得16位的MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetMD5_16(string input)
{
return GetMD5_32(input).Substring(8, 16);
}
/// <summary>
/// 获得8位的MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetMD5_8(string input)
{
return GetMD5_32(input).Substring(8, 8);
}
/// <summary>
/// 获得4位的MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetMD5_4(string input)
{
return GetMD5_32(input).Substring(8, 4);
}
public static string MD5EncryptHash(String input)
{
MD5 md5 = new MD5CryptoServiceProvider();
//the GetBytes method returns byte array equavalent of a string
byte[] res = md5.ComputeHash(Encoding.Default.GetBytes(input), 0, input.Length);
char[] temp = new char[res.Length];
//copy to a char array which can be passed to a String constructor
Array.Copy(res, temp, res.Length);
//return the result as a string
return new String(temp);
}
#endregion
#region MD5签名验证
/// <summary>
/// 对给定文件路径的文件加上标签
/// </summary>
/// <param name="path">要加密的文件的路径</param>
/// <returns>标签的值</returns>
public static bool AddMD5(string path)
{
bool IsNeed = true;
if (CheckMD5(path)) //已进行MD5处理
IsNeed = false;
try
{
FileStream fsread = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] md5File = new byte[fsread.Length];
fsread.Read(md5File, 0, (int)fsread.Length); // 将文件流读取到Buffer中
fsread.Close();
if (IsNeed)
{
string result = MD5Buffer(md5File, 0, md5File.Length); // 对Buffer中的字节内容算MD5
byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result); // 将字符串转换成字节数组以便写人到文件中
FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
fsWrite.Write(md5File, 0, md5File.Length); // 将文件,MD5值 重新写入到文件中。
fsWrite.Write(md5, 0, md5.Length);
fsWrite.Close();
}
else
{
FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
fsWrite.Write(md5File, 0, md5File.Length);
fsWrite.Close();
}
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 对给定路径的文件进行验证
/// </summary>
/// <param name="path"></param>
/// <returns>是否加了标签或是否标签值与内容值一致</returns>
public static bool CheckMD5(string path)
{
try
{
FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] md5File = new byte[get_file.Length]; // 读入文件
get_file.Read(md5File, 0, (int)get_file.Length);
get_file.Close();
string result = MD5Buffer(md5File, 0, md5File.Length - 32); // 对文件除最后32位以外的字节计算MD5,这个32是因为标签位为32位。
string md5 = System.Text.Encoding.ASCII.GetString(md5File, md5File.Length - 32, 32); //读取文件最后32位,其中保存的就是MD5值
return result == md5;
}
catch
{
return false;
}
}
/// <summary>
/// 计算文件的MD5值
/// </summary>
/// <param name="MD5File">MD5签名文件字符数组</param>
/// <param name="index">计算起始位置</param>
/// <param name="count">计算终止位置</param>
/// <returns>计算结果</returns>
private static string MD5Buffer(byte[] MD5File, int index, int count)
{
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(MD5File, index, count);
string result = System.BitConverter.ToString(hash_byte);
result = result.Replace("-", "");
return result;
}
#endregion
#region SHA256加密算法
/// <summary>
/// SHA256函数
/// </summary>
/// <param name="str">原始字符串</param>
/// <returns>SHA256结果(返回长度为44字节的字符串)</returns>
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
#endregion
#region RC4加密 解密
/// <summary>RC4加密算法
/// 返回进过rc4加密过的字符
/// </summary>
/// <param name="str">被加密的字符</param>
/// <param name="ckey">密钥</param>
public static string EncryptRC4wq(string str, string ckey)
{
int[] s = new int[256];
for (int i = 0; i < 256; i++)
{
s[i] = i;
}
//密钥转数组
char[] keys = ckey.ToCharArray();//密钥转字符数组
int[] key = new int[keys.Length];
for (int i = 0; i < keys.Length; i++)
{
key[i] = keys[i];
}
//明文转数组
char[] datas = str.ToCharArray();
int[] mingwen = new int[datas.Length];
for (int i = 0; i < datas.Length; i++)
{
mingwen[i] = datas[i];
}
//通过循环得到256位的数组(密钥)
int j = 0;
int k = 0;
int length = key.Length;
int a;
for (int i = 0; i < 256; i++)
{
a = s[i];
j = (j + a + key[k]);
if (j >= 256)
{
j = j % 256;
}
s[i] = s[j];
s[j] = a;
if (++k >= length)
{
k = 0;
}
}
//根据上面的256的密钥数组 和 明文得到密文数组
int x = 0, y = 0, a2, b, c;
int length2 = mingwen.Length;
int[] miwen = new int[length2];
for (int i = 0; i < length2; i++)
{
x = x + 1;
x = x % 256;
a2 = s[x];
y = y + a2;
y = y % 256;
s[x] = b = s[y];
s[y] = a2;
c = a2 + b;
c = c % 256;
miwen[i] = mingwen[i] ^ s[c];
}
//密文数组转密文字符
char[] mi = new char[miwen.Length];
for (int i = 0; i < miwen.Length; i++)
{
mi[i] = (char)miwen[i];
}
string miwenstr = new string(mi);
return miwenstr;
}
/// <summary>RC4解密算法
/// 返回进过rc4解密过的字符
/// </summary>
/// <param name="str">被解密的字符</param>
/// <param name="ckey">密钥</param>
public static string DecryptRC4wq(string str, string ckey)
{
int[] s = new int[256];
for (int i = 0; i < 256; i++)
{
s[i] = i;
}
//密钥转数组
char[] keys = ckey.ToCharArray();//密钥转字符数组
int[] key = new int[keys.Length];
for (int i = 0; i < keys.Length; i++)
{
key[i] = keys[i];
}
//密文转数组
char[] datas = str.ToCharArray();
int[] miwen = new int[datas.Length];
for (int i = 0; i < datas.Length; i++)
{
miwen[i] = datas[i];
}
//通过循环得到256位的数组(密钥)
int j = 0;
int k = 0;
int length = key.Length;
int a;
for (int i = 0; i < 256; i++)
{
a = s[i];
j = (j + a + key[k]);
if (j >= 256)
{
j = j % 256;
}
s[i] = s[j];
s[j] = a;
if (++k >= length)
{
k = 0;
}
}
//根据上面的256的密钥数组 和 密文得到明文数组
int x = 0, y = 0, a2, b, c;
int length2 = miwen.Length;
int[] mingwen = new int[length2];
for (int i = 0; i < length2; i++)
{
x = x + 1;
x = x % 256;
a2 = s[x];
y = y + a2;
y = y % 256;
s[x] = b = s[y];
s[y] = a2;
c = a2 + b;
c = c % 256;
mingwen[i] = miwen[i] ^ s[c];
}
//明文数组转明文字符
char[] ming = new char[mingwen.Length];
for (int i = 0; i < mingwen.Length; i++)
{
ming[i] = (char)mingwen[i];
}
string mingwenstr = new string(ming);
return mingwenstr;
}
#endregion
}
}