Results 1 to 2 of 2
  1. #1
    mahta_gh is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Red face problem usding RSA encode/decode while sending data via sockets

    hi
    i need to encode data by public key and send them to server via sockets and decode them using private key there
    i generated pairkeys and i think i'v done every thing
    but in server side , when i am trying to decode , i get -> Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero

    the RSAHandler which i'm using is not written by me , i just found it in internet
    here is my code

    PublicKey readKeyFromFileC(String keyFileName) throws IOException {

    InputStream in = new FileInputStream(keyFileName);
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
    try {
    BigInteger m = (BigInteger) oin.readObject();
    BigInteger e = (BigInteger) oin.readObject();
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PublicKey pubKey = fact.generatePublic(keySpec);
    return pubKey;
    } catch (Exception e) {
    throw new RuntimeException("Spurious serialisation error", e);
    } finally {
    oin.close();
    }
    }

    PrivateKey readKeyFromFileS(String keyFileName) throws IOException {


    InputStream in = new FileInputStream(keyFileName);
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
    try {
    BigInteger m = (BigInteger) oin.readObject();
    BigInteger e = (BigInteger) oin.readObject();
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PrivateKey privKey = fact.generatePrivate(keySpec);
    return privKey;
    } catch (Exception e) {
    throw new RuntimeException("Spurious serialisation error", e);
    } finally {
    oin.close();
    }
    }

    public String rsaEncrypt(byte[] data) throws Exception {
    PublicKey pubKey = (PublicKey) readKeyFromFileC("/public.key");
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    byte[] cipherData = cipher.doFinal(data);
    return cipherData.toString();
    }


    public String rsaDecrypt(byte[] data) throws Exception {
    PrivateKey privKey = (PrivateKey) readKeyFromFileS("/private.key");
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] deCipherData = cipher.doFinal(data);
    return deCipherData.toString();
    }


    --------------
    here is the client side code

    public void sendToServer(String sendText) throws Exception {
    Socket clientSocket = new Socket("localhost", 5000);
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter (clientSocket.getOutputStream())), true);
    out.println(new String(sendText));
    }

    tcpClient.sendToServer(rsaHandler.rsaEncrypt(login Info.getBytes()));
    String res = tcpClient.RecieveFromServer();

    -------------------------------
    and server side code

    RSAHandler rsaHandler = new RSAHandler();
    Message = rsaHandler.rsaDecrypt(fromclient.getBytes()).toCha rArray();





    please help me , its long time that i'm dealing with this code
    i think maybe the problem is in converting string to byte / byte to string in encoding/decoding and sending process , but i can't fix it
    thanks in advance if somebody can solve my problem


    the italic line , is where i get that exception javax.crypto.BadPaddingException: Data must start with zero

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Don't double post the same question and don't hijack another poster's thread. Also, don't post to old, long dead threads.

    Your post in Padding Problem as been removed.

    db

Similar Threads

  1. Encode data to torrent?
    By greatmajestics in forum AWT / Swing
    Replies: 0
    Last Post: 04-28-2010, 04:12 PM
  2. Replies: 1
    Last Post: 01-09-2010, 02:16 PM
  3. Getting problem in UTF-8 Encode/Decode with Java
    By sagarsway in forum Advanced Java
    Replies: 2
    Last Post: 12-22-2008, 07:01 PM
  4. Sending files over sockets!
    By rameshraj in forum Networking
    Replies: 2
    Last Post: 05-30-2008, 10:18 PM
  5. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •