Reputation: 322
I have imported the following into my project
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
The following is telling me "Unhandled exception type NoSuchPaddingException"
Cipher c = Cipher.getInstance("AES");
I'm using JaveSE-1.6.
Any ideas what would be causing this?
Upvotes: 0
Views: 2333
Reputation: 1365
Cipher.getInstance(...)
throws two kinds of exceptions, and requires that you handle them.
Either have the method containing Cipher c = Cipher.getInstance("AES");
rethrow the exception if you wish to handle it elsewhere:
public void foo(){ throws Exception
...
}
Or better yet, enclose the method in a try-catch block:
try{
Cipher c = Cipher.getInstance("AES");
}
catch(Exception e){
//do something about it
}
You can also get fancier and do this:
try{
Cipher c = Cipher.getInstance("AES");
}
catch(NoSuchAlgorithmException e){
//handle the case of having no matching algorithm
}
catch(NoSuchPaddingException e){
//handle the case of a padding problem
}
Certain Java methods throw exceptions, and some of these require that you handle them. Anything in the Java API docs with Throws
after the method requires handling. Generally there is a good reason they make you do this. In this case, if you can't get the correct cipher, you can't encrypt anything.
Upvotes: 5