gpt4 book ai didi

java - 如何修复此错误?我想构建应用程序加密和解密文件,但显示错误如何修复它?

转载 作者:行者123 更新时间:2023-12-01 22:05:50 27 4
gpt4 key购买 nike

我想用java构建用于加密和解密的应用程序,但显示错误,我该如何修复它?

我使用 AES 算法进行加密,并且使用 java,我该如何修复此代码?

这是我的代码 CryptoException.java

public class CryptoException extends Exception {
public CryptoException() {}

public CryptoException(String message, Throwable throwable) {
super(message, throwable);
}
}

这是我的代码 CryptoUtils.java

public class CryptoUtils {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";

public static void encrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);

}

public static void decrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}

private static void doCrypto(int cipherMode, String key, File inputFile, File outputFile) throws CryptoException {
try {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, secretKey);

FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);

byte[] outputBytes = cipher.doFinal(inputBytes);

FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);

inputStream.close();
outputStream.close();

} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException ex) {
throw new CryptoException("Error Encrypting/Decrypting file", ex);
}
}
}

这是我的 MainApp.java

public class App {

public static void main( String[] args ) {
String key = "Rahasialah";
File inputFile = new File("D:\\document.txt");
File encryptedFile = new File("D:\\document.encrypted");
File decryptedFile = new File("D:\\document.decrypted");

try {
CryptoUtils.encrypt(key, inputFile, encryptedFile);
CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
} catch (CryptoException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}

这是我的消息错误:

Error Encrypting/Decrypting file
com.fusi24.bms.EncryptdanDecrypt.CryptoException: Error

Encrypting/Decrypting file
at com.fusi24.bms.EncryptdanDecrypt.CryptoUtils.doCrypto(CryptoUtils.java:53)
at com.fusi24.bms.EncryptdanDecrypt.CryptoUtils.encrypt(CryptoUtils.java:23)
at com.fusi24.bms.EncryptdanDecrypt.App.main(App.java:20)
Caused by: java.security.InvalidKeyException: Invalid AES key length: 10 bytes
at com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87)
at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:94)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:591)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:467)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
at javax.crypto.Cipher.init(Cipher.java:1248)
at javax.crypto.Cipher.init(Cipher.java:1185)
at com.fusi24.bms.EncryptdanDecrypt.CryptoUtils.doCrypto(CryptoUtils.java:36)
... 2 more

最佳答案

您必须按以下方式使用 AES 加密。

KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128); // AES key size in number of bits
SecretKey secKey = generator.generateKey();

如上所述,大小至少应为 128 位。然后你必须使用下面的代码来加密。

Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] byteCipherText = aesCipher.doFinal(someTxtToEncrypt.getBytes());

关于java - 如何修复此错误?我想构建应用程序加密和解密文件,但显示错误如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58688750/

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