Java 在字节[]keyValue中添加两个encr/decr键
我正在尝试实现Diffie-Hellman算法来为我的加密交换密钥,但在将总数分配到数组时遇到了问题。这就是我尝试过的:Java 在字节[]keyValue中添加两个encr/decr键,java,arrays,byte,Java,Arrays,Byte,我正在尝试实现Diffie-Hellman算法来为我的加密交换密钥,但在将总数分配到数组时遇到了问题。这就是我尝试过的: private static final String privatekey = "password"; private static final String publickey = "aloha"; private static final byte[] b = privatekey.getBytes(); private static final byte[] c =
private static final String privatekey = "password";
private static final String publickey = "aloha";
private static final byte[] b = privatekey.getBytes();
private static final byte[] c = publickey.getBytes();
private static byte[] keyValue;
public void createCommonKey(){
for(int i=0; i<b.length; i++){
keyValue[i]=(byte)(b[i]+c[i]);
}
System.out.println(keyValue);
}
private静态最终字符串privatekey=“password”;
私有静态最终字符串publickey=“aloha”;
私有静态最终字节[]b=privatekey.getBytes();
私有静态最终字节[]c=publickey.getBytes();
私有静态字节[]键值;
public void createCommonKey(){
对于(int i=0;i而言,NullPointerException
不是由数组的潜在长度不同引起的,而是由keyValue
为null
这一事实引起的。您可以添加
public void createCommonKey(){
keyValue = new byte[b.length]; // <-- This line
for(int i=0; i<b.length; i++){
...
您不需要在代码中初始化keyValue
。
public void createCommonKey()
{
int max = Math.max(b.length, c.length);
keyValue = new byte[max];
for(int i=0; i<max; i++)
{
int ib = i % b.length;
int ic = i % c.length;
keyValue[i]=(byte)(b[ib]+c[ic]);
}
}