I am working on an Android project in Eclipse that uses encryption. The code contains the following line:
Cipher en = Cipher.getInstance("AES/ECB/NOPADDING");
Eclipse complains about the line of code with: "Unhandled exception type NoSuchAlgorithmException" and "Unhandled exception type NoSuchPaddingException".
I wrote a simple encrypt/decrypt java program with a text editor outside Eclipse that contained that line of code. The program compiled and ran from the command line correctly, didn't throw any exceptions, and encrypted and decrypted properly.
This tells me that there is a provider properly installed on the machine that supports that algorithm and padding but for some reason Eclipse doesn't see or cant find some necessary file. I've tried looking through various directories and changed Eclipse's search paths several times with no luck. Any ideas?
If you look at the documentation for Cipher.getInstance, you'll notice that it throws the exceptions that you mentioned. Surround your reference with a try/catch block (eclipse should provide this as a solution if you hover your mouse over the highlighted error).
Example:
try {
Cipher en = Cipher.getInstance("AES/ECB/NOPADDING");
//use the cipher
//...
} catch (NoSuchAlgorithmException e) {
//handle exception
// ex: e.printStackTrace(); System.exit(1);
} catch (NoSuchPaddingException e) {
//handle exception
} finally {
//optional, use this block if necessary
}
Java requires that you handle checked exceptions, so you should be sure to properly use try/catch blocks in your code.
Exception Basics
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With