因此,我正在为自己开发一个个人项目,并且正在尝试加密手机上的文件。这些文件可以是任何文件,例如文档,照片等。现在,我正在尝试使其正常运行。每当我运行加密时,它似乎都可以正常工作并加密文件。当我运行解密时,有时它可以工作,而其他时候则不起作用。当它失败时,我通常会收到“在确定密码时出错,填充块已损坏”错误。我也没有使用不同的测试文件,所以它不像某些文件可以工作,而其他文件则不能。我每次都尝试相同的两个文件。
public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static void decrypt(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.replace(".crypt",""));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
目前,Salt和密码是静态的,并且出于测试目的不会更改。仍然有大约一半的时间出错。
有谁知道为什么会这样吗?我一直在搜索,发现了一些可以尝试的方法,但都无济于事。
更新!
人们说得对,那是盐。当我除去盐时,问题就解决了……再多挖一点,原来是salt +
Pass是问题所在,但是因为salt是字节[],而Pass是字符串。我将salt更改为String,然后使用salt.concat(Pass),问题解决了!
也许我缺少了一些东西,但就我而言,它可以正常工作。您能否仅通过更改fileToBeCrypted,fileToBeDecrypted和fileDecryptedOutput变量来尝试以下类?
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class TestCrypt{
private static final String salt = "t784";
private static final String cryptPassword = "873147cbn9x5'2 79'79314";
private static final String fileToBeCrypted = "c:\\Temp\\sampleFile.conf";
private static final String fileToBeDecrypted = "c:\\Temp\\sampleFile.conf.crypt";
private static final String fileDecryptedOutput = "c:\\Temp\\sampleFile.conf.decrypted";
public static void main(String[] args) throws Exception
{
for (int i=0; i<100; i++)
{
encryptfile(fileToBeCrypted, cryptPassword);
decrypt(fileToBeDecrypted, cryptPassword, fileDecryptedOutput);
System.out.println(i);
}
}
public static void encryptfile(String path,String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static void decrypt(String path,String password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(outPath);
byte[] key = (salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
}
我可以多次迭代而不会出错!我正在使用Oracle JDK 1.8,但在1.7兼容模式下运行。
希望这可以帮助你。
我正在尝试构建一个程序,该程序接收一个文件(任意大小的EXE),对其进行加密并将其复制到一个结构中。然后稍后对其进行解密,并确保其与使用时相同。 我有一个艰难的时间加密,然后解密文件。它似乎没有正确加密,我不知道如何测试它。 以下是我的问题: 我在这里做错了什么? 是否有更好的库使用AES加密?或者我应该坚持openSSL 让我们说我想用另一个键说“你好世界”。我能不能用这个字符串作为加密算法的参
我试图在Android和PHP端使用AES加密/解密数据,并累犯空答案。 首先,我在Android中生成了对称密钥: 在服务器端,我试图解密数据。我可以解密(从RSA)秘密的AES密钥,并得到它的字符串表示。在客户端(Android)和服务器端(PHP)上是一样的。但是如何使用这个字符串AES密钥来解密数据呢?我尝试了这个(PHP): PHP中的结果: 怎么啦?
问题内容: 有没有一个很好的示例,说明如何在Android上使用AES 加密和解密图像及其他文件? 问题答案: 并像这样调用它们: 这应该可行,我现在在项目中使用类似的代码。
问题内容: 我尝试使用以下代码来加密1 GB的文件。但是Node.js中止,并显示“致命错误:JS分配失败-进程内存不足”。我该如何处理? 问题答案: 您可以将加密的文件写回到磁盘,而不是将整个内容缓存在内存中:
我正在使用这样的代码来加密文件。 但是,尽管此代码成功加密。txt和。xml文件,它不适用于其他文件类型,例如。docx或图像文件格式。我可以对代码进行哪些更改以将功能扩展到所有此类文件类型?
我在php中用AES加密法加密了文件,代码如下。 现在我正在尝试在Android中解密它,但我总是面临InvalidKey异常:密钥长度不是128/192/256位错误。这是Android代码: 有人能建议我怎么做吗?任何帮助都将不胜感激。