Last active
May 12, 2016 14:40
-
-
Save connlark/d166ed04c3d175f39c9325b1c98737e8 to your computer and use it in GitHub Desktop.
pls
This file contains 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.ArrayList; | |
/** | |
* Created by Connor on 5/9/16. | |
*/ | |
public class KeywordCipher extends Message { | |
private String key; | |
private ArrayList<Character> list; | |
public char[] ogAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); | |
public KeywordCipher(String s, String k) { | |
message = s.toUpperCase(); | |
this.key = k.toUpperCase(); | |
list = new ArrayList<Character>(); | |
generateAlphabet(key); | |
} | |
void generateAlphabet(String key){ | |
ArrayList<Character> alphas = new ArrayList<Character>(); | |
for (char aBet : ogAlpha) | |
alphas.add(aBet); | |
for (int i = 0; i < key.length(); i++) { | |
if (!list.contains(key.charAt(i)) && | |
!(key.charAt(i) < 65 || key.charAt(i) > 90)){ | |
list.add(key.charAt(i)); | |
alphas.remove(alphas.indexOf(key.charAt(i))); | |
} | |
} | |
char lastChar = list.get(list.size()-1); | |
for (int i = 0; i < alphas.size(); i++) { | |
if (alphas.get(i) < lastChar){ | |
alphas.add(alphas.remove(i)); | |
i--; | |
} | |
else break; | |
} | |
while (list.size() < 26) | |
list.add(alphas.remove(0)); | |
} | |
@Override | |
public void encrypt() { | |
if (isEncrypted||message.length()==0) return; | |
isEncrypted = true; | |
message = encryptHelper(message); | |
} | |
public String encryptHelper(String msg){ | |
if (msg.length() < 1) return msg; | |
return (msg.charAt(0) < 65 || msg.charAt(0) > 90) ? | |
msg.substring(0, 1) + encryptHelper(msg.substring(1)) : | |
String.valueOf(list.get(msg.charAt(0)-65)) + encryptHelper(msg.substring(1)); | |
} | |
@Override | |
public void decrypt() { | |
if (!isEncrypted) return; | |
for (int i = 0; i < message.length(); i++) { | |
message = (message.charAt(i) < 65 || message.charAt(i) > 90)? | |
message: message.substring(0,i) + ogAlpha[list.indexOf(message.charAt(i))] + message.substring(i+1); | |
} | |
isEncrypted = false; | |
} | |
} |
This file contains 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
/** | |
* Created by Connor on 5/3/16. | |
*/ | |
public abstract class Message implements Encryptable { | |
protected String message; | |
protected boolean isEncrypted; | |
public void setMessage(String s){ | |
message = s; | |
} | |
@Override | |
public String getMessage() { | |
return message; | |
} | |
@Override | |
public boolean isEncrypted() { | |
return isEncrypted; | |
} | |
} |
This file contains 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
/** | |
* Created by Connor on 5/3/16. | |
*/ | |
public class RailFenceCipher extends Message{ | |
public RailFenceCipher(String s) { | |
message = s; | |
isEncrypted = false; | |
} | |
@Override | |
public void encrypt() { | |
if (isEncrypted||message.length()==0) return; | |
isEncrypted = true; | |
message = encryptHelper(message)+encryptHelper(message.substring(1)); | |
} | |
public String encryptHelper(String str){ | |
if (str.length() <= 1)return str; | |
return str.charAt(0)+encryptHelper(str.substring(2)); | |
} | |
@Override | |
public void decrypt() { | |
if (!isEncrypted) return; | |
int mid = (int) Math.ceil(message.length()/2.0); | |
String a = message.substring(0,mid),z = message.substring(mid); | |
message = decryptHelper(a,z); | |
isEncrypted = false; | |
} | |
public String decryptHelper(String a,String z){ | |
if (a.length() <= 1 && z.length() <= 1)return a+z; | |
return ""+ a.charAt(0) + z.charAt(0) + decryptHelper(a.substring(1),z.substring(1)); | |
} | |
} |
This file contains 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
/** | |
* Created by Connor on 5/9/16. | |
*/ | |
public class ShiftCipher extends Message{ | |
private int shift; | |
public ShiftCipher(String s, int i) { | |
message = s.toUpperCase(); | |
if (i < 1 || i > 25) | |
throw new IllegalArgumentException("Shift value must be between 1 and 25."); | |
shift = i; | |
} | |
@Override | |
public void encrypt() { | |
if (isEncrypted)return; | |
isEncrypted = true; | |
message = encryptHelper(message); | |
} | |
public String encryptHelper(String str){ | |
if (str.length() < 1)return str; | |
return (str.charAt(0) < 65 || str.charAt(0) > 90) ? | |
str.substring(0, 1) + encryptHelper(str.substring(1)) : | |
String.valueOf((char) ((int) str.charAt(0) + shift)) + encryptHelper(str.substring(1)); | |
} | |
@Override | |
public void decrypt() { | |
if (!isEncrypted) return; | |
isEncrypted = false; | |
shift *= -1; | |
encrypt(); | |
isEncrypted = false; | |
shift *= -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment