Cryptography Program


SUBMITTED BY: Biggergig

DATE: March 15, 2018, 10:58 p.m.

UPDATED: March 15, 2018, 11:24 p.m.

FORMAT: Text only

SIZE: 6.2 kB

HITS: 1063

  1. import java.util.*;
  2. import java.util.Arrays;
  3. import chn.util.*;
  4. /**
  5. * Write a description of class RandomAlpha here.
  6. *
  7. * @author (your name)
  8. * @version (a version number or a date)
  9. */
  10. public class RandomAlpha
  11. {
  12. public static void main(String[] args){
  13. ConsoleIO sys = new ConsoleIO();
  14. RandomAlpha rnd = new RandomAlpha();
  15. System.out.print("Would you like to encode/decode (e/d)\n>");
  16. char choice = sys.readLine().charAt(0);
  17. System.out.print("Enter the name of the file\n>");
  18. String name = sys.readLine();
  19. FileInput inFile = new FileInput(name);
  20. String key = "";
  21. if(choice=='e'){
  22. key = rnd.arrToString(rnd.getAlphabet());
  23. }
  24. else if(choice=='d'){
  25. //System.out.println("1");
  26. key=inFile.readLine();
  27. //System.out.println("2");
  28. }
  29. inFile.close();
  30. inFile = new FileInput(name);
  31. ArrayList<String> file = rnd.readFile(inFile);
  32. System.out.println("\nKey is: "+key);
  33. String[] temp = rnd.listToArr(file);
  34. if(choice == 'e'){
  35. temp = rnd.encodeArr(key,temp);
  36. }
  37. else if(choice =='d')
  38. temp=rnd.decodeArr(key,temp);
  39. rnd.exportToFile(key,temp);
  40. //rnd.test();
  41. }
  42. public void test(){
  43. RandomAlpha rnd = new RandomAlpha();
  44. String[] arr = rnd.getAlphabet();
  45. String key = rnd.arrToString(arr);
  46. ConsoleIO sys = new ConsoleIO();
  47. System.out.println("Key is: "+rnd.arrToString(arr));
  48. System.out.println(" abcdefghijklmnopqrstuvwxyz");
  49. System.out.print("Enter the name of the file\n>");
  50. FileInput inFile = new FileInput(sys.readLine());
  51. ArrayList<String> file = rnd.readFile(inFile);
  52. file=rnd.toLower(file);
  53. System.out.println("Lower: "+file);
  54. String[] encoded = rnd.encodeArr(rnd.arrToString(arr),rnd.listToArr(file));
  55. for(int i = 0;i<encoded.length;i++)
  56. System.out.println("Encoded line "+(i+1)+": "+encoded[i]);
  57. String[] decoded = rnd.decodeArr(rnd.arrToString(arr),encoded);
  58. for(int i = 0;i<decoded.length;i++)
  59. System.out.println("Decoded line "+(i+1)+": "+decoded[i]);
  60. exportToFile(key,encoded);
  61. }
  62. public void exportToFile(String key, String[] arr){
  63. FileOutput outFile = new FileOutput("output.txt");
  64. outFile.println(key);
  65. for(int i = 0;i<arr.length;i++){
  66. outFile.println(arr[i]);
  67. }
  68. outFile.close();
  69. }
  70. public String[] encodeArr(String inp, String[] file){
  71. String[] out = new String[file.length];
  72. for(int i = 0; i<file.length;i++){
  73. out[i]=encode(inp,file[i]);
  74. }
  75. return out;
  76. }
  77. public String[] decodeArr(String inp, String[] file){
  78. String[] out = new String[file.length];
  79. for(int i = 0; i<file.length;i++){
  80. out[i]=decode(inp,file[i]);
  81. }
  82. return out;
  83. }
  84. public String encode(String inp,String txt){
  85. char[] key = new char[26];
  86. for(int i = 0;i<26;i++){
  87. key[i]=inp.charAt(i);
  88. }
  89. String temp = "";
  90. for(int i =0;i<txt.length();i++){
  91. char cur = txt.charAt(i);
  92. if(cur>96&&cur<123){
  93. temp+=""+key[cur-'a'];
  94. }
  95. else{
  96. temp+=""+cur;
  97. }
  98. }
  99. return temp;
  100. }
  101. public String decode(String inp,String txt){
  102. char[] key = new char[26];
  103. for(int i = 0;i<26;i++){
  104. key[i]=inp.charAt(i);
  105. }
  106. String temp = "";
  107. for(int i =0;i<txt.length();i++){
  108. char cur = txt.charAt(i);
  109. if(cur>96&&cur<123){
  110. temp+=""+(char)('a'+indexOfArr(key,cur));
  111. //System.out.println("Decoding "+cur+" to "+(char)('a'+indexOfArr(key,cur))+" because index is "+indexOfArr(key,cur));
  112. }
  113. else{
  114. temp+=""+cur;
  115. }
  116. }
  117. //System.out.println("\n");
  118. return temp;
  119. }
  120. public static int indexOfArr(char[] arr, char inp){
  121. int ret = -1;
  122. for(int i =0;i<arr.length;i++){
  123. if(arr[i]==inp){
  124. return i;
  125. }
  126. }
  127. return ret;
  128. }
  129. public ArrayList<String> toLower (ArrayList<String> inp){
  130. ArrayList<String> temp = new ArrayList<String>();
  131. for(int i = 0;i<inp.size();i++){
  132. temp.add(inp.get(i).toLowerCase());
  133. //System.out.println(temp);
  134. }
  135. return temp;
  136. }
  137. public ArrayList<String> readFile(FileInput inFile){
  138. ArrayList<String> out = new ArrayList<String>();
  139. while(inFile.hasMoreLines()){
  140. //System.out.println("hit");
  141. out.add(inFile.readLine());
  142. //System.out.println("err");
  143. }
  144. return out;
  145. }
  146. public String arrToString(String[] inp){
  147. String temp="";
  148. for(int i = 0;i<inp.length;i++){
  149. temp+=inp[i];
  150. }
  151. return temp;
  152. }
  153. public String[] getAlphabet(){
  154. ArrayList<Character> all = new ArrayList<Character>();
  155. ArrayList<String> alpha = new ArrayList<String>();
  156. for(int i = 97;i<123;i++){
  157. all.add((char)i);
  158. }
  159. //System.out.println(all);
  160. while(all.size()>0){
  161. alpha.add(""+all.remove((int)(Math.random()*all.size())));
  162. //System.out.println(all.size());
  163. }
  164. //System.out.println("Returning "+alpha);
  165. //System.out.println(alpha.size());
  166. return listToArr(alpha);
  167. }
  168. public String[] listToArr(ArrayList<String> inp){
  169. String[] ret = new String[inp.size()];
  170. for(int i = 0;i<ret.length;i++){
  171. ret[i]=inp.get(i);
  172. //System.out.println(inp.get(i));
  173. }
  174. //System.out.println(Arrays.toString(ret));
  175. return ret;
  176. }
  177. }

comments powered by Disqus