java - How can I ensure correct padding in DES encryption? -
i trying implement basic diffie-hellman protocol , code succeeds point when needs decrypt sent value using des. have looked @ lot of examples in matter of keys not matching up, printing values on both ends of connection , both same. have tried multiple padding schemes changing how keys generated.
my last attempt in adding parameter ivparameterspec cipher init, solved 1 of errors.
i running on single machine socket connecting on localhost , have been checking issues on either side sent data not matching received data, nothing altered in sending. did notice, however, when printing each of byte arrays on either side of socket client side longer server appears padding(?)
the error getting saying final block padded incorrectly , decryption fails
my server code (the side not working intended):
public static void main(string[] args) { serversocket welcomesocket = null; // creates connectable socket on port 6789 try { welcomesocket = new serversocket(6789); } catch (ioexception e) { e.printstacktrace(); } while(true){ try{ double k2, b, a; double n = 13; double g = 61; long y = 7; b = (math.pow(g, y))%n; system.out.println("accepting connections"); // accept incoming connection on socket server socket connectionsocket = welcomesocket.accept(); // creates read , write stream client datainputstream infromclient = new datainputstream(connectionsocket.getinputstream()); dataoutputstream outtoclient = new dataoutputstream(connectionsocket.getoutputstream()); // sends double client outtoclient.writedouble(b); system.out.println("sent " + b); // reads number sent client = infromclient.readdouble(); system.out.println("received " + a); // modifies number k2 = (math.pow(a, y))%n; system.out.println("des key seed = " + k2); byte[] deskeydata = tobytearray(k2); // turns bytes of modified number des key spec deskeyspec deskeyspec = new deskeyspec(deskeydata); // makes secret key (des) secretkeyfactory keyf = secretkeyfactory.getinstance("des"); secretkey keystuff = keyf.generatesecret(deskeyspec); system.out.println(keystuff.tostring()); // gets incoming string client , turns binary byte[] incomingbytes = new byte[128]; try{ infromclient.readfully(incomingbytes); } catch(eofexception eof){ system.out.println("finished reading"); } system.out.println(new string(incomingbytes)); cipher c = cipher.getinstance("des/cbc/pkcs5padding"); // decrypts string using shared secret key c.init(cipher.decrypt_mode, keystuff, new ivparameterspec(new byte[8])); byte[] ct2 = c.dofinal(incomingbytes); // decode base 64 //byte[] decodedbytes = base64.getdecoder().decode(ct2); // prints received string system.out.println("received: " + new string(ct2)); infromclient.close(); outtoclient.close(); } catch(exception e){ e.printstacktrace(); } } }
my client code:
public static void main(string[] args) { // creates socket local host on port 6789 socket clientsocket = null; try { clientsocket = new socket("localhost", 6789); } catch (ioexception e1) { e1.printstacktrace(); } try{ double k1, b, a; double n = 13; double g = 61; long x = 3; // sends unencrypted number server = (math.pow(g, x))%n; dataoutputstream outtoserver = new dataoutputstream(clientsocket.getoutputstream()); datainputstream infromserver = new datainputstream(clientsocket.getinputstream()); // transforms byte array , sends on outtoserver.writedouble(a); outtoserver.flush(); system.out.println("sending " + a); // reads incoming data server b = infromserver.readdouble(); system.out.println("recieved " + b); // modifies data create number des key k1 = (math.pow(b, x))%n; system.out.println("des key seed = " + k1); byte[] deskeydata = tobytearray(k1); // turns bytes of modified number des key spec deskeyspec deskeyspec = new deskeyspec(deskeydata); // makes secret key (des) secretkeyfactory keyf = secretkeyfactory.getinstance("des"); secretkey keystuff = keyf.generatesecret(deskeyspec); system.out.println(keystuff.tostring()); // takes in input user , turns binary bufferedreader infromuser = new bufferedreader(new inputstreamreader(system.in)); system.out.println("enter message:"); string sentence = infromuser.readline(); byte[] str2 = sentence.getbytes(); byte[] encodedmessage = base64.getencoder().encode(str2); cipher c = cipher.getinstance("des/cbc/pkcs5padding"); // encrypts user's input secret key c.init(cipher.encrypt_mode, keystuff, new ivparameterspec(new byte[8])); byte[] ct2 = c.dofinal(encodedmessage); system.out.println("initted cipher , moving forward " + new string(ct2)); // writes encrypted message user outtoserver.write(ct2); outtoserver.flush(); infromserver.close(); outtoserver.close(); } catch(exception e){ e.printstacktrace(); } }
anything me in getting working immensely welcome since have been working on error alone quite time.
i managed figure out way solve (though suspect inefficient). issue due readfully method on server side. reading answer 128 byte array , decrypt function saw empty slots in byte array instead of nothing.
to solve replaced input section following, reads each individual byte , creates array of bytes exact length of incoming message.
arraylist<byte> totalbytes = new arraylist<byte>(); while(true){ try{ byte in = infromclient.readbyte(); totalbytes.add(in); } catch(eofexception eof){ system.out.println("finished reading"); break; } } int incomingsize = totalbytes.size(); byte[] receivedbytes = new byte[incomingsize]; for(int = 0; < incomingsize; i++){ receivedbytes[i] = totalbytes.get(i); }
Comments
Post a Comment