using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.Security;
namespace Utils.Files
{
public class SafeConfig
{
///
/// 向指定的加密文件中添加键值对。
/// 如果键已经存在,则为更新
/// 如果文件不存在,则创建一个。路径必须存在
///
/// 路径与文件名
/// 密码。8位:不够则补全,多了则截断
/// 键
/// 值
public static void AddAndSaveConfig(string fileName_pathAndFileName, string password, string key, string value)
{
password = CheckPassword(password);
var configs = GetAllConfigs(fileName_pathAndFileName, password);
if (configs.Keys.Contains(key))
configs[key] = value;
else
configs.Add(key, value);
Save(configs, fileName_pathAndFileName, password);
}
///
/// 向指定的加密文件中添加键值对。
/// 如果键已经存在,则为更新
/// 如果文件不存在,则创建一个。路径必须存在
///
/// 路径与文件名
/// 密码。8位:不够则补全,多了则截断
/// 要添加的键值对
public static void AddAndSaveConfig(string fileName_pathAndFileName, string password, Dictionary configs)
{
password = CheckPassword(password);
var all = GetAllConfigs(fileName_pathAndFileName, password);
foreach (var item in configs) {
if (all.Keys.Contains(item.Key))
all[item.Key] = item.Value;
else
all.Add(item.Key, item.Value);
}
Save(all, fileName_pathAndFileName, password);
}
///
/// 获取加密文件的键值对信息。【文件可以不存在】
///
/// 文件路径和文件名
/// 密码。8位:不够则补全,多了则截断
/// 键值对信息
public static Dictionary GetAllConfigs(string fileName_pathAndFileName, string password)
{
Dictionary all = new Dictionary();
var value = GetDecryptConfigFile(fileName_pathAndFileName, password);
var configs = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in configs) {
if (item.StartsWith(Separator))//忽略键为空的行
continue;
if (!item.Contains(Separator))//忽略没有分隔符的行
continue;
var pairs = item.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
if (pairs.Count() > 0) {
all.Add(pairs.ElementAt(0), pairs.Count() > 1 ? pairs.ElementAt(1) : string.Empty);
}
}
return all;
}
///
/// 将键值对加密后保存。密码必须为8位。
/// 文件不存在则创建。路径必须存在。
///
/// 键值对
/// 文件路径和文件名
/// 8位密码
private static void Save(Dictionary configs, string fileName_pathAndFileName, string password)
{
string file = string.Empty;
foreach (var item in configs) {
file += item.Key + Separator + item.Value + Environment.NewLine;
}
EncryptSave(fileName_pathAndFileName, password, file);
}
///
/// 获取解密文件
///
/// 文件路径和文件名
/// 密码。8位:不够则补全,多了则截断
/// 解密后的文件
public static string GetDecryptConfigFile(string fileName_pathAndFileName, string password)
{
password = CheckPassword(password);
var source = ReadWriteFile.ReadFile(fileName_pathAndFileName);
if (string.IsNullOrEmpty(source))
return string.Empty;
return SecurityUtil.ChangeBack(source, password);
}
///
/// 将指定内容,加密后保存为一个文件(如果存在,则重写;否则新建)
///
/// 文件路径和文件名
/// 密码。8位:不够则补全,多了则截断
/// 要加密并保存的文件
public static void EncryptSave(string fileName_pathAndFileName, string password, string content)
{
var saveFile = SecurityUtil.Change(content, password);
ReadWriteFile.WriteFile(fileName_pathAndFileName, saveFile);
}
private static string CheckPassword(string password)
{
if (string.IsNullOrEmpty(password))
return DefaultPassword;
if (password.Length == 8)
return password;
if (password.Length > 8)
return password.Substring(0, 8);
return password + DefaultPassword.Substring(0, 8 - password.Length);
}
private static string DefaultPassword = "ρ☉〩$℡№◆↙";//必须为8位。一经使用,不许更改
private static string Separator = "⊙丷▼☆↘ωα";
}
}