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 6796269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:26:49+00:00 2026-05-26T18:26:49+00:00

This is my first question here, so I hope that I am providing enough

  • 0

This is my first question here, so I hope that I am providing enough information to be able to get some kind of resolution to this (smallish?) problem I’m having with a (IMO) very basic public key crypting/decrypting problem.

I’ve tried to follow examples, and I’ve read through APIs to try and figure out what is wrong, but so far I haven’t been able to come up with an answer to why the decryption fails with such a basic test…

I have printed out the hexes of all the stuff I transfer between the client and the server, and they seem to be identical. I’ve tried to use different paddings, and ciphers, but none seem to work.

The codes from the two programs (I’ve edited out the non-essential printouts):

Server code:

RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.initialize(1024);
KeyPair kp = kpg.generateKeyPair();
PublicKey pk = kp.getPublic();
PrivateKey pri = kp.getPrivate();
InputStream in = csocket.getInputStream();
OutputStream out = csocket.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);

// write getEncoded().length
dos.writeInt(pk.getEncoded().length);

// write key
byte[] public_key = pk.getEncoded();
for (int x=0;x<public_key.length;x++) {
    dos.writeByte(public_key[x]);
}
dos.flush();

// read enc length
int len=dis.readInt();
byte[] data = new byte[len];

// read enc stuff
System.out.println("Read data:");
for (int x=0;x<len;x++) {
    data[x]=dis.readByte();
}
// decrypt
byte [] decrypted = null;
try { cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, pri);
    decrypted = cipher.doFinal(new String(data).getBytes());
} catch (Exception e) { e.printStackTrace(); }

Client:

InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);

// Read key length
int len = dis.readInt();

// Read key
byte[] public_key = new byte[len];
byte[] tmp = new byte[1];
for (int x = 0; x<len; x++) {
    public_key[x] = dis.readByte();
}

try { 
    keySpec = new X509EncodedKeySpec(public_key);
    keyFactory = keyFactory.getInstance("RSA");
    publicKey = keyFactory.generatePublic(keySpec);
} catch (Exception e) { e.printStackTrace(); }

try {
    cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "IBMJCE");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    // crypt string
    data = cipher.doFinal(new String("Encrypt this").getBytes());
} catch (Exception e) { e.printStackTrace(); }

// write data.length
dos.writeInt(data.length);

// write encrypted data
for (int x=0; x<data.length;x++) {
      dos.writeByte(data[x]);
}
dos.flush();
  • 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-26T18:26:49+00:00Added an answer on May 26, 2026 at 6:26 pm

    The answer to this problem was: Learn to read your own code, and keep track of what you’ve done… Sorry for wasting your time :/

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

Sidebar

Related Questions

This is my first question here on stackoverflow, so I hope that I am
This is my first question here, but I hope I will get a lot
this is my first question here; hope I'll be clear enough... I've got this
this is my first question here so I hope I can articulate it well
This is my first time here so I hope I post this question at
this is my first question here :) I know that I should not check
First-timer here, I hope I explain this well enough... PHP/Smarty, I'm working on a
First time I'm writing something here. I have a question that i hope someone
Since this is my first question here on stackoverflow I hope my question is
this my first question on here. I hope someone can help. It is to

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.