简体   繁体   English

java RSA多重加密

[英]java RSA Multiple Encryption

I encrypt my message with a symmetric key and the symmetric key itself has to be further encrypted with different RSA public keys. 我使用对称密钥加密我的消息,并且对称密钥本身必须使用不同的RSA公钥进一步加密。 When I tried to implement the above I got the following error: 当我尝试实现上述内容时,出现以下错误:

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)

Is there a way to solve this problem ? 有没有办法解决这个问题?

i need to do something like this [[[[key]PK-A]PK-B]PK-C], where PK-A = Public key of A, similarly PK-B and PK-C. 我需要做这样的[[[[key] PK-A] PK-B] PK-C],其中PK-A = A的公钥,类似PK-B和PK-C。

Below are the codes that i tried here are the codes 以下是我在这里尝试的代码是代码

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);

Either you should pad your data to satisfy the encryption algorithm requirements or you should create the instance of Cipher class pointing the padding to use, eg: 您应该填充数据以满足加密算法要求,或者您应该创建指向要使用的填充的Cipher类实例,例如:

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

As you need to use asymmetric keys then initialize your cipher the following way: 当您需要使用非对称密钥时,请按以下方式初始化密码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM