理解corejava书籍(11ed)第6.4节中代码的问题

理解corejava书籍(11ed)第6.4节中代码的问题,java,encryption,Java,Encryption,在corejava的6.4节中,作者介绍了一个关于Caesar密码的代码示例,在解密方法中,代码new byte[]{byte-key[0]}是什么意思 package serviceLoader; public interface Cipher { byte[] encrypt(byte[] source, byte[] key); byte[] decrypt(byte[] source, byte[] key); int strength(); } package serviceLoad

在corejava的6.4节中,作者介绍了一个关于Caesar密码的代码示例,在解密方法中,代码new byte[]{byte-key[0]}是什么意思

package serviceLoader;
public interface Cipher
{
byte[] encrypt(byte[] source, byte[] key);
byte[] decrypt(byte[] source, byte[] key);
int strength();
}

package serviceLoader.impl;
public class CaesarCipher implements Cipher
{
public byte[] encrypt(byte[] source, byte[] key)
{
var result = new byte[source.length];
for (int i = 0; i < source.length; i++)
result[i] = (byte)(source[i] + key[0]);
return result;
}
public byte[] decrypt(byte[] source, byte[] key)
{
return encrypt(source, new byte[] { (byte) -key[0] });
}
public int strength() { return 1; }
}

新字节[]{byte-key[0]}表示一个新的字节数组,其中包含一个由字节铸造的密钥数组的第一个元素[0],该元素的符号被反转,例如通过-

当您根据密钥更改字母时,会使用凯撒密码。该键指定必须移动的字符数。当您解码密码时,需要向后移动的字符数与加密时移动的字符数相同。当加密密钥为5时,解密密钥必须为-5。我建议您将密钥类型更改为整数,因为您只需要给定数组中的第一个条目。