我有两个长度未知的 byte[]
数组,我只想将一个附加到另一个的末尾,即:
byte[] ciphertext = blah;
byte[] mac = blah;
byte[] out = ciphertext + mac;
我尝试过使用 arraycopy()
但似乎无法正常工作。
最佳答案
使用 System.arraycopy()
,类似以下的东西应该可以工作:
// create a destination array that is the size of the two arrays
byte[] 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);
关于java - 将一个 byte[] 附加到另一个 byte[] 的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368704/