初步完成基础功能

This commit is contained in:
Falcon 2025-10-24 13:23:01 +08:00
parent 78d1925ef5
commit ef218c4113
11 changed files with 452 additions and 47 deletions

View File

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace PrivateBox.DataContext
{
/// <summary>
/// 用户保密键
/// </summary>
public class CryptionKey:DbEntityBase
{
/// <summary>
/// 用于加密的键
/// </summary>
[Required]
public string? Key { get; set; }
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace PrivateBox.DataContext
{
/// <summary>
/// 加密项
/// </summary>
public class CrytionItem:DbEntityBase
{
/// <summary>
/// 加密项名称
/// </summary>
[Required]
[MaxLength(200)]
public string? ItemName { get; set; }
/// <summary>
/// 加密项值
/// </summary>
[SqlSugar.SugarColumn(ColumnDataType ="text")]
public string? ItemValue { get; set; }
}
}

View File

@ -1,6 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using PrivateBox.DataContext;
using SqlSugar;
using System.Runtime.CompilerServices;
using System.Text;
namespace PrivateBox
{
@ -11,24 +13,53 @@ namespace PrivateBox
{
public AppConfig Config { get; init; }
public DbContext(IServiceProvider provider,AppConfig config) : base(new ConnectionConfig {
/// <summary>
/// 数据库是否已经初始化
/// </summary>
private static bool IsInited = false;
public DbContext(AppConfig config) : base(new ConnectionConfig {
DbType = DbType.Sqlite,
ConnectionString = config.SqliteFileName,
IsAutoCloseConnection = false,
}) {
this.Config = config;
DbInit();
}
/// <summary>
/// 初始化数据库
/// </summary>
public void DbInit() {
if(IsInited) {
return;
}
this.CodeFirst.SetStringDefaultLength(200);
//db.CodeFirst.InitTables<WorkUnit>();
//db.CodeFirst.InitTables<PlusFileUnit>();
//db.CodeFirst.InitTables<WorkUnitPlusFile>();
this.CodeFirst.InitTables<CryptionKey>();
this.CodeFirst.InitTables<CrytionItem>();
IsInited = true;
}
/// <summary>
/// 获取加密用的key
/// </summary>
/// <returns>加密用的key</returns>
public string GetCrytionKey() {
var qu = this.Queryable<CryptionKey>().ToList();
if(qu.Any() && !string.IsNullOrEmpty(qu.First().Key)) {
return qu.First().Key;
}
var keyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnobqrstuvwxyz1234567890".ToArray();
StringBuilder key = new(36);
Random rd = new Random();
for(int i = 0;i < key.Capacity;i++) {
int index = (int)rd.NextInt64(0,keyChars.Length);
var k = keyChars[index];
key.Append(k);
}
this.Insertable(new CryptionKey { Key = key.ToString() }).ExecuteCommand();
return key.ToString();
}
}
}

View File

@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
namespace PrivateBox.DataContext
{
/// <summary>
/// 用户保密键
/// </summary>
public abstract class DbEntityBase
{
/// <summary>
/// 键ID
/// </summary>
[StringLength(36)]
[Key, Required]
public string Id { get; set; } = Guid.NewGuid().ToString();
/// <summary>
/// 键创建时间
/// </summary>
[Required]
public DateTime CreateTime { get; set; } = DateTime.Now;
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrivateBox.DataContext
{
internal class UserKeys
{
}
}

84
PrivateBox/InputDlgBox.Designer.cs generated Normal file
View File

@ -0,0 +1,84 @@
namespace PrivateBox
{
partial class InputDlgBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if(disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
textBox1 = new TextBox();
btOk = new Button();
btCancel = new Button();
SuspendLayout();
//
// textBox1
//
textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
textBox1.Location = new Point(12,12);
textBox1.Multiline = true;
textBox1.Name = "textBox1";
textBox1.Size = new Size(555,65);
textBox1.TabIndex = 0;
//
// btOk
//
btOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
btOk.Location = new Point(12,93);
btOk.Name = "btOk";
btOk.Size = new Size(75,23);
btOk.TabIndex = 1;
btOk.Text = "确定";
btOk.UseVisualStyleBackColor = true;
//
// btCancel
//
btCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btCancel.Location = new Point(487,93);
btCancel.Name = "btCancel";
btCancel.Size = new Size(75,23);
btCancel.TabIndex = 2;
btCancel.Text = "取消";
btCancel.UseVisualStyleBackColor = true;
//
// InputDlgBox
//
AcceptButton = btOk;
AutoScaleDimensions = new SizeF(7F,17F);
AutoScaleMode = AutoScaleMode.Font;
CancelButton = btCancel;
ClientSize = new Size(574,128);
Controls.Add(btCancel);
Controls.Add(btOk);
Controls.Add(textBox1);
Name = "InputDlgBox";
Text = "输入值";
ResumeLayout(false);
PerformLayout();
}
#endregion
private TextBox textBox1;
private Button btOk;
private Button btCancel;
}
}

36
PrivateBox/InputDlgBox.cs Normal file
View File

@ -0,0 +1,36 @@
namespace PrivateBox
{
public partial class InputDlgBox:Form
{
public InputDlgBox() {
InitializeComponent();
this.Load += InputDlgBox_Load;
}
public InputDlgBox(string title,string msg) : this() {
this.Text = title;
this.textBox1.Text = msg;
}
private void InputDlgBox_Load(object? sender,EventArgs e) {
this.btOk.Click += BtOk_Click;
}
private void BtOk_Click(object? sender,EventArgs e) {
this.DialogResult = DialogResult.OK;
this.Hide();
}
private int msg;
/// <summary>
/// 输入的信息
/// </summary>
public string Msg {
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
}
}

120
PrivateBox/InputDlgBox.resx Normal file
View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -25,13 +25,16 @@
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
toolStripContainer1 = new ToolStripContainer();
splitContainer1 = new SplitContainer();
lbKeyList = new ListBox();
menuKeyList = new ContextMenuStrip(components);
KeyListAddNew = new ToolStripMenuItem();
KeyListRemove = new ToolStripMenuItem();
splitContainer2 = new SplitContainer();
toolStrip1 = new ToolStrip();
btEdit = new ToolStripButton();
btSave = new ToolStripButton();
btCancel = new ToolStripButton();
tbMessage = new RichTextBox();
@ -41,6 +44,7 @@
splitContainer1.Panel1.SuspendLayout();
splitContainer1.Panel2.SuspendLayout();
splitContainer1.SuspendLayout();
menuKeyList.SuspendLayout();
((System.ComponentModel.ISupportInitialize)splitContainer2).BeginInit();
splitContainer2.Panel1.SuspendLayout();
splitContainer2.Panel2.SuspendLayout();
@ -81,6 +85,7 @@
//
// lbKeyList
//
lbKeyList.ContextMenuStrip = menuKeyList;
lbKeyList.Dock = DockStyle.Fill;
lbKeyList.FormattingEnabled = true;
lbKeyList.ItemHeight = 17;
@ -89,6 +94,26 @@
lbKeyList.Size = new Size(285,379);
lbKeyList.TabIndex = 0;
//
// menuKeyList
//
menuKeyList.Items.AddRange(new ToolStripItem[] { KeyListAddNew,KeyListRemove });
menuKeyList.Name = "menuKeyList";
menuKeyList.Size = new Size(101,48);
//
// KeyListAddNew
//
KeyListAddNew.Name = "KeyListAddNew";
KeyListAddNew.Size = new Size(100,22);
KeyListAddNew.Text = "新增";
KeyListAddNew.Click += KeyListAddNew_Click;
//
// KeyListRemove
//
KeyListRemove.Name = "KeyListRemove";
KeyListRemove.Size = new Size(100,22);
KeyListRemove.Text = "删除";
KeyListRemove.Click += KeyListRemove_Click;
//
// splitContainer2
//
splitContainer2.Dock = DockStyle.Fill;
@ -109,23 +134,13 @@
//
// toolStrip1
//
toolStrip1.Items.AddRange(new ToolStripItem[] { btEdit,btSave,btCancel });
toolStrip1.Items.AddRange(new ToolStripItem[] { btSave,btCancel });
toolStrip1.Location = new Point(0,0);
toolStrip1.Name = "toolStrip1";
toolStrip1.Size = new Size(467,27);
toolStrip1.TabIndex = 0;
toolStrip1.Text = "toolStrip1";
//
// btEdit
//
btEdit.DisplayStyle = ToolStripItemDisplayStyle.Text;
btEdit.Image = (Image)resources.GetObject("btEdit.Image");
btEdit.ImageTransparentColor = Color.Magenta;
btEdit.Margin = new Padding(3);
btEdit.Name = "btEdit";
btEdit.Size = new Size(36,21);
btEdit.Text = "编辑";
//
// btSave
//
btSave.DisplayStyle = ToolStripItemDisplayStyle.Text;
@ -135,6 +150,7 @@
btSave.Name = "btSave";
btSave.Size = new Size(36,21);
btSave.Text = "保存";
btSave.Click += btSave_Click;
//
// btCancel
//
@ -145,6 +161,7 @@
btCancel.Name = "btCancel";
btCancel.Size = new Size(36,21);
btCancel.Text = "取消";
btCancel.Click += btCancel_Click;
//
// tbMessage
//
@ -170,6 +187,7 @@
splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit();
splitContainer1.ResumeLayout(false);
menuKeyList.ResumeLayout(false);
splitContainer2.Panel1.ResumeLayout(false);
splitContainer2.Panel1.PerformLayout();
splitContainer2.Panel2.ResumeLayout(false);
@ -188,8 +206,10 @@
private RichTextBox tbMessage;
private SplitContainer splitContainer2;
private ToolStrip toolStrip1;
private ToolStripButton btEdit;
private ToolStripButton btSave;
private ToolStripButton btCancel;
private ContextMenuStrip menuKeyList;
private ToolStripMenuItem KeyListAddNew;
private ToolStripMenuItem KeyListRemove;
}
}

View File

@ -1,22 +1,90 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PrivateBox.DataContext;
namespace PrivateBox
{
public partial class MainForm:Form
{
public IEncryptionService Encrytion { get; init; }
public DbContext Db { get; init; }
public string CrytionKey { get; init; }
public MainForm(IServiceProvider provider) {
InitializeComponent();
this.Encrytion = provider.GetRequiredService<IEncryptionService>();
this.Db = provider.GetRequiredService<DbContext>();
this.CrytionKey = this.Db.GetCrytionKey();
this.Load += (s,d) => {
RefreshKeyList();
};
this.lbKeyList.SelectedIndexChanged += LbKeyList_SelectedIndexChanged;
}
private void LbKeyList_SelectedIndexChanged(object? sender,EventArgs e) {
this.tbMessage.Text = "";
if(this.lbKeyList.SelectedItem == null) {
return;
}
var k = this.lbKeyList.SelectedItem.ToString();
var qu = this.Db.Queryable<CrytionItem>().Where(a => a.ItemName == k).ToList();
if(qu.Any()) {
var i = qu.First();
i.ItemValue = string.IsNullOrEmpty(i.ItemValue) ? "" : this.Encrytion.Decrypt(qu.First().ItemValue,this.CrytionKey);
this.tbMessage.Text = i.ItemValue;
this.tbMessage.Tag = i;
}
}
private void RefreshKeyList() {
this.lbKeyList.Items.Clear();
var qu = this.Db.Queryable<CrytionItem>().Select(a => a.ItemName).ToList();
foreach(var i in qu) {
this.lbKeyList.Items.Add(i);
}
}
private void KeyListAddNew_Click(object sender,EventArgs e) {
var dlg = new InputDlgBox("输入键值","");
if(dlg.ShowDialog() == DialogResult.OK) {
var key = dlg.Msg;
var qu = this.Db.Queryable<CrytionItem>().Where(a => a.ItemName == key).ToList();
if(qu.Any()) {
return;
}
this.Db.Insertable(new CrytionItem {
ItemName = key,ItemValue = "",
}).ExecuteCommand();
RefreshKeyList();
}
}
private void KeyListRemove_Click(object sender,EventArgs e) {
if(this.lbKeyList.SelectedItem == null) {
return;
}
var sItem = this.lbKeyList.SelectedItem.ToString();
this.Db.Deleteable<CrytionItem>().Where(a => a.ItemName == sItem).ExecuteCommand();
RefreshKeyList();
}
private void btSave_Click(object sender,EventArgs e) {
var tag = this.tbMessage.Tag as CrytionItem;
if(tag == null) {
return;
}
tag.ItemValue = this.tbMessage.Text;
var val = this.Encrytion.Encrypt(tag.ItemValue,this.CrytionKey);
this.Db.Updateable<CrytionItem>()
.Where(a => a.ItemName == tag.ItemName)
.SetColumns(a => a.ItemValue == val)
.ExecuteCommand();
}
private void btCancel_Click(object sender,EventArgs e) {
var tag = this.tbMessage.Tag as CrytionItem;
this.tbMessage.Text = tag.ItemValue;
}
}
}

View File

@ -117,19 +117,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuKeyList.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value>
</metadata>
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="btEdit.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACRSURBVDhPY/j27dt/SjDYACcnJ7IwigEf3n8kCZNswPNb
J/+f6DYF0yA+yQac6Db5f6hWCmwIiE+mC0wIu2DS2Vf/F1x6DefjwlgNyNr34r/0wkdgTMgQDAOQNRNj
CIoBOg0rMTTDMLIhIHbriZeYBmDTiIxBGkEYxge5liQDsGGQqykyAISpZwAlmIEywMAAAAc1/Jwvt6sN
AAAAAElFTkSuQmCC
</value>
</data>
<data name="btSave.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8