未报告异常NoSuchAlgorithmException



我试图用java生成aes密钥,我得到错误NoSuchAlgorithmException,我尝试了一切,没有任何工作。我看到别人的问题,答案就是我做了什么,我做错了什么?这是我的代码:

import javax.crypto.KeyGenerator;
import java.security.SecureRandom;
class Main {
public static String Func(byte[] bArr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; bArr != null && i < bArr.length; i++) {
String hexString = Integer.toHexString(bArr[i] & 255);
if (hexString.length() == 1) {
sb.append('0');
}
sb.append(hexString);
}
return sb.toString();
}
public static void main(String[] args) {
String[] strArr = null;
strArr = new String[2];
KeyGenerator generator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom();
generator.init(128, secureRandom);
strArr[0] = Func(generator.generateKey().getEncoded());
System.out.println(strArr[0]);
}
}

,我得到错误:

error: unreported exception NoSuchAlgorithmException; must be caught or declared to be thrown
KeyGenerator generator = KeyGenerator.getInstance("AES");

使用

public static void main(String[] args) throws Exception {

而不是

相关内容

  • 没有找到相关文章

最新更新