Advertisement
yaramohamed1

Caesar Cipher

May 12th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9.  
  10.  
  11.  
  12. public class Main {
  13.  
  14.     public static void main(String[] args)
  15.     {
  16.        
  17.        
  18.         ArrayList<String> msg=readFromFile("plaintext.txt");
  19.         writeToFile(msg,"cipherText.txt");
  20.         ArrayList<String> normalText=readFromFile("cipherText.txt");
  21.         writeToFile(normalText,"plaintext2.txt");
  22.                
  23.     }
  24.     public static ArrayList<String> readFromFile(String path)
  25.     {
  26.         BufferedReader br = null;
  27.         //ArrayList[] msg = null;
  28.         ArrayList<String> msg = new ArrayList<String>();
  29.          
  30.         try {
  31.  
  32.             String sCurrentLine;
  33.  
  34.             br = new BufferedReader(new FileReader(path));
  35.             int i=0;
  36.             while ((sCurrentLine = br.readLine()) != null)
  37.             {
  38.                 if(path=="plaintext.txt")
  39.                 {
  40.                 String result=encode(sCurrentLine,3);
  41.                 msg.add(result);
  42.                 }
  43.                 else
  44.                 {
  45.                     String originalLine=decode(sCurrentLine);
  46.                     msg.add(originalLine);
  47.                    
  48.                 }
  49.                
  50.             }
  51.  
  52.         } catch (IOException e) {
  53.             e.printStackTrace();
  54.         } finally {
  55.             try {
  56.                 if (br != null)br.close();
  57.             } catch (IOException ex) {
  58.                 ex.printStackTrace();
  59.             }
  60.         }
  61.         return msg;
  62.        
  63.     }
  64.     public static void writeToFile(ArrayList<String> msg,String path)
  65.     {
  66.         try {
  67.              
  68.              
  69.             File file = new File(path);
  70.  
  71.             // if file doesnt exists, then create it
  72.             if (!file.exists()) {
  73.                 file.createNewFile();
  74.             }
  75.  
  76.             FileWriter fw = new FileWriter(file.getAbsoluteFile());
  77.             BufferedWriter bw = new BufferedWriter(fw);
  78.             Iterator<String> iterator = msg.iterator();
  79.             while (iterator.hasNext())
  80.             {
  81.                 //System.out.println(iterator.next());
  82.                 bw.write(iterator.next());
  83.                 bw.newLine();
  84.             }
  85.             bw.close();
  86.  
  87.             System.out.println("Done");
  88.  
  89.         } catch (IOException e) {
  90.             e.printStackTrace();
  91.         }
  92.  
  93.     }
  94.    
  95. public static String encode(String line,int key)
  96. {
  97.     char[] lineChar=line.toCharArray();
  98.     int offset=key%26+26;
  99.     StringBuilder secretMessage=new StringBuilder();
  100.     for(int i=0;i<lineChar.length;i++)
  101.     {
  102.         if(lineChar[i]==' ')
  103.         {
  104.             secretMessage.append(lineChar[i]);
  105.             continue;
  106.         }
  107.         else
  108.         {
  109.             secretMessage.append((char) ('A' + (lineChar[i] - 'A' + offset) % 26 ));
  110.            
  111.            
  112.         }
  113.     }
  114.     return secretMessage.toString();
  115. }
  116. public static  String decode(String line)
  117. {
  118.     return encode(line,23);
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement