Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Ciphark
- {
- private final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .";
- private String shiftSt;
- private String swapSt = ". ZYXWVUTSRQPONMLKJIHGFEDCBA";
- private int howFar;
- private static String message;
- private ArrayList scrambled;
- public static void main(String args[])
- {
- Scanner keyboard = new Scanner(System.in);
- System.out.print("Type in a message to encode or to decode");
- message = keyboard.nextLine();
- System.out.print("Press e if you wish to encode or d if you wish to decode the message");
- String m = keyboard.next();
- if(m.equals("e"))
- {
- Ciphark lawlz = new Ciphark();
- message = lawlz.swap(message);
- message = lawlz.shift(message);
- message = lawlz.swap(message);
- message = lawlz.scramble(message);
- System.out.println(message);
- }
- else if(m.equals("d"))
- {
- }
- }
- public Ciphark()
- {
- howFar = (int)(Math.random()*28);
- shiftSt = alpha.substring(howFar) + alpha.substring(0,howFar);
- }
- public String swap(String toSwap)
- {
- toSwap = toSwap.toUpperCase();
- String meh = "";
- for(int i = 0;i<toSwap.length();i++)
- {meh+=swapSt.charAt(alpha.indexOf(toSwap.charAt(i)));}
- return meh;
- }
- public String shift(String toShift)
- {
- toShift = toShift.toUpperCase();
- String meh = "";
- for(int i = 0;i<toShift.length();i++)
- {meh+=shiftSt.charAt(alpha.indexOf(toShift.charAt(i)));}
- return meh;
- }
- public String deShift(String toDeShift)
- {
- toDeShift = toDeShift.toUpperCase();
- String meh = "";
- for(int i = 0;i<toDeShift.length();i++)
- {meh+=alpha.charAt(shiftSt.indexOf(toDeShift.charAt(i)));}
- return meh;
- }
- public String scramble(String toScramble)
- {
- String meh = "";
- ArrayList alph = new ArrayList();
- for(int i = 0;i<alpha.length();i++)
- {alph.add(alpha.charAt(i));}
- scrambled = new ArrayList();
- while(alph.size()>0)
- {
- int spot = (int)(Math.random()*alph.size());
- scrambled.add(alph.get(spot));
- alph.remove(spot);
- }
- toScramble = toScramble.toUpperCase();
- for(int i = 0;i<toScramble.length();i++)
- {
- char p = toScramble.charAt(i);
- int ind = indexOff(alph,p);
- meh+=scrambled.get(ind);
- }
- return meh.toLowerCase();
- }
- public int indexOff(ArrayList a,char b)
- {
- int index = 0;
- while(index<a.size())
- {
- if(a.get(index) == b) return index;
- }
- return -1;
- }
- }
Add Comment
Please, Sign In to add comment