java pkcs1padding,RSA / NONE / PKCS1Padding给出错误为java.security.NoSuchAlgorithmException

I am using "RSA/None/PKCS1Padding" as :

Cipher RSACipher = Cipher.getInstance("RSA/None/PKCS1Padding");

This gives me exception as :

java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/None/PKCS1Padding

Thanks for help.

解决方案

Try "RSA/ECB/PKCS1Padding" instead if you are running in an Oracle or Open JDK. It does not make too much sense to use a block cipher mode of encryption with RSA, but not all algorithm names are logical within the Java SE providers.

The Bouncy Castle Libraries support "RSA/None/PKCS1Padding" though. So maybe the code was written for Bouncy or Android.

以下是使用Java生成PKCS#1 RSA密钥对的示例代码: ```java import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import javax.crypto.Cipher; public class RSATest { public static void main(String[] args) throws Exception { // 生成RSA密钥对 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); keyPairGen.initialize(2048); KeyPair keyPair = keyPairGen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 将公钥和私钥转换成PKCS#1格式 byte[] pkcs1PublicKey = getPKCS1PublicKey((RSAPublicKey) publicKey); byte[] pkcs1PrivateKey = getPKCS1PrivateKey((RSAPrivateKey) privateKey); // 打印公钥和私钥 System.out.println("PKCS#1公钥:" + bytesToHex(pkcs1PublicKey)); System.out.println("PKCS#1私钥:" + bytesToHex(pkcs1PrivateKey)); } /** * 将RSA公钥转换成PKCS#1格式 */ public static byte[] getPKCS1PublicKey(RSAPublicKey publicKey) throws Exception { byte[] encoded = publicKey.getEncoded(); int totalLength = encoded.length + 22; byte[] pkcs1PublicKey = new byte[totalLength]; pkcs1PublicKey[0] = 0x30; pkcs1PublicKey[1] = (byte) (totalLength - 2); pkcs1PublicKey[2] = 0x30; pkcs1PublicKey[3] = 0x0d; pkcs1PublicKey[4] = 0x06; pkcs1PublicKey[5] = 0x09; pkcs1PublicKey[6] = 0x2a; pkcs1PublicKey[7] = 0x86; pkcs1PublicKey[8] = 0x48; pkcs1PublicKey[9] = (byte) 0x86; pkcs1PublicKey[10] = (byte) 0xf7; pkcs1PublicKey[11] = 0x0d; pkcs1PublicKey[12] = 0x01; pkcs1PublicKey[13] = 0x01; pkcs1PublicKey[14] = 0x01; pkcs1PublicKey[15] = 0x05; pkcs1PublicKey[16] = 0x00; pkcs1PublicKey[17] = 0x03; pkcs1PublicKey[18] = 0x81; pkcs1PublicKey[19] = 0x00; byte[] modulus = publicKey.getModulus().toByteArray(); System.arraycopy(modulus, 0, pkcs1PublicKey, 22 + modulus.length - publicKey.getModulus().bitLength() / 8, modulus.length); return pkcs1PublicKey; } /** * 将RSA私钥转换成PKCS#1格式 */ public static byte[] getPKCS1PrivateKey(RSAPrivateKey privateKey) throws Exception { byte[] encoded = privateKey.getEncoded(); int totalLength = encoded.length + 26; byte[] pkcs1PrivateKey = new byte[totalLength]; pkcs1PrivateKey[0] = 0x30; pkcs1PrivateKey[1] = (byte) (totalLength - 2); pkcs1PrivateKey[2] = 0x02; pkcs1PrivateKey[3] = 0x01; pkcs1PrivateKey[4] = 0x00; pkcs1PrivateKey[5] = 0x30; pkcs1PrivateKey[6] = 0x0d; pkcs1PrivateKey[7] = 0x06; pkcs1PrivateKey[8] = 0x09; pkcs1PrivateKey[9] = 0x2a; pkcs1PrivateKey[10] = (byte) 0x86; pkcs1PrivateKey[11] = (byte) 0x48; pkcs1PrivateKey[12] = (byte) 0x86; pkcs1PrivateKey[13] = (byte) 0xf7; pkcs1PrivateKey[14] = 0x0d; pkcs1PrivateKey[15] = 0x01; pkcs1PrivateKey[16] = 0x01; pkcs1PrivateKey[17] = 0x01; pkcs1PrivateKey[18] = 0x05; pkcs1PrivateKey[19] = 0x00; pkcs1PrivateKey[20] = 0x04; byte[] modulus = privateKey.getModulus().toByteArray(); System.arraycopy(modulus, 0, pkcs1PrivateKey, 24 + modulus.length - privateKey.getModulus().bitLength() / 8, modulus.length); pkcs1PrivateKey[24 + modulus.length - privateKey.getModulus().bitLength() / 8 - 2] = 0x02; byte[] exponent = privateKey.getPrivateExponent().toByteArray(); System.arraycopy(exponent, 0, pkcs1PrivateKey, 24 + modulus.length + exponent.length - privateKey.getModulus().bitLength() / 8 - privateKey.getPrivateExponent().bitLength() / 8 - 1, exponent.length); return pkcs1PrivateKey; } /** * 将byte数组转换成十六进制字符串 */ public static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02X", b)); } return sb.toString(); } } ``` 该代码生成一个2048位的RSA密钥对,并将公钥和私钥转换成PKCS#1格式。可以通过调用`getPKCS1PublicKey`方法和`getPKCS1PrivateKey`方法获取PKCS#1公钥和私钥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值