C# 如何从AES 256加密文件中获取文件类型
我正在尝试制作一个可以加密和解密文件的控制台应用程序。我给了加密文件一个自定义文件扩展名“.aes”(例如samplefile.aes),现在的问题是,当我解密文件时,无法识别加密文件的原始扩展名。是否有可能从AES 256加密文件中获取文件类型C# 如何从AES 256加密文件中获取文件类型,c#,encryption,cryptography,aes,C#,Encryption,Cryptography,Aes,我正在尝试制作一个可以加密和解密文件的控制台应用程序。我给了加密文件一个自定义文件扩展名“.aes”(例如samplefile.aes),现在的问题是,当我解密文件时,无法识别加密文件的原始扩展名。是否有可能从AES 256加密文件中获取文件类型 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Syst
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
namespace FileEncryption
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Environment.ExitCode = 1;
Console.Error.WriteLine(Strings.CommandlineUsage);
return;
}
bool encrypt = args[0].StartsWith("-e", StringComparison.InvariantCultureIgnoreCase);
bool decrypt = args[0].StartsWith("-d", StringComparison.InvariantCultureIgnoreCase);
if (!(encrypt || decrypt))
{
Environment.ExitCode = 1;
Console.Error.WriteLine(Strings.CommandlineUnknownMode);
return;
}
string inputname = (args.Length >= 3) ? args[2] : null;
if (inputname != null && !File.Exists(inputname))
{
Environment.ExitCode = 2;
Console.Error.WriteLine(Strings.CommandlineInputFileNotFound);
return;
}
byte[] passwordBytes = Encoding.UTF8.GetBytes(args[1]);
// Hash the password with SHA256
passwordBytes = SHA256Managed.Create().ComputeHash(passwordBytes);
try
{
if (encrypt)
{
//Encrypt file
byte[] bytesToBeEncrypted = File.ReadAllBytes(inputname);
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
//Save encrypted file
string fileEncrypted = inputname.Remove(inputname.IndexOf('.')) + ".aes";
File.WriteAllBytes(fileEncrypted, bytesEncrypted);
}
else
{
byte[] bytesToBeDecrypted = File.ReadAllBytes(inputname);
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
string file = inputname;
File.WriteAllBytes(file, bytesDecrypted);
}
Environment.ExitCode = 0;
}
catch (Exception ex)
{
Console.Error.WriteLine(string.Format(Strings.CommandlineError, ex.Message));
}
}
//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//*
//Encrypt File
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
//Decrypt File
public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用System.Security.Cryptography;
命名空间文件加密
{
班级计划
{
静态void Main(字符串[]参数)
{
如果(参数长度<2)
{
Environment.ExitCode=1;
Console.Error.WriteLine(Strings.CommandlineUsage);
返回;
}
bool encrypt=args[0]。StartsWith(“-e”,StringComparison.InvariantCultureInogoreCase);
bool decrypt=args[0]。StartsWith(“-d”,StringComparison.InvariantCultureInogoreCase);
if(!(加密| |解密))
{
Environment.ExitCode=1;
Console.Error.WriteLine(Strings.CommandlineUnknownMode);
返回;
}
字符串inputname=(args.Length>=3)?args[2]:null;
如果(inputname!=null&&!File.Exists(inputname))
{
Environment.ExitCode=2;
Console.Error.WriteLine(Strings.CommandlineInputFileNotFound);
返回;
}
byte[]passwordBytes=Encoding.UTF8.GetBytes(args[1]);
//用SHA256散列密码
passwordBytes=SHA256Managed.Create().ComputeHash(passwordBytes);
尝试
{
如果(加密)
{
//加密文件
byte[]bytesToBeEncrypted=File.ReadAllBytes(inputname);
byte[]bytesEncrypted=AES_Encrypt(bytesToBeEncrypted,passwordBytes);
//保存加密文件
string fileEncrypted=inputname.Remove(inputname.IndexOf('.'))+“.aes”;
File.writealBytes(文件加密,字节加密);
}
其他的
{
byte[]bytesToBeDecrypted=File.ReadAllBytes(inputname);
byte[]bytesDecrypted=AES_Decrypt(bytesToBeDecrypted,passwordBytes);
字符串文件=inputname;
File.writealBytes(文件,字节已解密);
}
Environment.ExitCode=0;
}
捕获(例外情况除外)
{
Console.Error.WriteLine(string.Format(Strings.CommandlineError,ex.Message));
}
}
//***********************************************************************************************
//---辅助函数---
//*
//加密文件
公共静态字节[]AES_加密(字节[]bytesToBeEncrypted,字节[]passwordBytes)
{
字节[]encryptedBytes=null;
//将盐放在这里,根据您的口味进行更换:
//salt字节必须至少为8字节。
字节[]saltBytes=新字节[]{1,2,3,4,5,6,7,8};
使用(MemoryStream ms=new MemoryStream())
{
使用(RijndaelManaged AES=new RijndaelManaged())
{
AES.KeySize=256;
AES.BlockSize=128;
var key=新的Rfc2898DeriveBytes(passwordBytes,saltBytes,1000);
AES.Key=Key.GetBytes(AES.KeySize/8);
AES.IV=key.GetBytes(AES.BlockSize/8);
AES.Mode=CipherMode.CBC;
使用(var cs=new CryptoStream(ms,AES.CreateEncryptor(),CryptoStreamMode.Write))
{
写(bytesToBeEncrypted,0,bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes=ms.ToArray();
}
}
返回encryptedBytes;
}
//解密文件
公共静态字节[]AES_解密(字节[]bytesToBeDecrypted,字节[]passwordBytes)
{
byte[]decryptedBytes=null;
//将盐放在这里,根据您的口味进行更换:
//salt字节必须至少为8字节。
字节[]saltBytes=新字节[]{1,2,3,4,5,6,7,8};
使用(MemoryStream ms=new MemoryStream())
{
使用(RijndaelManaged AES=new RijndaelManaged())
{
AES.KeySize=256;
AES.BlockSize=128;
var key=新的Rfc2898DeriveBytes(passwordBytes,saltBytes,1000);
AES.Key=Key.GetBytes(AES.KeySize/8);
AES.IV=key.GetBytes(AES.BlockSize/8);
AES.Mode=CipherMode.CBC;
使用(var cs=new CryptoStream(ms,AES.CreateDecryptor(),CryptoStreamMode.Write))
{
Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes=ms.ToArray();
}
}
返回解密字节;
}
}
}
我知道加密文件的本质不是识别原始扩展名。我要做的是,当我加密文件时,我会给它.originalextension.aes(例如picture.png.aes),然后当我解密它时,我只需要删除.aes扩展名。只是想知道这是一个好方法还是不好?如果用户要求文件类型需要通过加密隐藏或不隐藏。您可能只想保护内容,而不是文件类型。