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

  • SEARCH
  • Home
  • 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 9069041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:19:36+00:00 2026-06-16T17:19:36+00:00

I am trying to encrypt and decrypt the data with RSA 2048. We have

  • 0

I am trying to encrypt and decrypt the data with RSA 2048.

We have one public key and private key and will be using same throughout.
but the problem is, when I decrypt, I am getting javax.crypto.BadPaddingException: Data must start with zero

File file = new File("C:\\temp-ldi\\pubkey.txt");
FileWriter writer = new FileWriter(file);
file.createNewFile();
encryptedText = RSACrypto.encrypt("PLAIN TEXT"); //no argument of pub-key, generate key pair
writer.write(new BASE64Encoder().encode(RSACrypto.pubKeyToBytes(RSACrypto.publicKey)));
writer.close();
file = new File("C:\\temp-ldi\\privkey.txt");
writer = new FileWriter(file);
file.createNewFile();
writer.write(new BASE64Encoder().encode(RSACrypto.privKeyToBytes(RSACrypto.privateKey)));
writer.close();

then I am using below code to decrypt the data

File privfile = new File("C:\\temp-ldi\\privkey.txt");
File pubfile = new File("C:\\temp-ldi\\pubkey.txt");
FileReader reader = new FileReader(pubfile);
// file.createNewFile();
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String s;
while ((s = br.readLine()) != null) {
    sb.append(s);
}
br.close();
reader.close();
this.encryptedText = RSACrypto.encrypt("PLAIN TEXT", sb.toString());
reader = new FileReader(privfile);
br = new BufferedReader(reader);
sb = new StringBuilder();
while ((s = br.readLine()) != null) {
    sb.append(s);
}
br.close();
reader.close();
System.out.println(RSACrypto.decrypt(this.encryptedText, sb.toString()));

all the encryption/decryption string will return in Base64Encoder/Base64Decoder format.

How do I pass the private key from file/simple string so that the key is not modified.

Update
RSACrypto class : http://sebsauvage.net/paste/?83517f2b3db94d24#Sdu12/vXPuxa5AxO95FPgKSF6N40R2DzD6lwQkvroyE=

  • 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-06-16T17:19:37+00:00Added an answer on June 16, 2026 at 5:19 pm

    OK, problem in RSACrypto. When you encrypt file, it creates every time new keypair (in encrypt). Just remove new keypair generation from encrypt, call newKeyPair direcly, when you need it.
    And that static variables does no good for multi-thread environment.

    I’ll suggest to throw RSACrypto class, or rewrite it at least. I don’t know why you so afraid of using byte[] type and why you need everything be BASE64 encoded. Code will a lot simple without additional encoding/decoding.

    Here is working example (without RSACrypto), you can use it as template:

        File file = new File("C:\\temp-ldi\\pubkey.txt");
        FileWriter writer = new FileWriter(file);
        file.createNewFile();
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048, new SecureRandom());
        KeyPair keyPayr = generator.generateKeyPair();
        writer.write(new BASE64Encoder().encode(keyPayr.getPublic().getEncoded()));
        writer.flush();
        writer.close();
        file = new File("C:\\temp-ldi\\privkey.txt");
        writer = new FileWriter(file);
        file.createNewFile();
        writer.write(new BASE64Encoder().encode(keyPayr.getPrivate().getEncoded()));
        writer.flush();
        writer.close();
    
    
        File privfile = new File("C:\\temp-ldi\\privkey.txt");
        File pubfile = new File("C:\\temp-ldi\\pubkey.txt");
        FileReader reader = new FileReader(pubfile);
    
        BufferedReader br = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        String s;
        while ((s = br.readLine()) != null) {
            sb.append(s);
        }
        br.close();
        reader.close();
        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(sb.toString())));
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        String encryptedText = new BASE64Encoder().encode(cipher.doFinal("PLAIN TEXT".getBytes("UTF-8")));
        System.out.println("encrypted: " + encryptedText);
        reader = new FileReader(privfile);
        br = new BufferedReader(reader);
        sb = new StringBuilder();
        while ((s = br.readLine()) != null) {
            sb.append(s);
        }
        br.close();
        reader.close();
        PrivateKey privateKey =  KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(sb.toString())));
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        System.out.println( new String(cipher.doFinal (new BASE64Decoder().decodeBuffer(encryptedText))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to encrypt and decrypt the data using RSA 2048 with public
I'm trying to encrypt and decrypt data using RSA in C#. I have the
I am trying the most basic thing - encrypt data using the public key
I'm trying to encrypt some content with an RSA private key. I'm following this
I am trying to encrypt some plain text with a public key using RSA_public_encrypt(),
I'm trying to import an RSA public/private key pair generated with the Win32 Crypto
I'm trying to use PHP and OpenSSL to encrypt some data using a public
I'd like to encrypt in JavaScript, decrypt in PHP, using public-key cryptography. I've been
I have a CryptoAPI code to encrypt\ decrypt given data using AES-128 and a
i am trying to encrypt and decrypt a string using RSA algorithm. Here the

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.