0

I encrypt my message with a symmetric key and the symmetric key itself has to be further encrypted with different RSA public keys. 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.

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);
3
  • 1
    Is there a way to solve this problem ? If you post your code too, most definitely yes! Commented Oct 21, 2012 at 20:39
  • Does your encryption algorithm require block length that are mod 8? I seem to remember this about java... Commented Oct 21, 2012 at 20:40
  • i have given the codes i tried Commented Oct 22, 2012 at 19:40

1 Answer 1

2

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, e.g.:

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");
Sign up to request clarification or add additional context in comments.

2 Comments

i have to use asymmetric algorithm like RSA. Its kind of similar to Onion protocol.
i suppose its PKCS1Padding. i tried cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); i still get the following error javax.crypto.IllegalBlockSizeException: The input was invalid: Invalid input length. i need to do something like this [[[[key]PK-A]PK-B]PK-C] where PK-A = public key of A, similarly PK-B, PK-C i do this layered encryption and then i decrypt from the outer wall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.