且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用AES-256和解密文件加密文件到其原始格式

更新时间:2023-02-17 17:27:51

如果您正在处理文件,则解密后可能需要重新创建该文件的唯一信息是文件名和文件扩展名。



这样做的一个方法是将文件按原样简单加密,而不加密文件格式为name.ext.anuj,当您加密的文件为 name.ext。它包含重新创建原始文件的所有内容。



此问题是文件名显示。有时元数据(如文件名)都是攻击者的需要。想想你的配偶何时会找到一个文件divorce.odf.anuj。



在这种情况下,您可以定义一个新的文件格式。你可以使用文件名,将其写入流中(也可以使用文件名长度 DataOutputStream 提供),然后将实际的文件内容写入 byte [] 。现在,你可以加密整个事情。当您解密时,只需从前面读取文件名,并将该文件写入剩余的解密字节。


I have to build a project for encryption and decryption of files in AES-256. So, I have to encrypt files and those files could be of any format like text file, image file, video file or any kind of file with any format, And have to encrypt those files and store them on device with different format like *.anuj (extension name).

Suppose I encrypted file and made new file with custom extension. While decryption that file how am I supposed to know that original file was text file or image or of any other format.

My question is 1. Do i need to add extra character while saving encrypted file in custom format like for image (abcd_img.anuj), for text (abcd_txt.anuj). At the time of decryption, I can get the original file format from that extra embedded character img for image, txt for text. So that i can decrypt to that file format.

But this is bulky process because there are number of formats. Even image has multiple formats (.gif, .png, .jpg etc).

What approach should I choose before working on this project?

If you're working on files, then the only information that you might need to re-create that file after decryption is the file name and file extension.

One way to do this is to simply encrypt the file as-is without a special file format as "name.ext.anuj" when the file that you encrypted was "name.ext". It contains everything to re-create the original file.

The problem with this is that the filename is shown. Sometimes meta-data such as a filename is all an attacker needs. Think about when your spouse finds a file "divorce.odf.anuj".

In those cases, you can define a new file format. You can for example take the filename, write it into a stream (maybe prepend it with the filename length which DataOutputStream provides) and write the actual file contents after that as byte[]. Now, you can encrypt the whole thing. When you decrypt it, simply read the filename from the front and write to this file the remaining decrypted bytes.