Fork me on GitHub

C#文本加密

[原创]C#文本加密

MD5加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public string GetMD5(string sDataIn)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] bytValue, bytHash;
bytValue = Encoding.UTF8.GetBytes(sDataIn);
bytHash = md5.ComputeHash(bytValue);
md5.Clear();
string sTemp = "";
for (int i = 0; i < bytHash.Length; i++)
{
sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
}
return sTemp.ToLower();
}

SHA1加密

1
2
3
4
5
6
7
public string GetSHA1(string data)
{
var bytes = Encoding.Default.GetBytes(data);
var SHA = new SHA1CryptoServiceProvider();
var encryptbytes = SHA.ComputeHash(bytes);
return Base64To16(encryptbytes);
}

SHA256加密

1
2
3
4
5
6
7
8
9
10
11
12
13
public string GetSHA256(string data)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] hash = SHA256.Create().ComputeHash(bytes);

StringBuilder builder = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
builder.Append(hash[i].ToString("X2"));
}

return builder.ToString();
}

SHA384加密

1
2
3
4
5
6
7
public string SHA384Encrypt(string data)
{
var bytes = Encoding.Default.GetBytes(data);
var SHA384 = new SHA384CryptoServiceProvider();
var encryptbytes = SHA384.ComputeHash(bytes);
return Base64To16(encryptbytes);
}

SHA512加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public string GetSHA512(string strData)
{
byte[] bytValue = Encoding.UTF8.GetBytes(strData);
try
{
SHA512 sha512 = new SHA512CryptoServiceProvider();
byte[] retVal = sha512.ComputeHash(bytValue);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("error:" + ex.Message);
}
}

Base64加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public string Base64Encode(Encoding encodeType, string source)
{
string encode = string.Empty;
byte[] bytes = encodeType.GetBytes(source);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = source;
}
return encode;
}

DES加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static string DESEncrypt(string originalValue, string key, string iv)
{
using (DESCryptoServiceProvider sa
= new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV =Encoding.UTF8.GetBytes(iv)})
{
using (ICryptoTransform ct = sa.CreateEncryptor())
{
byte[] by = Encoding.UTF8.GetBytes(originalValue);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, ct,
CryptoStreamMode.Write))
{
cs.Write(by, 0, by.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
}

DES解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static string DESDecrypt(string encryptedValue, string key, string iv)
{
using (DESCryptoServiceProvider sa =
new DESCryptoServiceProvider
{ Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(iv) })
{
using (ICryptoTransform ct = sa.CreateDecryptor())
{
byte[] byt = Convert.FromBase64String(encryptedValue);

using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
{
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
}
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}

AES加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public string AESEncrypt(string encryptStr, string encryptKey)
{
if (string.IsNullOrWhiteSpace(encryptStr))
return string.Empty;

encryptKey = StringHelper.SubString(encryptKey, 32);
encryptKey = encryptKey.PadRight(32, ' ');

SymmetricAlgorithm des = Rijndael.Create();
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptStr);
des.Key = Encoding.UTF8.GetBytes(encryptKey);
des.IV = _aeskeys;
byte[] cipherBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cipherBytes = ms.ToArray();
cs.Close();
ms.Close();
}
}
return Convert.ToBase64String(cipherBytes);
}

AES解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public string AESDecrypt(string decryptStr, string decryptKey)
{
if (string.IsNullOrWhiteSpace(decryptStr))
return string.Empty;

decryptKey = StringHelper.SubString(decryptKey, 32);
decryptKey = decryptKey.PadRight(32, ' ');

byte[] cipherText = Convert.FromBase64String(decryptStr);

SymmetricAlgorithm des = Rijndael.Create();
des.Key = Encoding.UTF8.GetBytes(decryptKey);
des.IV = _aeskeys;
byte[] decryptBytes = new byte[cipherText.Length];
using (MemoryStream ms = new MemoryStream(cipherText))
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
{
cs.Read(decryptBytes, 0, decryptBytes.Length);
cs.Close();
ms.Close();
}
}
return Encoding.UTF8.GetString(decryptBytes).Replace("\0", "");
}