public class Encryption { public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789"; private int shift; public Encryption(int shift) { this.shift = shift; } public String encrypt(String plaintext) { String ciphertext = ""; for (int i = 0; i < plaintext.length(); i++) { char c = plaintext.charAt(i); if (Character.isLetter(c) || Character.isDigit(c)) { // encrypt character c int index = ALPHABET.indexOf(c); int shiftedIndex = (index + this.shift) % ALPHABET.length(); c = ALPHABET.charAt(shiftedIndex); } ciphertext += c; updateShift(c); } return ciphertext; } private void updateShift(char c) { String previousCipher = "" + c + this.shift; int hash = previousCipher.hashCode(); this.shift = Math.abs(hash) % ALPHABET.length(); } }