public Form1() { InitializeComponent(); tempPrivateKeyFile = tempDic + "\\NyaEye_Tools_Pri_" + GetTimeStamp(); tempPublicKeyFile = tempDic + "\\NyaEye_Tools_Pub_" + GetTimeStamp(); tempAESKeyFile = tempDic + "\\NyaEye_Tools_Aes_" + GetTimeStamp(); string[] opensslPaths = Enc.getOpensslPaths(); if (opensslPaths.Length == 0) { MessageBox.Show("没有在程序目录和环境变量中找到 openssl.exe ,程序将立即退出。", "需要 OpenSSL", MessageBoxButtons.OK, MessageBoxIcon.Error); Thread thread = new Thread(() => { Thread.Sleep(100); Application.Exit(); }); thread.IsBackground = true; thread.Start(); return; } foreach (string opensslPath in opensslPaths) { comboBox1.Items.Add(opensslPath); } comboBox1.Text = comboBox1.Items[0].ToString(); Enc.opensslPath = comboBox1.Text; }
private void btnEncTxt_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在加密..."; string rText = ""; if (radioKeyMode1.Checked) { rText = Enc.encryptAESData(tempAESKeyFile, txtText.Text, false, txtSecMode.Text); } else { rText = Enc.encryptData(tempPublicKeyFile, txtText.Text); } if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "加密操作失败"; MessageBox.Show(Enc.getErrorOnce(), "加密操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "加密完成"; if (checkBase64.Checked) { byte[] rTextB64 = Encoding.Default.GetBytes(rText); rText = Convert.ToBase64String(rTextB64); } txtText.Text = rText; tabControl1.SelectedIndex = 3; } }
private void btnNewAES_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在创建对称密钥..."; string rText = Enc.getAESkey(int.Parse(txtAESKeyLength.Text)); if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "创建对称密钥失败"; MessageBox.Show(Enc.getErrorOnce(), "创建对称密钥失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "创建对称密钥完成。"; txtAES.Text = rText; tabControl1.SelectedIndex = 2; } }
private void btnNewPubKey_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在提取公钥..."; string rText = Enc.getPublicKey(txtPrivatePEM.Text); tabControl1.SelectedIndex = 1; if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "提取公钥操作失败"; MessageBox.Show(Enc.getErrorOnce(), "提取公钥操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "提取公钥完成。"; txtPublicPEM.Text = rText; } }
private void btnNewPriKey_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在创建私钥..."; string rText = Enc.genPrivateKey(int.Parse(txtKeyLength.Text)); tabControl1.SelectedIndex = 0; if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "创建私钥操作失败"; MessageBox.Show(Enc.getErrorOnce(), "创建私钥操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "创建私钥完成。"; txtPrivatePEM.Text = rText; } }
private void btnDecTxt_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在解密..."; string rText = txtText.Text; if (checkBase64.Checked) { try { byte[] rTextB64 = Convert.FromBase64String(rText); rText = Encoding.Default.GetString(rTextB64); } catch (Exception err) { toolStripStatusLabel1.Text = err.Message; MessageBox.Show(toolStripStatusLabel1.Text, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioKeyMode1.Checked) { rText = Enc.decryptionAESData(tempAESKeyFile, rText); } else { rText = Enc.decryptionData(tempPrivateKeyFile, rText); } if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "解密操作失败"; MessageBox.Show(Enc.getErrorOnce(), "解密操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "解密完成"; txtText.Text = rText; tabControl1.SelectedIndex = 3; } }
private void btnDecFile_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在解密..."; string rText = ""; if (radioKeyMode1.Checked) { rText = Enc.decryptionAESData(tempAESKeyFile, txtFrom.Text, true, "aes-256-cbc", txtTo.Text); } else { rText = Enc.decryptionData(tempPrivateKeyFile, txtFrom.Text, true, txtTo.Text); } if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "解密操作失败"; MessageBox.Show(Enc.getErrorOnce(), "解密操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "解密完成"; } }
private void btnEncFile_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = "正在加密..."; string rText = ""; if (radioKeyMode1.Checked) { rText = Enc.encryptAESData(tempAESKeyFile, txtFrom.Text, true, txtSecMode.Text, txtTo.Text); } else { rText = Enc.encryptData(tempPublicKeyFile, txtFrom.Text, true, txtTo.Text); } if (rText.Length == 0 && Enc.isErrorInfo()) { toolStripStatusLabel1.Text = "加密操作失败"; MessageBox.Show(Enc.getErrorOnce(), "加密操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { toolStripStatusLabel1.Text = "加密完成"; } }
static int Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); AppDomain appd = AppDomain.CurrentDomain; Process[] processes = Process.GetProcessesByName(Application.CompanyName); Enc.debug = debuglevel == 4; if (processes.Length > 1) { printf("已经有一个实例正在运行。"); Thread.Sleep(1000); Environment.Exit(1); } appd.ProcessExit += (s, e) => { printf("退出。"); Thread.Sleep(1000); }; Console.CancelKeyPress += delegate { printf("停止..."); screenshotThreadWorking = false; Environment.Exit(0); return; }; string fileDir = Environment.CurrentDirectory; printf("当前程序目录:" + fileDir); string temp = Environment.GetEnvironmentVariable("TEMP"); DirectoryInfo info = new DirectoryInfo(temp); string sysTempDir = info.FullName; string[] IniFileDirArr = Directory.GetFiles(fileDir, "*.ine"); string confFile = ""; if (IniFileDirArr.Length == 0) { // 沒有找到加密配置檔案,找明文配置檔案 IniFileDirArr = Directory.GetFiles(fileDir, "*.ini"); if (IniFileDirArr.Length == 0) { // 還是沒有找到 printf("找不到配置文件!", 3); return(-1); } else { confFile = IniFileDirArr[0]; } } else { // 找到加密的配置檔案,解密後加載 try { string txt = File.ReadAllText(IniFileDirArr[0]); txt = StringAES.Decrypt(txt, filePwd); tempini = sysTempDir + "\\" + randomString(64); File.WriteAllText(tempini, txt); confFile = IniFileDirArr[0]; IniFileDirArr[0] = tempini; } catch (Exception err) { printf("找不到配置文件!", 3); return(-1); } } string IniFileDir = IniFileDirArr[0]; printf("加载配置文件:" + confFile); INI ini = new INI(IniFileDir); if (!ini.ExistINIFile()) { printf("找不到配置文件!", 3); return(-1); } string[] opensslPaths = Enc.getOpensslPaths(); if (opensslPaths.Length == 0) { printf("找不到 OpenSSL !", 3); } else { Enc.opensslPath = opensslPaths[0]; printf("OpenSSL:" + opensslPaths[0]); } int screenW = Screen.PrimaryScreen.Bounds.Width; int screenH = Screen.PrimaryScreen.Bounds.Height; printf("主屏幕像素:" + screenW.ToString() + " × " + screenH.ToString()); prefix = ini.IniReadValue("User", "Prefix"); if (prefix.Length == 0) { prefix = "NyarukoEye"; } printf("文件前缀:" + prefix); tempdir = ini.IniReadValue("Work", "TempDir"); if (tempdir.Length == 0) { tempdir = sysTempDir + "\\" + prefix; } prefix += "_"; if (!Directory.Exists(tempdir)) { Directory.CreateDirectory(tempdir); } printf("临时文件夹:" + tempdir); imgType = ini.IniReadValue("File", "Type"); if (imgType.Length == 0) { imgType = "jpg"; } switch (imgType) { case "bmp": imgFormat = ImageFormat.Bmp; break; case "png": imgFormat = ImageFormat.Png; break; case "tif": imgFormat = ImageFormat.Tiff; break; case "gif": imgFormat = ImageFormat.Gif; break; default: imgFormat = ImageFormat.Jpeg; break; } printf("文件格式:" + imgType); encType = ini.IniReadValue("File", "EncType"); printf("加密文件格式:" + encType); keyType = ini.IniReadValue("File", "KeyType"); printf("对称密钥文件格式:" + keyType); encKeyType = ini.IniReadValue("File", "EncKeyType"); printf("被加密的对称密钥文件存储格式:" + encKeyType); publicKey = ini.IniReadValue("Encrypt", "PublicKey"); printf("公钥:" + publicKey); name = ini.IniReadValue("User", "Name"); if (name.Length == 0) { name = GetNumberAlpha(Environment.MachineName + Environment.UserDomainName + Environment.UserName) + "_"; } else { name = name + "_"; } symmetric = ini.IniReadValue("Encrypt", "Symmetric"); printf("对称加密算法:" + symmetric); string lengthI = ini.IniReadValue("Encrypt", "AESLength"); aesLength = int.Parse(lengthI); printf("对称加密长度:" + lengthI); printf("用户名:" + name); string sleepTimeSec = ini.IniReadValue("Work", "Sleep"); int sleepTimeSecInt = 60; if (sleepTimeSec.Length > 0) { sleepTimeSecInt = int.Parse(sleepTimeSec); sleepTime = sleepTimeSecInt * 1000; } uUsername = ini.IniReadValue("User", "Username"); printf("上载用户名:" + uUsername); uPassword = ini.IniReadValue("User", "Password"); printf("上载密码:<*>"); uURL = ini.IniReadValue("Network", "UploadURL"); printf("上载URL:" + uURL); printf("间隔时间(秒):" + sleepTimeSecInt.ToString()); sleepTimeSec = ini.IniReadValue("Work", "WorkSleep"); sleepTimeSecInt = 1; if (sleepTimeSec.Length > 0) { sleepTimeSecInt = int.Parse(sleepTimeSec); workSleepTime = sleepTimeSecInt * 1000; } printf("批量处理间隔时间(秒):" + sleepTimeSecInt.ToString()); ThreadStart screenshotRef = new ThreadStart(screenshotThreadRun); Thread screenshotThread = new Thread(screenshotRef); screenshotThread.Name = "screenshotThread" + (threadID++).ToString(); screenshotThread.Start(); printf("初始化完成。"); Application.Run(); return(0); }
static private void encryptThreadRun() { encryptThreadWorking = true; string nowThreadID = threadID.ToString(); printf("启动文件加密线程 " + nowThreadID + " ...", 0); foreach (string file in getTempFiles(imgType)) { printf("正在处理文件 " + file + " ...", 0); string[] extFileName = file.Split('.'); extFileName[extFileName.Length - 1] = keyType; string aesKey = string.Join(".", extFileName); extFileName[extFileName.Length - 1] = encType; string encFile = string.Join(".", extFileName); extFileName[extFileName.Length - 1] = encKeyType; string encKeyFile = string.Join(".", extFileName); printf("生成对称密钥 " + aesKey + " ...", 0); Enc.getAESkey(aesLength, aesKey); string err = Enc.getErrorOnce(); if (err.Length > 0) { printf(err, 3); return; } printf("使用对称密钥加密文件到 " + encFile + " ...", 0); Enc.encryptAESData(aesKey, file, true, symmetric, encFile); err = Enc.getErrorOnce(); if (err.Length > 0) { printf(err, 3); return; } printf("加密对称密钥到 " + encKeyFile + " ...", 0); Enc.encryptData(publicKey, aesKey, true, encKeyFile); err = Enc.getErrorOnce(); if (err.Length > 0) { printf(err, 3); return; } printf("删除未加密密钥 " + aesKey + " ...", 0); if (File.Exists(aesKey)) { File.Delete(aesKey); } printf("删除未加密文件 " + file + " ...", 0); if (File.Exists(file)) { File.Delete(file); } if (!netuploadThreadWorking && uURL.Length > 0) { ThreadStart encryptRef = new ThreadStart(uploadThreadRun); Thread encryptThread = new Thread(encryptRef); encryptThread.Name = "encryptThread" + (threadID++).ToString(); encryptThread.Start(); } Thread.Sleep(workSleepTime); } printf("结束文件加密线程 " + nowThreadID + " 。", 0); encryptThreadWorking = false; }