如何在 Java 中加密/解密文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2442264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:10:13  来源:igfitidea点击:

How to encrypt/decrypt a file in Java?

javaencryptionpasswords

提问by Petike

I am writing a Java application which can "encrypt" and consequently "decrypt" whatever binary file.

我正在编写一个 Java 应用程序,它可以“加密”并因此“解密”任何二进制文件。

I am just a beginner in the "cryptography" area so I would like to write a very simple application for the beginning.

我只是“密码学”领域的初学者,所以我想为开始编写一个非常简单的应用程序。

For reading the original file, I would probably use the java.io.FileInputStreamclass to get the "array of bytes" byte originalBytes[]of the file.

为了读取原始文件,我可能会使用java.io.FileInputStream该类来获取文件的“字节数组” byte originalBytes[]

Then I would probably use some very simple cipher, for example "shift up every byte by 1" and then I would get the "encrypted" bytes byte encryptedBytes[]and let's say that I would also set a "password" for it, for example "123456789".

然后我可能会使用一些非常简单的密码,例如“将每个字节向上移动 1”,然后我会得到“加密”字节byte encryptedBytes[],假设我还会为它设置一个“密码”,例如“123456789” .

Next, when somebody wants to "decrypt" that file, he has to enter the password ("123456789") first and after that the file could be decrypted (thus "shift down every byte by 1") and consequently saved to the output file via java.io.FileOutputStream.

接下来,当有人想要“解密”该文件时,他必须先输入密码(“123456789”),然后才能解密该文件(因此“将每个字节向下移 1”),然后将其保存到输出文件中通过java.io.FileOutputStream.


I am just wondering how to "store" the password information to the encrypted file so that the decrypting application knows if the entered password and the "real" password equals?


我只是想知道如何将密码信息“存储”到加密文件中,以便解密应用程序知道输入的密码和“真实”密码是否相等?

Probably it would be silly to add the password (for example the ASCII ordinal numbers of the password letters) to the beginning of the file (before the encrypted data).

将密码(例如密码字母的 ASCII 序数)添加到文件的开头(在加密数据之前)可能很愚蠢。


So my main question is how to store the password information to the encrypted file?


所以我的主要问题是如何将密码信息存储到加密文件中

回答by Hymanbot

It would probably be easier not to check the password give by the user against a global password, rather ensure that only that one password (known by the user) decrypts the ciphertext into the correct plaintext, any other password would return gibberish. This is usually how cryptography works and means you don't have to store a centralised password anywhere.

不根据全局密码检查用户提供的密码可能会更容易,而是确保只有一个密码(用户知道)将密文解密为正确的明文,任何其他密码都会返回乱码。这通常是密码学的工作方式,这意味着您不必在任何地方存储集中式密码。

回答by duffymo

Maybe this open source library can help you:

也许这个开源库可以帮助你:

http://www.jasypt.org/

http://www.jasypt.org/

回答by duffymo

try the sample given below. u could convert the bytes to string and then encrypt and then write it to file. reverse it while decrypting.

试试下面给出的示例。您可以将字节转换为字符串,然后加密,然后将其写入文件。解密时反转它。

http://www.exampledepot.com/egs/javax.crypto/desstring.html

http://www.exampledepot.com/egs/javax.crypto/desstring.html

below u can find a sample DES enc&dec for files..

您可以在下面找到文件的示例 DES enc&dec。

http://www.exampledepot.com/egs/javax.crypto/DesFile.html

http://www.exampledepot.com/egs/javax.crypto/DesFile.html

回答by Chris Lercher

Don't store it there! Any good encryption is based on mathematical algorithms (like AES). You may want to have a look at BouncyCastle http://www.bouncycastle.org/- but encryption is not a simple topic, so you should get a good book to learn about its basics first!

不要把它存放在那里!任何好的加密都基于数学算法(如 AES)。您可能想看看 BouncyCastle http://www.bouncycastle.org/- 但加密不是一个简单的话题,所以你应该先买一本好书来了解它的基础知识!

回答by Tedil

Use the password to encrypt your data. You could for example repeat the password so that it matches the byte array's length and then do something like

使用密码加密您的数据。例如,您可以重复密码,使其与字节数组的长度匹配,然后执行类似的操作

data[i] = data[i] >> password[i];

Edit:if you wanted to store the password, you would have to encrypt it. Which - at least when using symmetrical cryptosystems - will be inherently insecure.

编辑:如果您想存储密码,则必须对其进行加密。这 - 至少在使用对称密码系统时 - 本质上是不安全的。

回答by Graphics Noob

A really simple way to use a password to encrypt is to use XOR, here is some pseudo code

使用密码加密的一个非常简单的方法是使用 XOR,这里是一些伪代码

for(byte in file)
{
    Byte newByte = byte ^ (byte) password[i];
    outputFile.write(newByte);
    i = (i + 1) password.length();
}

This is based on the identity that (x XOR y) XOR y = x, all you need to do is encrypt/decrypt with the same password.

这是基于 (x XOR y) XOR y = x 的身份,您需要做的就是使用相同的密码加密/解密。