Mat*_*man 0 java encryption exception
我已将以下内容导入到我的项目中
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
Run Code Online (Sandbox Code Playgroud)
以下是告诉我"未处理的异常类型NoSuchPaddingException"
Cipher c = Cipher.getInstance("AES");
Run Code Online (Sandbox Code Playgroud)
我正在使用JaveSE-1.6.
会有什么想法导致这种情况?
Cipher.getInstance(...)
抛出两种异常,并要求你处理它们.
Cipher c = Cipher.getInstance("AES");
如果您希望在其他地方处理异常,则要么包含重新抛出异常的方法:
public void foo(){ throws Exception
...
}
或者更好的是,将方法包含在try-catch块中:
try{
Cipher c = Cipher.getInstance("AES");
}
catch(Exception e){
//do something about it
}
Run Code Online (Sandbox Code Playgroud)
你也可以变得更加漂亮并且做到这一点:
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
}
Run Code Online (Sandbox Code Playgroud)
某些Java方法会抛出异常,其中一些需要您处理它们.Throws
在该方法需要处理之后的Java API文档中的任何内容.一般来说,他们有很好的理由让你这样做.在这种情况下,如果您无法获得正确的密码,则无法加密任何内容.