Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active October 8, 2019 00:47
Show Gist options
  • Save dgodfrey206/94e0141e1df68a88c4fcca2fe30a4774 to your computer and use it in GitHub Desktop.
Save dgodfrey206/94e0141e1df68a88c4fcca2fe30a4774 to your computer and use it in GitHub Desktop.
MJ
import java.util.*;
/*public*/ interface Cipher {
public abstract String encode(String plainText);
}
/*public*/ class ShiftN_Cipher implements Cipher {
public ShiftN_Cipher(int shifts) {
this.shifts = shifts;
}
public ShiftN_Cipher() {
this.shifts = (int)(Math.random() * 50);
}
public String encode(String plainText) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<plainText.length(); i++) {
sb.append(shift(plainText.charAt(i)));
}
return sb.toString();
}
private char shiftOver(char c, int s) {
if ('a' <= c && c <= 'z') {
return (char)(('a') + mod(c - (int)'a' + s, 26));
}
else if ('A' <= c && c <= 'Z') {
return (char)(('A') + mod(c - (int)'A' + s, 26));
}
// punctuation
char val = '\u0161';
return (char)(val + c + s);
}
private char shift(char c) {
return shiftOver(c, shifts);
}
private int mod(int x, int n) {
return (x % n + n) % n;
}
public String decode(String encrypted) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<encrypted.length(); i++) {
sb.append(shiftOver(encrypted.charAt(i), -shifts));
}
return sb.toString();
}
private int shifts;
}
/*public*/ class CipherN_Shuffle implements Cipher {
public CipherN_Shuffle(int shuffles) {
this.shuffles = shuffles;
}
public CipherN_Shuffle() {
this.shuffles = (int)(Math.random() * 50);
}
public String shuffle(String text) {
StringBuilder s = new StringBuilder();
int mid = text.length() / 2 + text.length() % 2, right = text.length() - 1;
int i = 0, j = mid;
while (i < mid && j <= right) {
s.append(text.charAt(i++));
s.append(text.charAt(j++));
}
while (i < mid) {
s.append(text.charAt(i++));
}
while (j <= right) {
s.append(text.charAt(j++));
}
return s.toString();
}
public String encode(String text) {
for (int i=0; i<shuffles; i++) {
text = shuffle(text);
}
return text;
}
private String deshuffle(String s) {
StringBuilder temp = new StringBuilder(s.length());
int k = 0;
for (int i = 0; i < s.length(); i += 2) {
temp.setCharAt(k++, s.charAt(i));
}
for (int i = 1; i < s.length(); i += 2) {
temp.setCharAt(k++, s.charAt(i));
}
return temp.toString();
}
public String decode(String text) {
for (int i=0; i<shuffles; i++) {
text = deshuffle(text);
}
return text;
}
private int shuffles;
}
/*public*/ class RandomMap_Cipher implements Cipher {
RandomMap_Cipher() {
}
private char mapping(char[] alphaStr, char c) {
if ('a' <= c && c <= 'z') {
return alphaStr[c - (int)'a'];
}
return alphaStr[c - (int)'A' + 26];
}
private void createNewMap(char[] map, Random rand) {
for (int i=0; i<map.length; i++) {
swap(map, i, rand.nextInt(map.length - i) + i);
}
}
int UnicodeSum(String txt) {
int sum = 0;
for (int i=0; i<txt.length(); i++) {
sum += (int)txt.charAt(i);
}
return sum;
}
public String encode(String text) {
Random rand = new Random(UnicodeSum(text));
char[] alphaStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
createNewMap(alphaStr, rand);
StringBuilder sb = new StringBuilder();
for (int i=0; i<text.length(); i++) {
sb.append(mapping(alphaStr, text.charAt(i)));
}
return sb.toString();
}
private void swap(char[] m, int i, int j) {
char temp = m[i];
m[i] = m[j];
m[j] = temp;
}
}
class Main {
public static void main(String[] args) {
ShiftN_Cipher rmc = new ShiftN_Cipher(0);
System.out.println(rmc.encode("'"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment