【发布时间】:2021-03-11 21:34:13
【问题描述】:
我正在尝试创建一个密码来对我创建的 AES 密钥进行编码。我从存储公钥的文件中读取了公钥,并将其从字节转换为公钥对象。当我尝试使用此公钥初始化密码时,在 cipherAES.init(Cipher.ENCRYPT_MODE, DecodedPublicKey);
因此,当我创建 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();```
【问题讨论】:
-
我发现添加转换为“RSA”可以使一切正常,删除提供程序。虽然这消除了转换 AES/GCM,这仍然是一种有效的方式吗?
标签: java encryption rsa