-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (101 loc) · 5.61 KB
/
Copy pathProgram.cs
File metadata and controls
108 lines (101 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("==================");
Console.WriteLine("图像加密与解密工具");
Console.WriteLine("==================");
Console.WriteLine("1: 加密 | 2: 解密");
string? choice = Console.ReadLine();
if (string.IsNullOrEmpty(choice))
{
Console.WriteLine("无效选择!");
Console.WriteLine("按任意键退出...");
Console.ReadKey();
return;
}
if (choice == "1")
{
Console.WriteLine("输入宿主图片路径(PNG):");
string? hostPath = Console.ReadLine();
Console.WriteLine("输入第一张隐藏图片路径(PNG):");
string? secret1Path = Console.ReadLine();
Console.WriteLine("输入第二张隐藏图片路径(PNG):");
string? secret2Path = Console.ReadLine();
try
{
if (string.IsNullOrEmpty(hostPath)) throw new ArgumentException("宿主图片路径不能为空");
if (string.IsNullOrEmpty(secret1Path)) throw new ArgumentException("第一张隐藏图片路径不能为空");
if (string.IsNullOrEmpty(secret2Path)) throw new ArgumentException("第二张隐藏图片路径不能为空");
if (!File.Exists(hostPath)) throw new FileNotFoundException("宿主图片不存在");
if (!File.Exists(secret1Path)) throw new FileNotFoundException("第一张隐藏图片不存在");
if (!File.Exists(secret2Path)) throw new FileNotFoundException("第二张隐藏图片不存在");
if (!hostPath.ToLower().EndsWith(".png") || !File.ReadAllBytes(hostPath).Take(8).SequenceEqual(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }))
throw new ArgumentException("宿主图片必须为有效的 PNG 文件");
if (!secret1Path.ToLower().EndsWith(".png") || !File.ReadAllBytes(secret1Path).Take(8).SequenceEqual(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }))
throw new ArgumentException("第一张隐藏图片必须为有效的 PNG 文件");
if (!secret2Path.ToLower().EndsWith(".png") || !File.ReadAllBytes(secret2Path).Take(8).SequenceEqual(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }))
throw new ArgumentException("第二张隐藏图片必须为有效的 PNG 文件");
using (Bitmap host = new Bitmap(hostPath))
using (Bitmap secret1 = new Bitmap(secret1Path))
using (Bitmap secret2 = new Bitmap(secret2Path))
{
var (encryptedImage, pwd1, pwd2) = ImageCrypto.EncryptImages(host, secret1, secret2);
string outputPath = $"encrypted_host_{DateTime.Now.Ticks}.png";
encryptedImage.Save(outputPath, ImageFormat.Png);
string pwd1Base64 = Convert.ToBase64String(pwd1);
string pwd2Base64 = Convert.ToBase64String(pwd2);
File.WriteAllText("password1.txt", pwd1Base64);
File.WriteAllText("password2.txt", pwd2Base64);
Console.WriteLine($"口令1(保存好!):{pwd1Base64}");
Console.WriteLine($"口令2(保存好!):{pwd2Base64}");
Console.WriteLine("口令已保存到 password1.txt 和 password2.txt");
}
}
catch (Exception ex)
{
Console.WriteLine($"加密失败:{ex.Message}");
}
}
else if (choice == "2")
{
Console.WriteLine("输入加密后的宿主图片路径(PNG):");
string? encryptedPath = Console.ReadLine();
Console.WriteLine("输入口令(Base64格式):");
string? password = Console.ReadLine();
try
{
if (string.IsNullOrEmpty(encryptedPath)) throw new ArgumentException("加密图片路径不能为空");
if (string.IsNullOrEmpty(password)) throw new ArgumentException("口令不能为空");
if (!File.Exists(encryptedPath)) throw new FileNotFoundException("加密图片不存在");
if (!encryptedPath.ToLower().EndsWith(".png") || !File.ReadAllBytes(encryptedPath).Take(8).SequenceEqual(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }))
throw new ArgumentException("加密图片必须为有效的 PNG 文件");
using (Bitmap encryptedHost = new Bitmap(encryptedPath))
{
byte[] pwd = Convert.FromBase64String(password);
if (pwd.Length != 16)
throw new ArgumentException("口令长度必须为 16 字节");
var (decryptedImage, imageIndex) = ImageCrypto.DecryptImage(encryptedHost, pwd);
string outputPath = $"decrypted_{imageIndex}_{DateTime.Now.Ticks}.png";
decryptedImage.Save(outputPath, ImageFormat.Png);
Console.WriteLine($",输出文件:{outputPath}");
Array.Clear(pwd, 0, pwd.Length);
}
}
catch (Exception ex)
{
Console.WriteLine($"解密失败:{ex.Message}");
}
}
else
{
Console.WriteLine("无效选择!");
}
Console.WriteLine("按任意键退出...");
Console.ReadKey();
}
}