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
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