Forums Register Login

Remove padding

+Pie Number of slices to send: Send
Hi,
Do you have any good and effective way to remove padding at the end of byte-array, or string? Use of "PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()))" on decryption produces fixed length of byte arrays (8 bytes) and if the message is not equal to single block size, or multiplied block size, the end of byte array will be padded by byte '0'.
An example of BC's decryption generates first a String from byte array and uses a trim()-function on it to get rid of padding. It's quite obvious that this type of removing cannot be used in any other place as in example, because it also reduces orginal data, if it starts/ends by spaces.
I'm going to use traditional for-loop to get rid of byte '0', but is there any other and faster way to do it?
Aimed solution:
byte[] b = {32,65,66,67,32,0,0,0}; //for debugging
String rs = null;
for (int i = b.length-1; i>-1; i--){
if(b[i] != 0){
rs = new String(b, 0, i+1);
break;
}
}
if (rs == null){rs = new String();}
return rs:
Any idea of better/faster one? - something else?
br,
Jorma
+Pie Number of slices to send: Send
I doubt there's anything faster than your for loop. Of course there could be a class that does the same with less lines of code, though. I would just go with the for loop.
Squanch that. And squanch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2076 times.
Similar Threads
TripleDES compatibility (Sun - BC)
DESede exception
Error message: "pad block corrupted"
pad block corrupted
Problem encrypting with openssl, decrypting with bouncy castle
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:20:36.