Вопрос

I'm working on a project in which I have to decrypt a xml text from PHP server and parse into java, I have decrypted the xml text by using CipherInputStream and seen xml file fully get printed, but I'm facing a weird issue while trying to store the xml text in a java string, I'm working on the code below:

public String decrypt(InputStream Fis){

Cipher c = Cipher.getInstance(algo + "/CBC/NoPadding");

String add = "";

StringBuilder getAns = new StringBuilder();

c.init(Cipher.DECRYPT_MODE, key);

CipherInputStream cis = new CipherInputStream(Fis , c);

byte[] encData = new byte[16];

int dummy;

while ((dummy = cis.read(encData)) != -1)
        {

            System.out.println(new String(encData, "UTF-8").trim()); 

                    add = (new String(encData, "UTF-8").trim());

                    getAns.append(add);


}

System.out.println(getAns); 
...
}

It prints the full XML text in log cat by the first println statement inside while loop, But when printing the StringBuilder getAns, it only prints a part of the text.

I have also tried just using String:

add = add + (new String(encData, "UTF-8").trim());

also using ArrayList<String> but it only prints a part of the text.

I guess this may be due to a silly mistake , but I'm struggling with this. Any help will be appreciated.

Это было полезно?

Решение 2

Finally caught the issue, It's with the System.out.pritnln(), It has certain limits for printing. This function can only print around 4060 characters at a time, I've found this by using getAns.substring(10000,15000);.

Другие советы

You are reading some bytes from the input stream inside the while condition:

while ((dummy = cis.read(encData)) != -1)

This already populates the encData byte array with having the correct number of read bytes in the dummy variable. Afterwards you again read some bytes:

dummy = cis.read(encData);

This overwrites the bytes you have read one step before. Delete that line!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top