简体   繁体   English

如何使用 AES (Rijndael) 检查文件是否加密

[英]How to Check If File is Encrypted using AES (Rijndael)

I am using 'RijndaelManaged' and 'CryptoStream' classes in C# to encrypt files.我在 C# 中使用“RijndaelManaged”和“CryptoStream”类来加密文件。 Before encrypting the files, i want to check whether the file is already encrypted or not.在加密文件之前,我想检查文件是否已经加密。

I tried using File.GetAttributes() method to check for Encryption but it is not working.我尝试使用File.GetAttributes()方法来检查加密,但它不起作用。

I need some tips on ways i can check whether the file is already Encrypted or not.我需要一些有关检查文件是否已加密的方法的提示。

Without any sort of custom headers, the only way to be absolutely sure the file is encrypted is to attempt to decrypt it.没有任何类型的自定义标头,绝对确保文件已加密的唯一方法是尝试对其进行解密。

If you attempt to compress the file and it gets smaller, then it is extremely unlikely to be encrypted.如果您尝试压缩文件并且文件变小,则它极不可能被加密。 If there is a non-uniform distribution of byte values (including plain text!), then it is unlikely to be encrypted.如果字节值(包括纯文本!)的分布不均匀,则不太可能对其进行加密。

Those heuristics depend on proper execution of the encryption.这些启发式方法取决于加密的正确执行。 If AES is applied to a file one block at time, then patters can emerge in the result, but since you are using CryptoStream this shouldn't be a problem.如果 AES 一次一个块地应用于文件,则结果中可能会出现模式,但由于您使用的是 CryptoStream,这应该不是问题。

If your own code will always be used to encrypt and decrypt the files, then you should consider adding a custom header that indicates it is an encrypted file.如果您自己的代码将始终用于加密和解密文件,那么您应该考虑添加一个自定义标头,表明它是一个加密文件。

Suppose I have a file F containing ciphertext X, which is the enciphering of plaintext Y with key Z.假设我有一个包含密文 X 的文件 F,它是用密钥 Z 对明文 Y 进行加密。

I wish to ensure that the plaintext Y can only be determined by someone who possesses both key Z and key Q. (I can think of a number of reasons why I might wish to do this.)我希望确保明文 Y 只能由同时拥有密钥 Z 和密钥 Q 的人确定。(我可以想到一些我可能希望这样做的原因。)

I therefore wish to encrypt the already-encrypted file with key Q.因此,我希望使用密钥 Q 加密已经加密的文件。

You're telling me that your system wishes to detect that F is already encrypted, and then refuse to encrypt it with key Q?你告诉我你的系统希望检测到 F 已经加密,然后拒绝用密钥 Q 加密它?

That seems like a bad idea.这似乎是个坏主意。 I might want to encrypt the file with key Q irrespective of whether it is already encrypted with key Z or not.我可能用密钥 Q 加密文件,而不管它是否已经用密钥 Z 加密。

You have to inspect the file and look for structures, or byte strings that would not be there if the file is encrypted.您必须检查文件并查找如果文件被加密就不会存在的结构或字节字符串。 You would need a separate test for every type of file you are dealing with.您需要对您处理的每种类型的文件进行单独的测试。

设置你的加密方法 bool 类型,如果文件可以解密,则该方法返回 true 表示文件已加密,否则该方法抛出异常并返回 false 表示文件无法解密,或者说文件未加密。

If the file is encrypted it will appear as a stream of random bytes.如果文件被加密,它将显示为随机字节流。 You can:你可以:

  • Attempt to open the file and/or confirm that it is of the expected format (JPG, ZIP, whatever).尝试打开文件和/或确认它是预期的格式(JPG、ZIP 等)。 If the file matches a known format then you know it is decrypted.如果文件与已知格式匹配,则您知道它已被解密。

  • Attempt to decrypt the file if you have the key, then repeat the previous step.如果您有密钥,请尝试解密文件,然后重复上一步。 If it now matches a known format then you know it is (was?) encrypted.如果它现在与已知格式匹配,那么您就知道它是(曾经?)加密的。

我建议在加密过程中用一些东西重命名加密文件,你可以在想要解密时检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM