using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace PdmSwPlugin.Common.Util { public class BinUtil { /// /// 把明文密码加密后以二进制存入bin文件中 /// TODO 使用机器码加密 /// /// 字符内容 /// 存放路径 public static void SaveStrToBin(string content, string filePath = null) { if (string.IsNullOrEmpty(filePath)) { filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName), "bin"); } byte[] bytes = Convert.FromBase64String(content); using (Stream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(bytes); } } /// /// 从bin中加载字符串 /// /// 字符串内容 /// bin文件路径 /// 读取是否成功 public static bool LoadStrFromBin(ref string content, string filePath = null) { if (string.IsNullOrEmpty(filePath)) { filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName), "bin"); } if (!File.Exists(filePath)) { return false; } using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) using (BinaryReader reader = new BinaryReader(stream)) { byte[] bytes = new byte[stream.Length]; reader.Read(bytes, 0, bytes.Length); content = Convert.ToBase64String(bytes); return true; } } } }