我想构建用于用Java进行加密和解密的App,但是显示错误,该如何解决?
我使用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
答案 0 :(得分:0)
您必须通过以下方式使用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());