0

我用对称密钥加密我的消息,对称密钥本身必须用不同的 RSA 公钥进一步加密。当我尝试实现上述内容时,出现以下错误:

javax.crypto.IllegalBlockSizeException: The input was invalid: Invalid input length.
        at com.rsa.shareCrypto.j.hD.engineDoFinal(Unknown Source)
        at javax.crypto.Cipher.doFinal(Cipher.java:2087)
        at wrap1.main(wrap1.java:69)

有没有办法解决这个问题?

我需要做这样的事情[[[[key]PK-A]PK-B]PK-C],其中PK-A = A的公钥,类似PK-B和PK-C。

以下是我在这里尝试的代码是代码

String InitialKey = "2011BCSChampionA";

byte[] initkey = InitialKey.getBytes();
// First level encryption of the key

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

keyGen.initialize(1024);
KeyPair keypair = keyGen.genKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(initkey);
// Second level of encryption
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("RSA");
keyGen1.initialize(1024);
KeyPair keypair1 = keyGen1.genKeyPair();
PrivateKey prvKey = keypair1.getPrivate();
PublicKey pubKey = keypair1.getPublic();
Cipher cipher1 = Cipher.getInstance("RSA");
cipher1.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData_new = cipher1.doFinal(cipherData);
4

1 回答 1

2

您应该填充数据以满足加密算法要求,或者您应该创建Cipher指向要使用的填充的类的实例,例如:

cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

由于您需要使用非对称密钥,请按以下方式初始化密码:

cipher = Cipher.getInstance("RSA/ECB/PKCS5Padding");
于 2012-10-21T20:45:33.790 回答