Guest User

Untitled

a guest
Jul 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class Ciphark
  5. {
  6. private final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .";
  7. private String shiftSt;
  8. private String swapSt = ". ZYXWVUTSRQPONMLKJIHGFEDCBA";
  9. private int howFar;
  10. private static String message;
  11. private ArrayList scrambled;
  12.  
  13. public static void main(String args[])
  14. {
  15. Scanner keyboard = new Scanner(System.in);
  16. System.out.print("Type in a message to encode or to decode");
  17. message = keyboard.nextLine();
  18. System.out.print("Press e if you wish to encode or d if you wish to decode the message");
  19. String m = keyboard.next();
  20. if(m.equals("e"))
  21. {
  22. Ciphark lawlz = new Ciphark();
  23. message = lawlz.swap(message);
  24. message = lawlz.shift(message);
  25. message = lawlz.swap(message);
  26. message = lawlz.scramble(message);
  27. System.out.println(message);
  28. }
  29. else if(m.equals("d"))
  30. {
  31.  
  32. }
  33. }
  34.  
  35. public Ciphark()
  36. {
  37. howFar = (int)(Math.random()*28);
  38. shiftSt = alpha.substring(howFar) + alpha.substring(0,howFar);
  39. }
  40.  
  41. public String swap(String toSwap)
  42. {
  43. toSwap = toSwap.toUpperCase();
  44. String meh = "";
  45. for(int i = 0;i<toSwap.length();i++)
  46. {meh+=swapSt.charAt(alpha.indexOf(toSwap.charAt(i)));}
  47. return meh;
  48. }
  49.  
  50. public String shift(String toShift)
  51. {
  52. toShift = toShift.toUpperCase();
  53. String meh = "";
  54. for(int i = 0;i<toShift.length();i++)
  55. {meh+=shiftSt.charAt(alpha.indexOf(toShift.charAt(i)));}
  56. return meh;
  57. }
  58.  
  59. public String deShift(String toDeShift)
  60. {
  61. toDeShift = toDeShift.toUpperCase();
  62. String meh = "";
  63. for(int i = 0;i<toDeShift.length();i++)
  64. {meh+=alpha.charAt(shiftSt.indexOf(toDeShift.charAt(i)));}
  65. return meh;
  66. }
  67.  
  68. public String scramble(String toScramble)
  69. {
  70. String meh = "";
  71. ArrayList alph = new ArrayList();
  72. for(int i = 0;i<alpha.length();i++)
  73. {alph.add(alpha.charAt(i));}
  74.  
  75. scrambled = new ArrayList();
  76. while(alph.size()>0)
  77. {
  78. int spot = (int)(Math.random()*alph.size());
  79. scrambled.add(alph.get(spot));
  80. alph.remove(spot);
  81. }
  82.  
  83. toScramble = toScramble.toUpperCase();
  84. for(int i = 0;i<toScramble.length();i++)
  85. {
  86. char p = toScramble.charAt(i);
  87. int ind = indexOff(alph,p);
  88. meh+=scrambled.get(ind);
  89. }
  90. return meh.toLowerCase();
  91. }
  92.  
  93. public int indexOff(ArrayList a,char b)
  94. {
  95. int index = 0;
  96. while(index<a.size())
  97. {
  98. if(a.get(index) == b) return index;
  99. }
  100. return -1;
  101. }
  102. }
Add Comment
Please, Sign In to add comment