Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

RSA Encryption in crypto++ and decryption in java using BC

843811Sep 10 2007 — edited Sep 12 2007
I have some problem in performing in decrypting a string in BC which was encrypted using Crypto++ in c++ application using RSA encryption.

All the symmetric algorithms works fine , the problem is only with RSA.

Below is the code i have written to encrypt a string using crypto++
// rebuild the public key
    RSAFunction rs;
    Integer it("116297669686057679294877276879567246299222268120848197352278975150952126198908382362376665650470513975918339553377750756634487177347348719348381933238208421600325762381904044278291970012585654988470536793991370300324551834827906496125133540743511381486547013572102549606765658314029348528132264092602915745569");
    
  rs.SetModulus(it);
  rs.SetPublicExponent(65537);

 HexEncoder  pubFile(new FileSink(publicFilename.c_str()));
 rs.DEREncode(pubFile);
 pubFile.MessageEnd();

//perform encryption
   FileSource pubFile(pubFilename, true, new HexDecoder);
    RSAES_PKCS1v15_Encryptor pub(pubFile);

    RandomPool randPool;
  
    string result;
    StringSource(message, true, new PK_EncryptorFilter(randPool,   pub,    new Base64Encoder(new StringSink(result))));
    
And here is the java code for decryption...
cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
result = cipher.doFinal(Base64.decode(in));
At this point i get the following exception.....

java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA12275)

any suggestions to resolve this problem is appreciated
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2007
Added on Sep 10 2007
1 comment
1,813 views