Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.cipher;
- /**
- * Created by Mehul Patel on 2/13/16.
- */
- import java.util.*;
- import java.io.*;
- public class Cipher{
- public static boolean isFile(String file){
- File tmp = new File(file);
- boolean exists = tmp.exists();
- if (exists)
- return true;
- System.out.println("Bad filename");
- return false;
- }
- public static String getFileContent(String file) {
- String str = "";
- try{
- FileReader fileReader = new FileReader(file);
- String line = "";
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- while((line = bufferedReader.readLine()) != null) {
- str += line;
- }
- bufferedReader.close();
- } catch(FileNotFoundException ex) {
- System.out.println( "Bad filename");
- } catch (IOException e) {
- System.out.println("Something went wrong");
- }
- return str;
- }
- public static String encryptMain(char c, char key_s) {
- String s = "";
- int key = (key_s >= 'a') ? (int) key_s - (int) 'a' : (int) key_s - (int) 'A';
- if (c >= 'A' && c <= 'Z') s += (char)((c - 'A' + key) % 26 + 'A');
- else if (c >= 'a' && c <= 'z') s += (char)((c - 'a' + key) % 26 + 'a');
- else s += c;
- return s;
- }
- public static String decryptMain(char c, char key_s) {
- String s = "";
- int key = (key_s >= 'a') ? (int) key_s - (int) 'a' : (int) key_s - (int) 'A';
- if (c >= 'A' && c <= 'Z') s += (char)(Math.abs(((c - 'A' - key) % 26))+ 'A');
- else if (c >= 'a' && c <= 'z') s += (char)(Math.abs(((c - 'a' - key) % 26)) + 'a');
- else s += c;
- return s;
- }
- public static void encrypt() throws FileNotFoundException, UnsupportedEncodingException {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter your key\n");
- String key = input.next();
- String ifs;
- while (true){
- System.out.println("Enter your input filename");
- ifs = input.next();
- if (isFile(ifs)) break;
- }
- String file_content = getFileContent(ifs);
- String output_content = "";
- int j = 0;
- for(int i = 0; i < file_content.length(); i++) {
- char a = file_content.charAt(i);
- if(j >= key.length()) j = 0;
- char b = key.charAt(j);
- output_content += encryptMain(a,b);
- j++;
- }
- System.out.print("Enter your output filename\n");
- String ofs = input.next(); //name of output txt file
- PrintWriter writer = new PrintWriter(ofs, "UTF-8");
- writer.print(output_content);
- writer.close();
- System.out.println("Successfully encrypted!!");
- }
- public static void decrypt() throws FileNotFoundException, UnsupportedEncodingException {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter your key\n");
- String key = input.next();
- String ifs;
- while (true){
- System.out.println("Enter your input filename");
- ifs = input.next();
- if (isFile(ifs)) break;
- }
- String file_content = getFileContent(ifs);
- String output_content = " ";
- int j = 0;
- for(int i = 0; i < file_content.length(); i++) {
- char a = file_content.charAt(i);
- if(j >= key.length()) j = 0;
- char b = key.charAt(j);
- output_content += decryptMain(a,b);
- j++;
- }
- System.out.print("Enter your output filename\n");
- String ofs = input.next(); //name of output txt file
- PrintWriter writer = new PrintWriter(ofs, "UTF-8");
- writer.print(output_content);
- writer.close();
- System.out.println("Successfully decrypted!!");
- }
- public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
- Scanner input = new Scanner(System.in);
- System.out.println("Welcome to the encryption problem\n" +
- "What would you like to do?" );
- int choice = -1;
- while(choice != 0) {
- System.out.print("What would you like to do?\n" +
- "1 - Encrypt a file\n" +
- "2 - Decrypt a file\n" +
- "0 - Exit\n" +
- "Your Choice: ");
- choice = input.nextInt();
- if(choice == 1) encrypt();
- if(choice == 2) decrypt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement