From 99a1527555cc281f1391a7a30b8c4f7c566d7cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=87=E9=98=B3=20=E9=82=B9?= Date: Sat, 23 Dec 2023 11:17:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Elight.Utility/Encrypt/DataEncryption.cs | 175 +++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 Elight.Utility/Encrypt/DataEncryption.cs 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; + } + } +}