MD5数据加密
1.MD5概述:
MD5是一种数据加密的算法,可以用于数据的加密,文件快传,文件校验,数据压缩等方面。
MD5还被称之为数据摘要算法,或数据指纹算法。
特点:
- 经过加密后的数据,是不能被破解的,无法得到原有的明文内容。
- 经过加密的数据,都是128位2进制数据组成。通常会把它书写成32位16进制数据。
- 任何形式的数据,经过加密之后,都会变为二进制数据,长度为32位16进制数据。
- 同一份数据经过md5加密之后,一定会得到同一个结果
AES加密
AES是一个对称密码,旨在取代DES成为广泛使用的标准。
高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥。
AES为分组密码,分组密码也就是把明文分成一组一组的,每组长度相等,每次加密一组数据,直到加密完整个明文。在AES标准规范中,分组长度只能是128位,也就是说,每个分组为16个字节(每个字节8位)。密钥的长度可以使用128位、192位或256位。密钥的长度不同,推荐加密轮数也不同。
/// <summary>
/// 加密
/// </summary>
/// <param name="encryptString">待加密字符串</param>
/// <returns></returns>
public static string EncryptAES(string encryptString)
{
byte[] key = new byte[16]; // 秘钥长度一定要是16个字节
string filename = "../txt.key";
byte[] ikey = File.ReadAllBytes(filename);
for (int k = 0; k < ikey.Length && k < key.Length; k++) key[k] = ikey[k];
byte[] txt = System.Text.Encoding.Default.GetBytes(encryptString);
byte[] data = new byte[2048];
for (int k = 0; k < txt.Length; k++) data[k] = txt[k];
string encrypt = null;
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(key, ikey), CryptoStreamMode.Write))
{
cStream.Write(data, 0, data.Length);
cStream.FlushFinalBlock();
encrypt = Convert.ToBase64String(mStream.ToArray());
}
}
aes.Clear();
return encrypt;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="encryptString">待解密字符串</param>
/// <returns></returns>
public static string DecryptAES(string encryptString)
{
//byte[] bKey = Encoding.UTF8.GetBytes(Key);
//byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] key = new byte[16]; // 秘钥长度一定要是16个字节
string filename = "../txt.key";
byte[] ikey = File.ReadAllBytes(filename);
for (int k = 0; k < ikey.Length && k < key.Length; k++) key[k] = ikey[k];
byte[] byteArray = Convert.FromBase64String(encryptString);
string decrypt = null;
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(key, ikey), CryptoStreamMode.Write))
{
cStream.Write(byteArray, 0, byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
aes.Clear();
return decrypt;
}
另一种方式
/// <summary>
/// 加密
/// </summary>
/// <param name="str"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string EncryptAES2(string str, byte[] Key)
{
MemoryStream mStream = new MemoryStream();
byte[] plainBytes = Encoding.GetEncoding("UTF-8").GetBytes(str);
//byte[] bKey = HexStr2Byte(Key); //将十六进制字符串解码为byte[]
RijndaelManaged aes = new RijndaelManaged //访问Rigndael算法的托管版本,并设置对称算法的密钥、模式、填充模式
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
Key = Key
};
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
try
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
/// <summary>
/// 解密
/// </summary>
/// <param name="str"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string DecryptAES2(string str, byte[] Key)
{
MemoryStream mStream = new MemoryStream();
byte[] plainBytes = Convert.FromBase64String(str);
//byte[] bKey = HexStr2Byte(Key); //将十六进制字符串解码为byte[]
RijndaelManaged aes = new RijndaelManaged
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
Key = Key
};
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Write);
try
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
Base64编码,去掉等号特殊字符
/// <summary>自定义包含指定字符的base64工具</summary>
internal static class Base64Helper
{
static readonly string base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
static readonly int[] base64Index = new int[]
{
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,63,-1,-1,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,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,-1,
-1,-1,-1,62,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,
43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,-1
};
public static byte[] FromBase64String(string inData)
{
int inDataLength = inData.Length;
int lengthmod4 = inDataLength % 4;
int calcLength = (inDataLength - lengthmod4);
byte[] outData = new byte[inDataLength / 4 * 3 + 3];
int j = 0;
int i;
int num1, num2, num3, num4;
for (i = 0; i < calcLength; i += 4, j += 3)
{
num1 = base64Index[inData[i]];
num2 = base64Index[inData[i + 1]];
num3 = base64Index[inData[i + 2]];
num4 = base64Index[inData[i + 3]];
outData[j] = (byte)((num1 << 2) | (num2 >> 4));
outData[j + 1] = (byte)(((num2 << 4) & 0xf0) | (num3 >> 2));
outData[j + 2] = (byte)(((num3 << 6) & 0xc0) | (num4 & 0x3f));
}
i = calcLength;
switch (lengthmod4)
{
case 3:
num1 = base64Index[inData[i]];
num2 = base64Index[inData[i + 1]];
num3 = base64Index[inData[i + 2]];
outData[j] = (byte)((num1 << 2) | (num2 >> 4));
outData[j + 1] = (byte)(((num2 << 4) & 0xf0) | (num3 >> 2));
j += 2;
break;
case 2:
num1 = base64Index[inData[i]];
num2 = base64Index[inData[i + 1]];
outData[j] = (byte)((num1 << 2) | (num2 >> 4));
j += 1;
break;
}
Array.Resize(ref outData, j);
return outData;
}
public static string ToBase64String(byte[] inData)
{
int inDataLength = inData.Length;
int outDataLength = (int)(inDataLength / 3 * 4) + 4;
char[] outData = new char[outDataLength];
int lengthmod3 = inDataLength % 3;
int calcLength = (inDataLength - lengthmod3);
int j = 0;
int i;
for (i = 0; i < calcLength; i += 3, j += 4)
{
outData[j] = base64Table[inData[i] >> 2];
outData[j + 1] = base64Table[((inData[i] & 0x03) << 4) | (inData[i + 1] >> 4)];
outData[j + 2] = base64Table[((inData[i + 1] & 0x0f) << 2) | (inData[i + 2] >> 6)];
outData[j + 3] = base64Table[(inData[i + 2] & 0x3f)];
}
i = calcLength;
switch (lengthmod3)
{
case 2:
outData[j] = base64Table[inData[i] >> 2];
outData[j + 1] = base64Table[((inData[i] & 0x03) << 4) | (inData[i + 1] >> 4)];
outData[j + 2] = base64Table[(inData[i + 1] & 0x0f) << 2];
j += 3;
break;
case 1:
outData[j] = base64Table[inData[i] >> 2];
outData[j + 1] = base64Table[(inData[i] & 0x03) << 4];
j += 2;
break;
}
return new string(outData, 0, j);
}
public static string Base64Encode(string source)
{
byte[] barray = Encoding.Default.GetBytes(source);
return Base64Helper.ToBase64String(barray);
}
public static string Base64Decode(string source)
{
byte[] barray = Base64Helper.FromBase64String(source);
return Encoding.Default.GetString(barray);
}
}
DES数据加密