我正在尝试读取大小为 1KB 的文件,然后在 CBC 模式下使用 AES 算法对其进行加密和解密。当我尝试初始化密码时,它会引发错误。请在下面找到代码。我在密码类中找不到一个 init 方法,它接受“加密模式”、“密钥”和类 IvParameterSpec 的初始化向量。我可以看到带有期望参数的 init 方法,例如(int 加密模式、密钥密钥、AlgorithmParameters algoParameters、SecureRandom secureRandom)
我是否需要将我的密钥和初始化向量转换为所需的类。任何进一步推进的见解都会有所帮助。
import sun.security.provider.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.io.File;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidParameterSpecException;
public class AESFileEncryptionDecryption {
public class AES128CBC{
SecretKey secretKey;
Cipher cipher;
SecureRandom secureRandom = new SecureRandom();
byte[] iv = new byte[16];
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
{
try {
secretKey = KeyGenerator.getInstance("AES").generateKey();
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(1,secretKey,ivParameterSpec);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
File inputFile_1KB = new File("/Users/siddharthsinha/Desktop/input1KB.txt");
File encryptedFile_1KB = new File("/Users/siddharthsinha/Desktop/input1KB.encrypted");
File decryptedFile_1KB = new File("/Users/siddharthsinha/Desktop/input1KB.decrypted.txt");
}
}