gpt4 book ai didi

java - 文件解密失败 : javax. crypto.BadPaddingException:填充 block 已损坏

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:09 24 4
gpt4 key购买 nike

我在我的电脑上创建了一个 ZIP 文件(我用 finder 中的 os x zipper 对其进行了压缩),然后我用我的 java 程序对其进行了加密:

public class ResourceEncrypter {

static byte[] salt = { (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99 };

public static void main(String[] args) {
new ResourceEncrypter().encryptAllFiles();
System.out.println("Okay, done");
}



private byte[] getKey() {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(salt);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();
return key;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}


private void encryptAllFiles() {
try {
byte[] key = getKey();

//Take a list of files and encrypt each file...
String srcFilesPath = System.getProperty("user.dir") + "/srcFiles";
String encryptedSrcFilesPath = System.getProperty("user.dir") + "/encryptedSrcFiles";
File[] listOfFiles = new File(srcFilesPath).listFiles();
for(int i = 0; i < listOfFiles.length; ++i) {
if(listOfFiles[i].getAbsolutePath().contains(".zip")) {
//Encrypt this file!
byte[] data = Files.readAllBytes(Paths.get(listOfFiles[i].getAbsolutePath()));
byte[] encryptedData = ResourceEncrypter.encrypt(key, data);

String filename = listOfFiles[i].getName();
System.out.println("Write result to " + encryptedSrcFilesPath + "/" + filename);
FileOutputStream output = new FileOutputStream(encryptedSrcFilesPath + "/" + filename);
output.write(encryptedData);
output.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}



private static byte[] encrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encryptedData = cipher.doFinal(data);
return encryptedData;
}

因此,这会对任何 zip 文件进行加密并将结果保存到另一个文件夹。

现在,我有了一个 Android 应用程序,并将加密的 zip 文件放入 Assets 文件夹 main/assets/pic.zip.encrypted 中。

在我的 Android 应用程序中,我执行以下操作:

public class MainActivity extends AppCompatActivity {

static byte[] salt = { (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99 };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
decryptZipFile();
}


private byte[] getKey() {
try {
//Create the key for the encryption/decryption
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(salt);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();
return key;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}


private static byte[] decrypt(byte[] key, byte[] encryptedData) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encryptedData);
return decrypted;
}


public void decryptZipFile() {
// First decrypt the zip file
try {
InputStream is = getResources().getAssets().open("pics.zip.encrypted");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = is.read(buffer)) != -1)
baos.write(buffer, 0, count);
byte[] encryptedData = baos.toByteArray();

byte[] decryptedData = decrypt(getKey(), encryptedData);
} catch(Exception e) {
e.printStackTrace();
}
}

当我现在尝试使用此代码解密我的 zip 文件时,出现以下错误:

09-23 18:41:21.117  30799-30799/demo.zip.app.zipapp W/System.err﹕ javax.crypto.BadPaddingException: pad block corrupted
09-23 18:41:21.117 30799-30799/demo.zip.app.zipapp W/System.err﹕ at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:854)
09-23 18:41:21.117 30799-30799/demo.zip.app.zipapp W/System.err﹕ at javax.crypto.Cipher.doFinal(Cipher.java:1340)
09-23 18:41:21.117 30799-30799/demo.zip.app.zipapp W/System.err﹕ at demo.zip.app.zipapp.MainActivity.decrypt(MainActivity.java:63)

但是,当我在我的电脑上应用相同的解密方法时,它工作正常。

这里发生了什么?

最佳答案

好的,我找到了解决方案。

问题在于 SecureRandom,尤其是 setSeed 方法在 Android 上的工作方式与在普通 Java 上的工作方式不同。

因此,您不应该像我上面那样从盐 key 构造 key 。相反,您应该转到 PC 并从 private byte[] getKey() 作为 Base64 字符串对象获取 key。然后在我的例子中, key 看起来像这样:n9dReP+BPwHWGCLpDQe+MQ==,然后将其复制粘贴到 android 方法中,变成这样:

private byte[] getKey() {
byte[] key = Base64.decode(new String("n9dReP+BPwHWGCLpDQe+MQ==").getBytes(), 0);
return key;
}

就是这样。

关于java - 文件解密失败 : javax. crypto.BadPaddingException:填充 block 已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32745324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com