gpt4 book ai didi

java - JCE 填充未正确加密/解密

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

我正在开发一个程序,我将使用该程序使用 JCE 来加密和解密文件。我的加密和解密在默认模式 (ECB/PKCS5PADDING) 下正常工作,但是当我尝试使用 CBC 并解密我的文件时,我得到的一些文本是垃圾(或者当我尝试图像时它被损坏。

有人能看出我做错了什么吗? (我没有包含我的导入,如果需要可以添加)

public class encwork {
private static String keyString = "ykHySDZCWr16TVku"; //Encryption key
private static void bulkWork(int cipherMode, File inputFile, File outputFile) throws Exception{
//Let the user enter the key they wish to use
Key secretKey = new SecretKeySpec(keyString.getBytes(), "AES"); //Generates a key based on the default keysize for the specified algorithm

//Generate an Initialization Vector (IV)
final int ALG_KEYLENGTH = 128; //Change this as desired for the security level you want
byte[] iv = new byte[ALG_KEYLENGTH / 8]; //Save the IV bytes or send it in plaintext with the encrypted data so you can decrypt the data later
SecureRandom prng = new SecureRandom(); //Use SecureRandom to generate random bits. The size of the IV matches the blocksize of the cipher
prng.nextBytes(iv); //Construct the appropriate IvParameterSpec object for the data to pass to Cipher's init() method

//Create a Cipher by specifying the following parameters: Alg name, Mode (CBC), Padding (PKC7/PKCS5)
Cipher cipherForEncryption = Cipher.getInstance("AES/CBC/PKCS5PADDING"); // Must specify the mode explicitly as most JCE providers default to ECB mode

//Initialize the Cipher for Encryption
cipherForEncryption.init(cipherMode, secretKey, new IvParameterSpec(iv));

//Declare / Initialize the Data, Convert the Input to Bytes and encrypt or decrypt using doFinal.
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipherForEncryption.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
}

public static void main(String[] args) {
File inputFile = new File("C:/Users/admin/Desktop/Crypto/In/test.txt");
File encryptedFile = new File("C:/Users/admin/Desktop/Crypto/Enc/test.encrypted");
File decryptedFile = new File("C:/Users/admin/Desktop/Crypto/Dec/testdec.txt");

//Encryption
try {
encwork.encrypt(inputFile, encryptedFile); //Encrypt method
} catch (Exception e) {
e.printStackTrace(); //Will show what caused the error in the console if an error occurs
}

//Decryption
try {
encwork.decrypt(encryptedFile, decryptedFile); //Decrypt method
} catch (Exception e) {
e.printStackTrace(); //Will show what caused the error in the console if an error occurs
}
}

public static void encrypt(File inputFile, File outputFile) throws Exception {
bulkWork(Cipher.ENCRYPT_MODE, inputFile, outputFile); //ENC_MODE = Constant used to initialize cipher to encryption mode.
}

public static void decrypt(File inputFile, File outputFile) throws Exception {
bulkWork(Cipher.DECRYPT_MODE, inputFile, outputFile); //ENC_MODE = Constant used to initialize cipher to encryption mode.
}}

最佳答案

您没有使用相同的 IV 进行加密和解密。

从解密开始的评论来看:“第一行是“çQßs}@ L¤qMä]这是一个测试”,这意味着加密和解密的 IV 不同。

这条评论说明了一切:

//保存 IV 字节或将其与加密数据一起以明文形式发送,以便稍后解密数据

或者:

  1. 通过从加密中返回 IV 并在解密时将其传入来保存 IV

  • 在加密数据前添加 IV 前缀,并在解密时将其拆分以用于解密。 (IV不需要保密)
  • 有关 IV 和 CBC 模式的更多信息,请参阅 Cipher Block Chaining (CBC) :

    虽然 IV 会影响整个加密数据,但 CBC 模式是 self 纠正的,这意味着当使用错误的 IV 进行解密时,只有第一个 block 是不正确的。

    关于java - JCE 填充未正确加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907092/

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