Reputation: 11
In the section 6.4 of corejava, author introduces a code example about Caesar cipher,what the code new byte[] { (byte) -key[0] }
mean in the decrypt method???
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; }
}
Upvotes: 0
Views: 48
Reputation: 4574
new byte[] { (byte) -key[0] }
means a new byte array containing a byte-casted 1st element ([0]
) of "key" array, whose sign is being inverted (eg by the -
)
Upvotes: 1