-1

我正在尝试使用 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 加密消息的密钥

4

1 回答 1

1

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

byte[] = { (byte) 0xac, (byte) 0x1d, ... };
于 2018-09-22T16:34:15.283 回答