使用AES-256和解密文件加密文件到其原始格式 [英] Encrypt file with AES-256 and Decrypt file to its original format

查看:681
本文介绍了使用AES-256和解密文件加密文件到其原始格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须建立一个AES-256文件加密和解密项目。
所以,我必须加密文件,这些文件可以是任何格式,如文本文件,图像文件,视频文件或任何格式的任何文件,
和必须加密这些文件和存储他们在具有不同格式的设备上,如 *。anuj (扩展名)。



假设我加密了文件,并使用自定义扩展名制作了新文件。
虽然解密该文件我应该如何知道原始文件是文本文件或图像或任何其他格式。



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



但是这是庞大的过程,因为有很多格式。即使图像有多种格式(.gif,.png,.jpg等)。



在开展此项目之前,我应该选择哪种方式?

解决方案

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



这样做的一个方法是将文件按原样简单加密,而不加密文件格式为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.

这篇关于使用AES-256和解密文件加密文件到其原始格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆