private static void Main(string[] args)
{
var keyList = RsaKeyGenerator.Pkcs1Key(2048, true);
var privateKey = keyList[0];
var publicKey = keyList[1];
var filePath = "C:\\Users\\admin\\Downloads\\1.zip";
EncryptFile(filePath,"tttt",publicKey);
DecryptFile("tttt","",publicKey,privateKey);
Environment.Exit(0);
}
private static byte[] GetByteArray(int byteSize)
{
Random rnd = new Random();
byte[] b = new byte[byteSize];
rnd.NextBytes(b);
return b;
}
public static void EncryptFile(string filePath, string encryptFilePath, string publicKey)
{
var obj = new Class1();
obj.AESKey = GetByteArray(32);
obj.AESIV = GetByteArray(16);
obj.FileName = Path.GetFileName(filePath);
var transform = Aes.Create().CreateEncryptor(obj.AESKey, obj.AESIV);
RsaPkcs1Util util = new RsaPkcs1Util(Encoding.UTF8, publicKey);
EncryptObject(obj, util);
var res = MessagePackSerializer.Serialize(obj);
var outFs = new FileStream(encryptFilePath, FileMode.OpenOrCreate);
outFs.Write(res, 0, res.Length);
var outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write);
var inFs = new FileStream(filePath, FileMode.Open);
var blockSizeBytes = 128 / 8;
var data = new byte[blockSizeBytes];
while (inFs.Read(data, 0, blockSizeBytes) is int count && count > 0)
outStreamEncrypted.Write(data, 0, count);
outStreamEncrypted.FlushFinalBlock();
inFs.Dispose();
outFs.Dispose();
}
public static void DecryptFile(string filePath, string decryptFileDirectory, string publicKey,string privateKey)
{
var inFs = new FileStream(filePath, FileMode.Open);
var streamReader = new MessagePackStreamReader(inFs);
var msg = streamReader.ReadAsync(default).GetAwaiter().GetResult();
if (msg is null) return;
var headerLength = msg.Value.Length;
var obj = MessagePackSerializer.Deserialize<Class1>((ReadOnlySequence<byte>)msg);
streamReader.Dispose();
inFs.Dispose();
RsaPkcs1Util util = new RsaPkcs1Util(Encoding.UTF8, publicKey,privateKey);
DecryptObject(obj,util);
var transform = Aes.Create().CreateDecryptor(obj.AESKey, obj.AESIV);
inFs = new FileStream(filePath, FileMode.Open);
inFs.Seek(headerLength, SeekOrigin.Begin);
var worker = new IdWorker(1, 1);
var outFilePath = Path.Combine(decryptFileDirectory, obj.FileName);
while (File.Exists(outFilePath))
outFilePath = Path.Combine(decryptFileDirectory, worker.NextId() + "_" + obj.FileName);
var outFs = new FileStream(outFilePath, FileMode.Create);
var blockSizeBytes = 128 / 8;
var data = new byte[blockSizeBytes];
var outStreamDecrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write);
while (inFs.Read(data, 0, blockSizeBytes) is int count && count > 0) outStreamDecrypted.Write(data, 0, count);
outStreamDecrypted.FlushFinalBlock();
inFs.Dispose();
outFs.Dispose();
}
public static void EncryptObject(object obj, RSAUtilBase util)
{
var properties = obj.GetType().GetProperties();
foreach (var property in properties)
{
switch (property.PropertyType.Name)
{
case "String":
var temp1 = property.GetValue(obj) as string;
temp1 = util.Encrypt(temp1, RSAEncryptionPadding.Pkcs1);
property.SetValue(obj, temp1);
break;
case "Byte[]":
var temp2 = property.GetValue(obj) as byte[];
temp2 = util.PublicRsa.Encrypt(temp2, RSAEncryptionPadding.Pkcs1);
property.SetValue(obj, temp2);
break;
}
}
}
public static void DecryptObject(object obj, RSAUtilBase util)
{
var properties = obj.GetType().GetProperties();
foreach (var property in properties)
{
switch (property.PropertyType.Name)
{
case "String":
var temp1 = property.GetValue(obj) as string;
temp1 = util.Decrypt(temp1, RSAEncryptionPadding.Pkcs1);
property.SetValue(obj, temp1);
break;
case "Byte[]":
var temp2 = property.GetValue(obj) as byte[];
temp2 = util.PrivateRsa.Decrypt(temp2, RSAEncryptionPadding.Pkcs1);
property.SetValue(obj, temp2);
break;
}
}
}
[MessagePackObject]
public class Class1
{
[Key(0)] public string FileName { get; set; }
[Key(1)] public byte[] AESKey { get; set; }
[Key(2)] public byte[] AESIV { get; set; }
}