使用AES-256和Decrypt文件将文件加密为其原始格式

时间:2020-12-19 18:25:34

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).

我必须在AES-256中构建一个加密和解密文件的项目。因此,我必须加密文件,这些文件可以是任何格式,如文本文件,图像文件,视频文件或任何格式的任何类型的文件,并且必须加密这些文件并将它们存储在不同格式的设备上,如*。 anuj(扩展名)。

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.

我的问题是1.我是否需要添加额外的字符,同时保存自定义格式的加密文件,如图像(abcd_img.anuj),文本(abcd_txt.anuj)。在解密时,我可以从图像的额外嵌入字符img,文本的txt中获取原始文件格式。这样我就可以解密为该文件格式了。

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

但这是一个庞大的过程,因为有许多格式。甚至图像有多种格式(.gif,.png,.jpg等)。

What approach should I choose before working on this project?

在开始这个项目之前,我应该选择什么方法?

1 个解决方案

#1


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.

一种方法是,当您加密的文件是“name.ext”时,简单地加密文件而不使用特殊文件格式“name.ext.anuj”。它包含重新创建原始文件的所有内容。

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".

这个问题是显示了文件名。有时像文件名这样的元数据都是攻击者需要的。想想当你的配偶找到一个文件“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.

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

#1


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.

一种方法是,当您加密的文件是“name.ext”时,简单地加密文件而不使用特殊文件格式“name.ext.anuj”。它包含重新创建原始文件的所有内容。

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".

这个问题是显示了文件名。有时像文件名这样的元数据都是攻击者需要的。想想当你的配偶找到一个文件“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.

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