Java Encryption & Sockets problems

Discussion in 'Programming' started by Stefan15ist, May 10, 2015.

  1. So I made an encryption using AES, and from the client right before I send it to the server it has 32 bytes, now when I receive it on the server it has 11 Bytes
    here is some parts of the code:
    Client
    Code (Text):
                   
    s = new Socket(address, 1000);
    BufferedReader originalKey = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String key = originalKey.readLine();
                   
    byte[] mensagem = ("logout,"+usernameBox.getText()+","+passwordBox.getText()).getBytes();
    byte[] chave = key.getBytes();
                   
    byte[] encrypted = Encrypt(mensagem, chave);
                   
    PrintWriter writer = new PrintWriter(s.getOutputStream(), true);
    writer.println(encrypted); //it has 32 bytes right there
    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
    JOptionPane.showMessageDialog(null, input.readLine());
    System.exit(0);
    Server:
    Code (Text):
    BufferedReader information = new BufferedReader(new InputStreamReader(socket.getInputStream())); //encrypted
    String informationReader = information.readLine(); //it has 11 bytes here >_>
     
  2. You can't send and read binary data with readers and you can't read a 'line', binary data has no 'lines'.
     
  3. I spent 6 hours trying to do it Ill just try md5 it uses strings :p
     
    • Optimistic Optimistic x 1
  4. The problem is likely the encryption is sending a line character in the message, and since you're reading it as a line, it's picking up the first character. You'd be better to send the size as a byte/short/int before and then read that size before reading the data.

    Something like this for example.

    Write:
    Code (Text):
    byte[] encrypt;
    writeInt(encrypt.length);
    writeBytes(encrypt);
    Read:
    Code (Text):
    int size = readint();
    byte[] encrypt = new byte[size];
    readBytes(encrypt);
    byte[] unencrypt = unencrypt(encrypt);
     
     
  5. this doesn't make sence bytes can be the same size but with different values... I can't make a new byte out of something I just created I need to transfer the correct bytes over the sockets
     
  6. Ok what I'm saying is you're sending 32 encrypted bytes. Those encrypted bytes can be anything from 0-255. You're reading a line, which reads bytes until it matches the new_line character, or byte 10. So after you encrypt it, the byte at index 12 in the array of 32 is now a new_line character. when you read that on the other end, you're going to read until byte 12, and then it's going to be a length of 11.
     
  7. oh, so if I writeBytes with ObjectOutputStream it will read correctly on the other side?
     
  8. Not exactly, you need to know how many bytes are being written. By just writing the bytes to a buffer, you're not specifying the length, and as such the other end wouldn't know how to read it. If you want to do it properly, you will need to write the size as a byte, short, medium, int, or any other number type before you send it. Then you will read the size on the other end, and then read how many bytes the size says.
     
  9. Client:
    Code (Java):
    s = new Socket(address, 1000);
    BufferedReader originalKey = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String key = originalKey.readLine();

    byte[] mensagem = ("logout,"+usernameBox.getText()+","+passwordBox.getText()).getBytes();
    byte[] chave = key.getBytes();
    byte[] encrypted = Encrypt(mensagem, chave);

    DataOutputStream writer = new DataOutputStream(s.getOutputStream());
    writer.writeInt(encrypted.length);
    writer.write(encrypted);

    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
    JOptionPane.showMessageDialog(null, input.readLine());
    System.exit(0);
    Server:
    Code (Java):
    DataInputStream information = new DataInputStream(socket.getInputStream());
    int length = information.readInt();
    byte[] encrypted = new byte[length];
    information.readFully(encrypted);
    String informationReader = // decrypt your shit