Java AES: No installed provider supports this key: javax.crypto.spec.SecretKeySpec

后端 未结 3 1308
無奈伤痛
無奈伤痛 2020-12-31 09:40

I\'m trying to set up 128 bit AES encryption, and I\'m getting an exception thrown on my Cipher.init:

No installed provider supports this key: javax.crypto.spe

相关标签:
3条回答
  • 2020-12-31 10:27

    I've run your code in different ways, with: Java 1.{5,6,7} (using AES); different Base64 codecs (Apache Commons Codec, DatatypeConverted, Base64); different character sets; between different JVMs (through sockets) … to no avail. I got no errors.

    To narrow down the problem, can you run the following code on both ends?

    static {
      System.out.println(System.getProperty("java.version"));
      for (Provider provider : Security.getProviders())
        System.out.println(provider);
    }
    
    public static void main(String[] args) throws Exception {
      KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
      keyGenerator.init(128);
      SecretKey secretKey = keyGenerator.generateKey();
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    }
    

    (I know that you've already stated the JDK versions you're using and stuff, but it can't hurt.)

    Given that the key doesn't get corrupted while you transfer it from client to server (or maybe in reverse), then if:

    • the client throws, but the server doesn't—the error is on the client side;
    • the client doesn't throw, but the server does—the error is on the server side;
    • the client and server both throws or neither of them—needs further investigation.

    In any case, if an error is thrown, please post the whole stack trace somewhere. The error No installed provider supports this key: javax.crypto.spec.SecretKeySpec tells us nothing (at least for me it doesn't, and I couldn't reproduce this particular error either).

    0 讨论(0)
  • 2020-12-31 10:28

    This error could indicate that you need to install JCE (Java Cryptography Extension).

    Download this file (or newer version) and copy jars to JDK_FOLDER/jre/lib/security http://www.oracle.com/technetwork/pt/java/javase/downloads/jce-6-download-429243.html

    0 讨论(0)
  • 2020-12-31 10:40

    This error happens with me, when providing an incorrect key to SecretKeySpec constructor.

    0 讨论(0)
提交回复
热议问题