crypt file in php and download it to the android device, then you need to decrypt it.

a piece on php

$file = fopen('License.pdf', 'rb'); $size = filesize($file); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=License'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); while (!feof($file)) { // print ($data ^ 4); $data = fread($file,4096); if (strlen($data) == 4096) { for ($i = 0; $i < 4096; $i++) { $data[$i] = ($data[$i] ^ $data[0]); } } print $data; } 

java piece

 BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file)); outFile = new File(filesDir, file.getName()); FileOutputStream out = new FileOutputStream(filePath + "_dec_02"); String base = null; String save = null; byte[] buffer = new byte[4096]; int size; char helpKey = 0; char[] keys = key.toCharArray(); while ((size = reader.read(buffer)) != -1) { if (size == 4096) { // for (int i = 0; i < 4096; i++) { buffer[i] = (byte) (buffer[i] ^ buffer[0]); } } // out.write(buffer); out.flush(); } 

what am i doing wrong?

Closed due to the fact that off-topic participants are Vladimir Martyanov , user207618, aleksandr barakin , pavel , user194374 13 Aug '16 at 6:24 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - aleksandr barakin, pavel, community spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And who knows what you are doing wrong, if you don’t even write what the problem is ... - Vladimir Martyanov
  • I apologize :-) the point is that in php I get a cryptic file, and of course I can’t decrypt it back on an Android device - Dmitry_Kon
  • In the question itself, describe the problem ... - Vladimir Martyanov

1 answer 1

 for ($i = 0; $i < 4096; $i++) { $data[$i] = ($data[$i] ^ $data[0]); } 

At a minimum, an error here: the first iteration of the loop (i = 0) will actually execute such a code:

 $data[0] = ($data[0] ^ $data[0]); 

That is, you write zero in $ data [0] and continue the "encryption" with zero. It is unlikely that you wanted to grind your "key" ...