没有已安装的提供商支持此密钥:sun.security.rsa.RSAPublicKeyImpl 并且没有这样的算法:AES/GCM/NoPadding

No installed provider supports this key: sun.security.rsa.RSAPublicKeyImpl and No such algorithm: AES/GCM/NoPadding

我正在尝试创建密码以对我创建的 AES 密钥进行编码。我从存储 public 密钥的文件中读取了 public 密钥,并将其从字节转换为 public 密钥对象。当我尝试使用此 public 密钥初始化密码时,我在 cipherAES.init(Cipher.ENCRYPT_MODE, DecodedPublicKey);

处收到错误消息“没有安装的提供程序支持此密钥:sun.security.rsa.RSAPublicKeyImpl”

因此,当我创建一个 RSA 2048 密钥生成器对象以获取提供程序时,这是我在另一个程序中创建以前的密钥所使用的规范,我收到错误“没有这样的算法:AES/GCM/NoPadding" 在 Cipher cipherAES = Cipher.getInstance("AES/GCM/NoPadding", kpgenProv.getName());

我需要使用转换 AES/GCM/NoPadding,从 .getProvider 提供的提供者似乎是正确的,它是 SunRsaSign。有没有人对这个问题有任何见解?它在没有提供者时识别转换正常,但在有提供者时拒绝转换。

      String pubAlgo = publicKeyScanner.nextLine(); // not used
      String PublicKey = publicKeyScanner.nextLine(); // this will stop when it hits a newline and the encoded key may have the newline char value causing the private key to be piecemeal
      byte[] decodedPublicKey = Base64.getDecoder().decode(PublicKey);
      KeyFactory kf = KeyFactory.getInstance("RSA"); // or "EC" or whatever
      PublicKey DecodedPublicKey = kf.generatePublic(new X509EncodedKeySpec(decodedPublicKey));
      publicKeyScanner.close();
      System.out.println(DecodedPublicKey.toString());
    
      KeyGenerator keyGen = KeyGenerator.getInstance("AES");
      keyGen.init(128); // for example
      SecretKey AESKey = keyGen.generateKey();

      KeyPairGenerator kpgen = KeyPairGenerator.getInstance("RSA");
      kpgen.initialize(2048);
      Provider kpgenProv = kpgen.getProvider();
      System.out.println(kpgenProv.getName());
      Cipher cipherAES = Cipher.getInstance("AES/GCM/NoPadding", kpgenProv.getName());
      cipherAES.init(Cipher.ENCRYPT_MODE, DecodedPublicKey);
      byte[] AESKEYBYTES = AESKey.getEncoded();```

Java JDK 通常由一些负责加密操作的安全提供商提供。由于历史原因,他们没有得到 combined/bundled 而是存在于彼此之下。如果您通过实例化选择算法 Java 将找出哪个提供者“负责”该算法并接受他。

另一方面 - 如果您想使用特定的提供程序,您可以在实例化期间命名该提供程序(这就是您使用第二个参数“kpgenProv.getName”所做的事情:

Cipher.getInstance("AES/GCM/NoPadding", kpgenProv.getName())

如您在我的简单程序中所见,“RSA”和“AES/GCM/NoPadding”有两个不同 提供程序,但是当强制使用特定提供程序时,您会收到“否” -installed-provider-supports...”异常。

所以通常您会省略提供者名称,让 Java 选择该算法的“最佳”提供者。

我的系统输出(打开JDK11):

Get security provider
provider for RSA encryption: SunRsaSign version 11
provider for AES/GCM/NoPadding: SunJCE version 11

代码:

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException {
        System.out.println("Get security provider");

        KeyFactory kf = KeyFactory.getInstance("RSA");
        System.out.println("provider for RSA encryption: " + kf.getProvider());

        Cipher cipherAES = Cipher.getInstance("AES/GCM/NoPadding");
        System.out.println("provider for AES/GCM/NoPadding: " + cipherAES.getProvider());
    }
}