android - Read different inputs from InputStream JAVA -


i'm writing code send encrypted file client server first client send encrypted message digest of file server , send name of file , @ end send bytes of encrypted file, in server side read these variables 1 variable digest ,

and when server trying decrypt digest throws illegal block size exception question here how can server read , save them in different variables ??

client

            // set mode encrypt             aescipher.init(cipher.encrypt_mode, key);              dataoutputstream toserver = new dataoutputstream(socket.getoutputstream());              // digest of file             messagedigest md = messagedigest.getinstance("md5");             byte[] hash = md.digest(bytes);              // encrypt digest , write file             byte [] encryptedhash = aescipher.dofinal(hash);             toserver.write(encryptedhash);               // write file name server             toserver.writeutf(filename);              //encrypt file             byte[] encryptedbyte = aescipher.dofinal(bytes);               // write file server             toserver.write(encryptedbyte);             toserver.flush();             socket.close(); 

server

// read digest of file         byte [] digest =ioutils.tobytearray(fromclient);          // decrypt         aescipher.init(cipher.decrypt_mode, key);         byte[] decrypteddigest = aescipher.dofinal(digest);          // read file name received         string filename = fromclient.readutf();          file file = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads), filename);         file.createnewfile();         fileoutputstream fos = new fileoutputstream(file);         bufferedoutputstream bos = new bufferedoutputstream(fos);          // read file bytes client         byte[] filebytes = ioutils.tobytearray(fromclient);          aescipher.init(cipher.decrypt_mode, key);         byte[] decryptedbyte = aescipher.dofinal(filebytes);         bos.write(decryptedbyte, 0, decryptedbyte.length);         bos.close(); 

also tried code didn't works too

// read digest of file          bytearrayoutputstream buffer = new bytearrayoutputstream();          int nread;         byte[] data = new byte[1024];          while ((nread = fromclient.read(data, 0, data.length)) != -1) {             buffer.write(data, 0, nread);         }          buffer.flush();         byte[] digest = buffer.tobytearray();          // decrypt         aescipher.init(cipher.decrypt_mode, key);         byte[] decrypteddigest = aescipher.dofinal(digest);          // read file name received         string filename = fromclient.readutf();          file file = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads), filename);         file.createnewfile();         fileoutputstream fos = new fileoutputstream(file);         bufferedoutputstream bos = new bufferedoutputstream(fos);          // read file bytes client         byte[] filebytes = ioutils.tobytearray(fromclient);          aescipher.init(cipher.decrypt_mode, key);         byte[] decryptedbyte = aescipher.dofinal(filebytes);         bos.write(decryptedbyte, 0, decryptedbyte.length);         bos.close(); 

ioutils.tobytearray(inputstream) reads entire stream. instead of getting hash bytes, got whole stream, , there nothing left filename or ciphertext, , hash didn't check.

you don't need external libraries this. can datainputstream , dataoutputstream. need send length of hash ahead of hash.

client:

        // set mode encrypt         aescipher.init(cipher.encrypt_mode, key);          dataoutputstream toserver = new dataoutputstream(socket.getoutputstream());          // digest of file         messagedigest md = messagedigest.getinstance("md5");         byte[] hash = md.digest(bytes);          // encrypt digest , write file         byte [] encryptedhash = aescipher.dofinal(hash);         toserver.writeint(encryptedhash.length);         toserver.write(encryptedhash);          // write file name server         toserver.writeutf(filename);          //encrypt file         byte[] encryptedbyte = aescipher.dofinal(bytes);          // write file server         toserver.writeint(encryptedbyte.length);         toserver.write(encryptedbyte);         socket.close(); 

server:

    // read digest of file     int digestlength = fromclient.readint();     byte[] digest = new byte[digestlength];     fromclient.readfully(digest);      // decrypt     aescipher.init(cipher.decrypt_mode, key);     byte[] decrypteddigest = aescipher.dofinal(digest);      // read file name received     string filename = fromclient.readutf();      file file = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads), filename);     fileoutputstream fos = new fileoutputstream(file);     bufferedoutputstream bos = new bufferedoutputstream(fos);      // read file bytes client     int filelength = fromclient.readint();     byte[] filebytes = new byte[filelength];     fromclient.readfully(filebytes);      aescipher.init(cipher.decrypt_mode, key);     byte[] decryptedbyte = aescipher.dofinal(filebytes);     bos.write(decryptedbyte, 0, decryptedbyte.length);     bos.close(); 

however encryption , decryption parts of better done cipherinputstream , cipheroutputstream. shouldn't load entire files memory.

note file.createnewfile() call redundant before new fileoutputstream(...).

why you're encrypting message digest mystery. should using final step compare locally-generated digest after decryption.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -