-3

im trying to generate an aes key with java and i get the error NoSuchAlgorithmException, i tried everything and nothing worked. ive seen questions of other people and the answer was exactly what i did, what i did wrong? this is my code:

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]);
  }
}

and i get the error:

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

1 Answers1

1

Use

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

Instead

Grim
  • 1,938
  • 10
  • 56
  • 123