Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:25:05+00:00 2026-05-22T16:25:05+00:00

I am facing a very peculiar problem when using RSA encryption/decryption in Java. Example

  • 0

I am facing a very peculiar problem when using RSA encryption/decryption in Java.

Example code:

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();

Cipher enc = Cipher.getInstance("RSA");
enc.init(Cipher.ENCRYPT_MODE, kp.getPublic());
String CipherText = new String(enc.doFinal(PlainText.getBytes()));
System.out.println("CipherText: ") + CipherText);

Cipher dec = Cipher.getInstance("RSA");
dec.init(Cipher.DECRYPT_MODE, kp.getPrivate());
PlainText = new String(dec.doFinal(CipherText.getBytes()));
System.out.println("PlainText: " + PlainText);

As everyone can plainly see: I encrypt the plaintext using the public key, after which I decrypt the ciphertext using the private key.

This code crashes with the following message:

Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero

I also tried to explicitly use “RSA/ECB/NoPadding”, and this fails on decoding period. (Eg the decoded ciphertext doesn’t match the original plaintext).

Last but not least, I have tried to perform this when using my own PKCS1.5 padding function ala the PKCS1.5 specs:

EMB = 00 || 02 || RD || 00 || MD
EMB is encoded messageblock of length k
Where RD are 8 random nonzero bytes
MD is max length k = 11, and optionally padded with zero bytes to make EMB length k.

After two days of testing I can only conclude that the RSA algo in Java is flawed or simply not performing what I expect it to perform.

Any suggestions or fixes to the above code are very welcome, as I am completely stumped on why the above code will not simply work as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:25:06+00:00Added an answer on May 22, 2026 at 4:25 pm

    Don’t do this:

    String CipherText = new String(enc.doFinal(PlainText.getBytes()));
    

    Two reasons:

    • It’s almost never a good idea to call String.getBytes() without specifying an encoding. Do you really want the result to depend on the system default encoding?
    • It’s definitely never a good idea to treat the result of a binary encryption operation (i.e. opaque binary data) as an encoded string. Encode it in Base64 or hex instead.

    You can use Apache Commons Codec to perform the base64 encode/decode operations, or this standalone public domain encoder/decoder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A very standard example of the problem that I am facing is that of
I am facing a very peculiar problem that even when I delete some rows,
I am facing a very silly problem with my code for buttons in the
I am facing a really very interesting problem with string. My code is: Dim
I am facing the very strange problem in HTML/CSS.I am using chrome 12.0.742.122. Now
I am writing a very basic web spider in java.I am facing one problem,
I am testing out the very awesome Knockoutjs. I am facing a peculiar problem.
I am facing a very weird problem: I am using mailer.py as post-commit hooks
I am facing a very strange problem where the same loop keeps giving me
In my 2nd ASP.NET MVC project I'm facing a very weird problem: when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.