class ByteSumSt {
public ByteSumSt(byte[] bDest, int bDestLen) {
this.bDest = bDest;
this.bDestLen = bDestLen;
}
byte[] bDest;
int bDestLen;
}
public static ByteSumSt TransUtilsGenerateRandom(int randomLength, boolean isHex) {
int capacityLength = isHex ? randomLength * 2 : randomLength;
byte[] bRandom = new byte[capacityLength];
for (int i = 0; i < randomLength; i++) {
double dr = Math.random();
byte rByte = (byte)(dr * 256);
if (isHex) {
String strHex = Integer.toHexString(rByte & 0x0FF);
if (1 == strHex.length()) {
strHex = "0" + strHex;
}
bRandom[i * 2] = strHex.getBytes(StandardCharsets.UTF_8)[0];
bRandom[i * 2 + 1] = strHex.getBytes(StandardCharsets.UTF_8)[1];
} else {
bRandom[i] = rByte;
}
}
return new ByteSumSt(bRandom, capacityLength);
}
使用
ByteSumSt randomKey = TransUtilsGenerateRandom(16, false);
ishex为true时输出的是十六进制表示的字符串



