Appending a byte[] to the end of another byte[] [duplicate]
Using System.arraycopy()
, something like the following should work:
// create a destination array that is the size of the two arraysbyte[] destination = new byte[ciphertext.length + mac.length];// copy ciphertext into start of destination (from pos 0, copy ciphertext.length bytes)System.arraycopy(ciphertext, 0, destination, 0, ciphertext.length);// copy mac into end of destination (from pos ciphertext.length, copy mac.length bytes)System.arraycopy(mac, 0, destination, ciphertext.length, mac.length);
You need to declare out
as a byte array with a length equal to the lengths of ciphertext
and mac
added together, and then copy ciphertext
over the beginning of out
and mac
over the end, using arraycopy.
byte[] concatenateByteArrays(byte[] a, byte[] b) { byte[] result = new byte[a.length + b.length]; System.arraycopy(a, 0, result, 0, a.length); System.arraycopy(b, 0, result, a.length, b.length); return result;}