File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can anyone help me to solve that? RSA encryption/decryption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can anyone help me to solve that? RSA encryption/decryption" Watch "Can anyone help me to solve that? RSA encryption/decryption" New topic
Author

Can anyone help me to solve that? RSA encryption/decryption

mohamed reda shafy aboud
Greenhorn

Joined: Nov 13, 2010
Posts: 6
Thanks for your great help you offer through this nice form.
My problem is in encryption and decryption using RSA, in my project I need to encrypt some string using the public key and send the results via some socket to another client to decrypt it using the relative private key.
My encryption function return byte[] and I encode it to string to be able to send it also I add some other data to this string and then in the other side I break it again and get the encrypted part off and then again turn it back to byte[] form. When I try to decrypt it an error is raised:
Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)

I try to decrypt the original byte[] in the server side and I succeeded but when I convert it to string and back again to byte[] and then decrypt it the same error is appeared . I believe that the problem is in converting to and from string.
Can anyone help me to solve that?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I believe that the problem is in converting to and from string.

Very likely so. Encrypted text is not character data, and can't be treated as such - so no strings should be involved in handling it. That's probably the #1 reason for BadPaddingExceptions.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Lester Burnham wrote:
Very likely so. Encrypted text is not character data, and can't be treated as such - so no strings should be involved in handling it. That's probably the #1 reason for BadPaddingExceptions.


There are not issues converting binary to string and back -- as long as you do some sort of encoding, such as uuencode or base64.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
mohamed reda shafy aboud
Greenhorn

Joined: Nov 13, 2010
Posts: 6
Henry Wong wrote:
Lester Burnham wrote:
Very likely so. Encrypted text is not character data, and can't be treated as such - so no strings should be involved in handling it. That's probably the #1 reason for BadPaddingExceptions.


There are not issues converting binary to string and back -- as long as you do some sort of encoding, such as uuencode or base64.

Henry


thanks for your quick reply, but can you give me an example
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
An example of what? Apparently, you are converting the encrypted text in the byte[] to a string (you've actually said as much) - don't do that; treat it as binary data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anyone help me to solve that? RSA encryption/decryption