繁体   English   中英

corejava书(11版)6.4节代码理解的一个问题

[英]A problem of understanding the code in the corejava book (11ed) section 6.4

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

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; }
}

new byte[] { (byte) -key[0] }表示一个新的字节数组,包含一个字节转换的“key”数组的第一个元素 ( [0] ),其符号被反转(例如通过-

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM