gpt4 book ai didi

java - 密码解密问题 : javax. crypto.IllegalBlockSizeException:解密中最后一个 block 不完整

转载 作者:行者123 更新时间:2023-12-02 10:40:14 36 4
gpt4 key购买 nike

我必须在android中加密和解密文件,并且加密的文件(图像,视频)也可以从其他设备解密。

我在同一设备中进行加密和解密,工作正常,但是当我切换设备用户时,加密文件进行解密,它在 doFinal() 中显示错误

javax.crypto.IllegalBlockSizeException:解密中最后一个 block 不完整

有任何方法可以从一台设备加密文件并访问所有其他设备,例如 android 中的密码加密。并使用该密码 key 访问其他设备,我们可以访问文件信息。

//代码

private boolean encrypt() {
try {
String path = Environment.getExternalStorageDirectory() + File.separator + "Download/tools.png";
String pathe = Environment.getExternalStorageDirectory() + File.separator + "Download/tools.png";
byte[] fileData = FileUtils.readFile(path);
byte[] encodedBytes = EncryptDecryptUtils.encode(EncryptDecryptUtils.getInstance(this).getSecretKey(), fileData);
FileUtils.saveFile(encodedBytes, pathe);
return true;
} catch (Exception e) {
Toast.makeText(this, "File Encryption failed.\nException: " + e.getMessage(), Toast.LENGTH_SHORT).show();

}
return false;
}

/**
* Decrypt and return the decoded bytes
*
* @return
*/
private byte[] decrypt() {

try {
String pathe = Environment.getExternalStorageDirectory() + File.separator + "Download/tools.png";
byte[] fileData = FileUtils.readFile(pathe);
byte[] decryptedBytes = EncryptDecryptUtils.decode(EncryptDecryptUtils.getInstance(this).getSecretKey(), fileData);
return decryptedBytes;
} catch (Exception e) {
Toast.makeText(this, "File Decryption failed.\nException: " + e.getMessage(), Toast.LENGTH_SHORT).show();

}
return null;
}

下面是EncryptDecryptUtils类的代码

public static byte[] encode(SecretKey yourKey, byte[] fileData)
throws Exception {
byte[] data = yourKey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(data, 0, data.length, EncoDecode.KEY_SPEC_ALGORITHM);
Cipher cipher = Cipher.getInstance(EncoDecode.CIPHER_ALGORITHM, EncoDecode.PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
return cipher.doFinal(fileData);
}
public static byte[] decode(SecretKey yourKey, byte[] fileData)
throws Exception {
byte[] decrypted;
Cipher cipher = Cipher.getInstance(EncoDecode.CIPHER_ALGORITHM, EncoDecode.PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(new byte[cipher.getBlockSize()]));
Log.d("value ", "decode() returned: " + cipher.toString());
decrypted = cipher.doFinal(fileData);
Log.d("", "decode() returned: " + decrypted.length);
return decrypted;
}



public SecretKey getSecretKey() {
String encodedKey = "8qkWUsFfdY8yy5lIad4rjw==";
byte[] decodedKey = Base64.decode(encodedKey, Base64.NO_WRAP);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, EncoDecode.KEY_SPEC_ALGORITHM);
return originalKey;
}

最佳答案

您在解密时收到 IllegalBlockSizeException,因为您提供给解密的数据不是加密密码 block 大小的倍数。 block 模式下的加密不会产生 block 大小非法的数据,因此您可以高度确定地得出结论,您的数据从离开加密到进入解密期间已以某种方式损坏。

您可以通过在 encode(..) 方法中打印 return cipher.doFinal(fileData); 的输出大小以及decode(..) 中的 fileData 位于 decrypted = cipher.doFinal(fileData); 之前,它们应该是相同的,而且是密码 block 大小的倍数。

找出数据不相同的原因,只需进行调试,直到找到罪魁祸首。

关于java - 密码解密问题 : javax. crypto.IllegalBlockSizeException:解密中最后一个 block 不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52967487/

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