diff --git a/Elight.Utility/Encrypt/DataEncryption.cs b/Elight.Utility/Encrypt/DataEncryption.cs new file mode 100644 index 0000000..b87f403 --- /dev/null +++ b/Elight.Utility/Encrypt/DataEncryption.cs @@ -0,0 +1,175 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Elight.Utility.Encrypt +{ + public sealed class DataEncryption + { + private static byte[] KEY = new byte[8] { 40, 16, 37, 37, 32, 62, 83, 60 }; + + private static byte ByteXor(byte b, byte key) + { + b = (byte)(b ^ key); + return b; + } + + private static byte[] ByteXor(byte[] data, byte[] key) + { + var keyLen = key.Length; + var dataLen = data.Length; + if (dataLen == 0) + { + return data; + } + for (var i = 0; i < dataLen; i++) + { + data[i] = ByteXor(data[i], key[i % keyLen]); + } + return data; + } + + /// + /// 加密,key必须是2的n次方 + /// + public static byte[] Encryption(byte[] data, byte[] key) + { + return ByteXor(data, key); + } + + /// + /// 加密, 使用内置密钥 + /// + public static byte[] Encryption(byte[] data) + { + return ByteXor(data, KEY); + } + + /// + /// 加密文件 + /// + /// 明文文件 + /// 密文文件 + /// 密钥 + public static void Encryption(string srcFile, string descFile, byte[] key) + { + var fs = File.OpenRead(srcFile); + var newfs = File.Create(descFile, 1024 * 512); + var count = 0; + var keyLen = key.Length; + + var buffer = new byte[1024 * 512]; + using (fs) + { + using (newfs) + { + if (fs.Length == 0) + { + return; + } + while (fs.Position < fs.Length) + { + count = fs.Read(buffer, 0, 1024 * 512); + for (var i = 0; i < count; i++) + { + buffer[i] = ByteXor(buffer[i], key[i % keyLen]); + } + newfs.Write(buffer, 0, count); + } + } + } + } + + /// + /// 加密文件, 使用内置密钥 + /// + /// 明文文件 + /// 密文文件 + public static void Encryption(string srcFile, string descFile) + { + Encryption(srcFile, descFile, KEY); + } + + /// + /// 解密 + /// + public static byte[] Decryption(byte[] data, byte[] key) + { + return ByteXor(data, key); + } + + /// + /// 解密, 使用内置密钥 + /// + public static byte[] Decryption(byte[] data) + { + return ByteXor(data, KEY); + } + + /// + /// 解密文件 + /// + /// 密文文件 + /// 解密后的文件 + /// 密钥 + public static void Decryption(string srcFile, string descFile, byte[] key) + { + Encryption(srcFile, descFile, key); + } + + /// + /// 解密文件, 使用内置密钥 + /// + /// 密文文件 + /// 解密后的文件 + public static void Decryption(string srcFile, string descFile) + { + Decryption(srcFile, descFile, KEY); + } + + /// + /// 根据文件路径判断是否加密文件 + /// + /// 文件路径 + /// + public static bool JudgeIsEncryFile(string srcFile) + { + return srcFile.LastIndexOf(".encry") > 0; + } + + public static string Decryptiones(string srcFile) + { + var fs = File.OpenRead(srcFile); + //var newfs = File.Create(descFile, 1024 * 512); + var newfs = new MemoryStream(); + var count = 0; + var keyLen = KEY.Length; + + var buffer = new byte[1024 * 512]; + using (fs) + { + using (newfs) + { + if (fs.Length == 0) + { + return null; + } + while (fs.Position < fs.Length) + { + count = fs.Read(buffer, 0, 1024 * 512); + for (var i = 0; i < count; i++) + { + buffer[i] = ByteXor(buffer[i], KEY[i % keyLen]); + } + newfs.Write(buffer, 0, count); + } + } + } + + var base64 = Convert.ToBase64String(newfs.ToArray()); + return base64; + } + } +}