如何正确使用字节数组中的 SecureRandom 输出?

How to use SecureRandom output in a byte array properly?

我正在尝试使用 SecureRandom 生成随机密钥,但在将输出放入字节数组时遇到问题。它无法识别 SecureRandom 的输出。

public static String byteToHex(byte[] hash) {

        StringBuilder sb = new StringBuilder(hash.length * 2);
           for(byte b: hash) {
                sb.append(String.format("%02x", b));
           }
        return sb.toString();
    }
    SecureRandom r = new SecureRandom();
            byte a[] = new byte[16]; 
            r.nextBytes(a);

我将输出硬编码为字节数组:

byte[] k = {ac,1d,71,c8,96,bd,f7,d5,03,38,bc,46,a2,b4,f1,a8};

我得到的错误是:

Multiple markers at this line
    - a2 cannot be resolved to a variable
    - bd cannot be resolved to a variable
    - d5 cannot be resolved to a variable
    - b4 cannot be resolved to a variable
    - c8 cannot be resolved to a variable
    - f7 cannot be resolved to a variable
    - a8 cannot be resolved to a variable
    - f1 cannot be resolved to a variable
    - Type mismatch: cannot convert from double 
     to byte
    - ac cannot be resolved to a variable
    - bc cannot be resolved to a variable
    - Type mismatch: cannot convert from double 
     to byte

我想将其用作使用 AES 加密消息的密钥

您从之前执行的代码段中获得的值已被格式化为无效的 Java 代码。 这些数字似乎是用十六进制表示法写的。 要在 Java 中使用它,您必须 prepend 0x 到数字。然后这些将被解释为整数,因此您必须将它们转换为字节(当您的数字足够小时才有效):

byte[] = { (byte) 0xac, (byte) 0x1d, ... };