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

Java 如何在字节数组中正确使用SecureRandom输出?,java,arrays,byte,aes,secure-random,Java,Arrays,Byte,Aes,Secure Random,我正在尝试使用SecureRandom生成一个随机密钥,但在将输出放入字节数组时遇到了问题。它无法识别SecureRandom的输出 public static String byteToHex(byte[] hash) { StringBuilder sb = new StringBuilder(hash.length * 2); for(byte b: hash) { sb.append(String.format("%

我正在尝试使用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中使用它,您必须使用数字。这些将被解释为整数,因此您必须将它们转换为字节(因为您的数字足够小):


ac
是一个标识符
0xac
(或者更好的
0xac
)是十六进制
int
文本。正确的方法是:
byte[]k={(byte)0xAC,(byte)0x1D,(byte)0x71,(byte)0xC8,
等等。我习惯于在C中处理字节,所以我不知道。谢谢!
byte[] = { (byte) 0xac, (byte) 0x1d, ... };