Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. package com.example.cipher;
  2.  
  3.  
  4. /**
  5. * Created by Mehul Patel on 2/13/16.
  6. */
  7.  
  8. import java.util.*;
  9. import java.io.*;
  10.  
  11. public class Cipher{
  12.  
  13. public static boolean isFile(String file){
  14. File tmp = new File(file);
  15. boolean exists = tmp.exists();
  16. if (exists)
  17. return true;
  18. System.out.println("Bad filename");
  19. return false;
  20. }
  21.  
  22. public static String getFileContent(String file) {
  23. String str = "";
  24. try{
  25. FileReader fileReader = new FileReader(file);
  26. String line = "";
  27. BufferedReader bufferedReader = new BufferedReader(fileReader);
  28. while((line = bufferedReader.readLine()) != null) {
  29. str += line;
  30. }
  31. bufferedReader.close();
  32.  
  33.  
  34. } catch(FileNotFoundException ex) {
  35. System.out.println( "Bad filename");
  36. } catch (IOException e) {
  37. System.out.println("Something went wrong");
  38. }
  39. return str;
  40. }
  41.  
  42. public static String encryptMain(char c, char key_s) {
  43. String s = "";
  44. int key = (key_s >= 'a') ? (int) key_s - (int) 'a' : (int) key_s - (int) 'A';
  45. if (c >= 'A' && c <= 'Z') s += (char)((c - 'A' + key) % 26 + 'A');
  46. else if (c >= 'a' && c <= 'z') s += (char)((c - 'a' + key) % 26 + 'a');
  47. else s += c;
  48. return s;
  49. }
  50.  
  51. public static String decryptMain(char c, char key_s) {
  52. String s = "";
  53. int key = (key_s >= 'a') ? (int) key_s - (int) 'a' : (int) key_s - (int) 'A';
  54. if (c >= 'A' && c <= 'Z') s += (char)(Math.abs(((c - 'A' - key) % 26))+ 'A');
  55. else if (c >= 'a' && c <= 'z') s += (char)(Math.abs(((c - 'a' - key) % 26)) + 'a');
  56. else s += c;
  57. return s;
  58. }
  59.  
  60. public static void encrypt() throws FileNotFoundException, UnsupportedEncodingException {
  61.  
  62. Scanner input = new Scanner(System.in);
  63. System.out.print("Enter your key\n");
  64. String key = input.next();
  65. String ifs;
  66. while (true){
  67. System.out.println("Enter your input filename");
  68. ifs = input.next();
  69. if (isFile(ifs)) break;
  70. }
  71. String file_content = getFileContent(ifs);
  72. String output_content = "";
  73. int j = 0;
  74. for(int i = 0; i < file_content.length(); i++) {
  75. char a = file_content.charAt(i);
  76. if(j >= key.length()) j = 0;
  77. char b = key.charAt(j);
  78. output_content += encryptMain(a,b);
  79. j++;
  80. }
  81. System.out.print("Enter your output filename\n");
  82. String ofs = input.next(); //name of output txt file
  83.  
  84. PrintWriter writer = new PrintWriter(ofs, "UTF-8");
  85. writer.print(output_content);
  86. writer.close();
  87.  
  88. System.out.println("Successfully encrypted!!");
  89.  
  90.  
  91. }
  92. public static void decrypt() throws FileNotFoundException, UnsupportedEncodingException {
  93. Scanner input = new Scanner(System.in);
  94. System.out.print("Enter your key\n");
  95. String key = input.next();
  96.  
  97. String ifs;
  98. while (true){
  99. System.out.println("Enter your input filename");
  100. ifs = input.next();
  101. if (isFile(ifs)) break;
  102. }
  103. String file_content = getFileContent(ifs);
  104. String output_content = " ";
  105. int j = 0;
  106. for(int i = 0; i < file_content.length(); i++) {
  107. char a = file_content.charAt(i);
  108. if(j >= key.length()) j = 0;
  109. char b = key.charAt(j);
  110. output_content += decryptMain(a,b);
  111. j++;
  112. }
  113.  
  114. System.out.print("Enter your output filename\n");
  115. String ofs = input.next(); //name of output txt file
  116.  
  117. PrintWriter writer = new PrintWriter(ofs, "UTF-8");
  118. writer.print(output_content);
  119. writer.close();
  120.  
  121. System.out.println("Successfully decrypted!!");
  122. }
  123.  
  124. public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
  125. Scanner input = new Scanner(System.in);
  126.  
  127. System.out.println("Welcome to the encryption problem\n" +
  128. "What would you like to do?" );
  129. int choice = -1;
  130. while(choice != 0) {
  131. System.out.print("What would you like to do?\n" +
  132. "1 - Encrypt a file\n" +
  133. "2 - Decrypt a file\n" +
  134. "0 - Exit\n" +
  135. "Your Choice: ");
  136. choice = input.nextInt();
  137. if(choice == 1) encrypt();
  138. if(choice == 2) decrypt();
  139. }
  140.  
  141.  
  142.  
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement