Last active
June 8, 2020 13:34
-
-
Save onacit/52baf25eb1184516eeb302d9cbc01ebf to your computer and use it in GitHub Desktop.
Caesar cipher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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