在項目開發中,始終會涉及到的一個問題,就是信息安全,在調用接口,或者加載的資源,都會涉及安全問題,因此就出現了各種各樣的加密方式。
常見的也是目前用的最廣的加密方式,分別是:DES、3DES、AES、MD5、XOR(異或)
其中DES、3DES、AES、MD5用在數據加密中偏多,特別是接口調用數據信息傳輸上。
XOR異或加密用在資源加密上偏多。
因此很多工程師會整理一個通用的加密工具類,因此我這邊也同樣整理了一個工具腳本。
但DES和3DES之前項目中沒用到過,因此還沒整理,等后續如果有碰到會再更新。
下面主要是AES、MD5、XOR加密方式
1、XOR(異或加密)
異或加密其實很簡單,就是將二進制明文數據進行異或運算,解密時,只需要對密文再次異或運算即可。異或運算原理是,相同為 0,不同為 1
因此加密代碼和解密代碼一致,但是根據密鑰可以是 單個byte,或者 多個byte,這就導致出現了,更復雜的加密方式,例如對明文數據索引的取余等等。當索引滿足什么條件時進行加密,不滿足時不進行加密。
例如下方代碼
/// <summary>
/// 異或操作
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] XorCipher(byte[] data, byte key = XorDefaultPassword)
{for (int i = 0; i < data.Length; i++){data[i] ^= key;}return data;
}/// <summary>
/// 異或操作
/// </summary>
/// <param name="data"> 數據 </param>
/// <param name="key"> 密鑰 </param>
/// <param name="isRemainder"> 是否取余(數據索引,對key長度取余,不等于0時加密) </param>
/// <returns></returns>
public static byte[] XorCipher(byte[] data, byte[] key=null,bool isRemainder = true)
{if (key == null || key.Length <= 0) key = XorArrayPassword;for (int i = 0; i < data.Length; i++){if (!isRemainder){data[i] ^= key[i % key.Length];}else{if (i % key.Length != 0){data[i] ^= key[i % key.Length];}}}return data;
}
2、MD5加密
MD5碼是個很常見的東西,特別是在做資源熱更新的,很多公司會對給資源文件一個MD5編碼,用作版本管理,判斷這個編碼是否一致,一致就不更新,不一致就更新資源。但這個編碼的計算方式也可以用來做數據的加密。
/// <summary> /// 加密數據 /// </summary> /// <param name="content"></param> /// <param name="key"></param> /// <returns></returns> public static string EncryptByMD5(string content, string key = DefaultPassword){var des = System.Security.Cryptography.DES.Create();byte[] inputByteArray;inputByteArray = System.Text.Encoding.Default.GetBytes(content);System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(System.BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.Default.GetBytes(key + "md5"))).Replace("-", null).Substring(0, 8));des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(System.BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.Default.GetBytes(key + "md5"))).Replace("-", null).Substring(0, 8));System.IO.MemoryStream ms = new System.IO.MemoryStream();System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();System.Text.StringBuilder ret = new System.Text.StringBuilder();foreach (byte b in ms.ToArray()){ret.AppendFormat("{0:X2}", b);}return ret.ToString();}/// <summary> /// 解密數據 /// </summary> /// <param name="content"></param> /// <param name="key"></param> /// <returns></returns> public static string DecryptByMD5(string content, string key = DefaultPassword){var des = System.Security.Cryptography.DES.Create();int len;len = content.Length / 2;byte[] inputByteArray = new byte[len];int x, i;for (x = 0; x < len; x++){i = System.Convert.ToInt32(content.Substring(x * 2, 2), 16);inputByteArray[x] = (byte)i;}System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(System.BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key + "md5"))).Replace("-", null).Substring(0, 8));des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(System.BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key + "md5"))).Replace("-", null).Substring(0, 8));System.IO.MemoryStream ms = new System.IO.MemoryStream();System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();return System.Text.Encoding.Default.GetString(ms.ToArray());}
3、AES加密
一度是目前實用最廣泛的加密方式,雖然也被發現了破解方式,但安全性以及效率都還是比較高的加密方式,因此還在被廣泛的使用中。
提供了幾種方法:
1、字符串加密后返回字符串
2、字符串加密后返回字節數組
3、字節數組加密后返回字符串
4、字節數組加密后返回字節數組
/// <summary>/// AES加密/// </summary>/// <param name="content"> 原文 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字節數組 </returns>public static byte[] EncryptToBytesByAes(string content, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);byte[] encrypted;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create an encryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform encryptor = aesAlg.CreateEncryptor();// Create the streams used for encryption.using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()){using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)){using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt)){//Write all data to the stream.swEncrypt.Write(content);}encrypted = msEncrypt.ToArray();}}}// Return the encrypted bytes from the memory stream.return encrypted;}/// <summary>/// AES加密/// </summary>/// <param name="content"> 原文 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字節數組 </returns>public static byte[] EncryptToBytesByAes(byte[] content, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);byte[] encrypted;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create an encryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform encryptor = aesAlg.CreateEncryptor();// Create the streams used for encryption.using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()){using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)){using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt)){//Write all data to the stream.swEncrypt.Write(content);}encrypted = msEncrypt.ToArray();}}}// Return the encrypted bytes from the memory stream.return encrypted;}/// <summary>/// AES加密/// </summary>/// <param name="content"> 原文 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字符串 </returns>public static string EncryptToStringByAes(string content, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);byte[] encrypted;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create an encryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform encryptor = aesAlg.CreateEncryptor();// Create the streams used for encryption.using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()){using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)){using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt)){//Write all data to the stream.swEncrypt.Write(content);}encrypted = msEncrypt.ToArray();}}}// Return the encrypted bytes from the memory stream.return System.Text.Encoding.Default.GetString(encrypted);}/// <summary>/// AES加密/// </summary>/// <param name="content"> 原文 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字符串 </returns>public static string EncryptToStringByAes(byte[] content, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);byte[] encrypted;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create an encryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform encryptor = aesAlg.CreateEncryptor();// Create the streams used for encryption.using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()){using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)){using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt)){//Write all data to the stream.swEncrypt.Write(content);}encrypted = msEncrypt.ToArray();}}}// Return the encrypted bytes from the memory stream.return System.Text.Encoding.Default.GetString(encrypted);}/// <summary>/// AES解密/// </summary>/// <param name="content"> 加密數據 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字節數組 </returns>public static byte[] DecryptToBytesByAes(byte[] data, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);// the decrypted text.byte[] content;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create a decryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor();// Create the streams used for decryption.using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(data)){using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)){using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt)){// Read the decrypted bytes from the decrypting stream// and place them in a string.var Text = srDecrypt.ReadToEnd();content = System.Text.Encoding.Default.GetBytes(Text);}}}}return content;}/// <summary>/// AES解密/// </summary>/// <param name="content"> 加密數據 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字節數組 </returns>public static byte[] DecryptToBytesByAes(string data, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);// the decrypted text.byte[] content;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create a decryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor();// Create the streams used for decryption.using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(data))){using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)){using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt)){// Read the decrypted bytes from the decrypting stream// and place them in a string.var Text = srDecrypt.ReadToEnd();content = System.Text.Encoding.Default.GetBytes(Text);}}}}return content;}/// <summary>/// AES解密/// </summary>/// <param name="content"> 加密數據 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字符串 </returns>public static string DecryptToStringByAes(byte[] data, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);// the decrypted text.string content = string.Empty;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create a decryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor();// Create the streams used for decryption.using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(data)){using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)){using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt)){// Read the decrypted bytes from the decrypting stream// and place them in a string.content = srDecrypt.ReadToEnd();}}}}return content;}/// <summary>/// AES解密/// </summary>/// <param name="content"> 加密數據 </param>/// <param name="password"> 密鑰 </param>/// <returns> 字節數組 </returns>public static string DecryptToStringByAes(string data, string key = DefaultPassword, string iv = DefaultPassword){var keyData = System.Text.Encoding.Default.GetBytes(key);var ivData = System.Text.Encoding.Default.GetBytes(iv);// the decrypted text.string content = string.Empty;// Create an Aes object// with the specified key and IV.using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create()){aesAlg.Key = keyData;aesAlg.IV = ivData;aesAlg.Mode = System.Security.Cryptography.CipherMode.CBC;aesAlg.Padding = System.Security.Cryptography.PaddingMode.ISO10126;// Create a decryptor to perform the stream transform.System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor();// Create the streams used for decryption.using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(data))){using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)){using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt)){// Read the decrypted bytes from the decrypting stream// and place them in a string.content = srDecrypt.ReadToEnd();}}}}return content;}
目前整理的就這幾種,之后如果有更新,再更新添加過來。