Skip to content

Instantly share code, notes, and snippets.

@onacit
Last active June 8, 2020 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onacit/52baf25eb1184516eeb302d9cbc01ebf to your computer and use it in GitHub Desktop.
Save onacit/52baf25eb1184516eeb302d9cbc01ebf to your computer and use it in GitHub Desktop.
Caesar cipher
import java.util.Arrays;
import java.util.Objects;
public final class CaesarCipher {
/**
* Creates a new instance with specified shift.
*
* @param shift the shift
*/
public CaesarCipher(final int shift) {
super();
this.shift = shift;
}
public byte[] encrypt(final byte[] decrypted) {
Objects.requireNonNull(decrypted);
final byte[] encrypted = new byte[decrypted.length];
for (int i = 0; i < encrypted.length; i++) {
encrypted[i] = (byte) (decrypted[i] + shift);
}
return encrypted;
}
public byte[] decrypt(final byte[] encrypted) {
Objects.requireNonNull(encrypted);
final byte[] decrypted = new byte[encrypted.length];
for (int i = 0; i < decrypted.length; i++) {
decrypted[i] = (byte) (encrypted[i] - shift);
}
return decrypted;
}
private final int shift;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment