Advertisement
Guest User

Untitled

a guest
Sep 11th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. // Author: Isak Andersson
  2. // License: Public Domain
  3.  
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.util.HashMap;
  7. import java.util.Scanner;
  8.  
  9. import javax.swing.JFileChooser;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.filechooser.FileNameExtensionFilter;
  12.  
  13.  
  14. public class IsaKrypt {
  15.    
  16.     private JFileChooser chooser;
  17.     private FileNameExtensionFilter filter;
  18.     private HashMap<char [], String> encryptionDict;
  19.    
  20.     private String key;
  21.    
  22.     public IsaKrypt() {
  23.         chooser = new JFileChooser();
  24.         filter = new FileNameExtensionFilter("Supported files", "txt", "isakrypt");
  25.         chooser.setFileFilter(filter);
  26.         encryptionDict = initDictionary();
  27.     }
  28.  
  29.     public void start() throws Exception {
  30.         String filepath = pickFile();
  31.        
  32.         if (getFileExtension(filepath).equals("txt")) {
  33.             System.out.println("It is a .txt file! Let's encrypt!");
  34.             encypt(filepath);
  35.         }
  36.         else if (getFileExtension(filepath).equals("isakrypt")) {
  37.             System.out.println("It is a .isakrypt file! Let's decrypt!");
  38.             decrypt(filepath);
  39.         }
  40.         else {
  41.             throw new Exception("Unsupported file format. Isakrypt only accepts .txt and .isakrypt");
  42.         }
  43.     }
  44.    
  45.     private String pickFile() throws Exception {
  46.         int dialogVal = chooser.showOpenDialog(null);
  47.         String fp = "";
  48.        
  49.         if (dialogVal == JFileChooser.APPROVE_OPTION) {
  50.             System.out.println("File to use: " + chooser.getSelectedFile().getName());
  51.             System.out.println("Absolute path: " + chooser.getSelectedFile().getAbsolutePath());
  52.             fp = chooser.getSelectedFile().getAbsolutePath();
  53.         }
  54.         else {
  55.             throw new Exception("No file was selected");
  56.         }
  57.        
  58.         return fp;
  59.     }
  60.    
  61.     private String getFileExtension(String fp) throws Exception {
  62.         String ext = "";
  63.        
  64.         System.out.println("Entering for loop.... filepath length is: "+fp.length());
  65.        
  66.         for (int index = fp.length()-1; index >= 0; index--) {
  67.             System.out.println("Index: "+index);
  68.             if (fp.charAt(index) == '.') {
  69.                 ext = fp.substring(index+1);
  70.                 break;
  71.             }
  72.         }
  73.        
  74. //      if (ext.equals("")) {
  75. //          throw new Exception("No file extension was found");
  76. //      }
  77.        
  78.         return ext;
  79.     }
  80.    
  81.     private String read(String filepath) throws Exception {
  82.         StringBuilder builder = new StringBuilder();
  83.         String txt = "";
  84.        
  85.         try {
  86.             Scanner mrscanner = new Scanner(new File(filepath));
  87.             while(mrscanner.hasNext()) {
  88.                 builder.append(mrscanner.nextLine());
  89.                 builder.append("\n");
  90.             }
  91.             txt = builder.toString();
  92.            
  93.             if (txt.equals("")) {
  94.                 throw new Exception("Empty string, SOMETHING is wrong, file empty?");
  95.             }
  96.            
  97.         } catch (FileNotFoundException e) {
  98.             System.err.println("File could not be read");
  99.             e.printStackTrace();
  100.         }
  101.        
  102.         return txt;
  103.     }
  104.    
  105.     // Kallas om filen har .txt extension
  106.     private void encypt(String fp) throws Exception {
  107.         String filecontent = read(fp);
  108.         String kryptedVersion = "";
  109.         StringBuilder cryptBuilder = new StringBuilder();
  110.        
  111.         System.out.println("File content: "+filecontent);
  112.        
  113.         key = JOptionPane.showInputDialog(null, "Enter your encryption key");
  114.         char[] valueOnIndex;
  115.        
  116.         for (int index = 0; index < filecontent.length(); index++) {
  117.             System.out.println(String.valueOf(String.valueOf(filecontent.charAt(index)).toCharArray())); // Prints correctly I know it is converting more than necessary, I just wanted to emulate what's below
  118.             valueOnIndex = String.valueOf(filecontent.charAt(index)).toCharArray();
  119.             if (encryptionDict.containsKey(valueOnIndex)) {
  120.                 // Append to string
  121.                 cryptBuilder.append(encryptionDict.get(valueOnIndex));
  122.             }
  123.             else {
  124.                 // If a character is not in the map, just add it as it is
  125.                 cryptBuilder.append(valueOnIndex);
  126.             }
  127.         }
  128.        
  129.         System.out.println("Encrypted version: \n" + cryptBuilder.toString());
  130.     }
  131.    
  132.     // Kallas om filen hap .isakrypt extension
  133.     private void decrypt(String fp) throws Exception {
  134.         String filecontent = read(fp);
  135.     }
  136.    
  137.     private HashMap<char [], String> initDictionary() {
  138.         HashMap<char [], String> d = new HashMap<char [], String>();
  139.        
  140.         d.put("a".toCharArray(), "!\"#¤");
  141.         d.put("b".toCharArray(), "¤#\"!");
  142.         d.put("c".toCharArray(), "\"#¤%");
  143.         d.put("d".toCharArray(), "%¤#\"");
  144.         d.put("e".toCharArray(), "#¤%&");
  145.         d.put("f".toCharArray(), "&%¤#");
  146.         d.put("g".toCharArray(), "¤%&/");
  147.         d.put("h".toCharArray(), "/&%¤");
  148.         d.put("i".toCharArray(), "%&/(");
  149.         d.put("j".toCharArray(), "(/&%");
  150.         d.put("k".toCharArray(), "&/()");
  151.         d.put("l".toCharArray(), ")(/&");
  152.         d.put("m".toCharArray(), "/()=");
  153.         d.put("n".toCharArray(), "=)(/");
  154.         d.put("o".toCharArray(), "()=?");
  155.         d.put("p".toCharArray(), "?=)(");
  156.         d.put("q".toCharArray(), ")=?!");
  157.         d.put("r".toCharArray(), "!?=)");
  158.         d.put("s".toCharArray(), "=?!\"");
  159.         d.put("t".toCharArray(), "\"!?=");
  160.         d.put("u".toCharArray(), "?!\"#");
  161.         d.put("v".toCharArray(), "#\"!?");
  162.         d.put("w".toCharArray(), ";:*^");
  163.         d.put("x".toCharArray(), "^*:;");
  164.         d.put("y".toCharArray(), ":*^>");
  165.         d.put("z".toCharArray(), ">^*:");
  166.        
  167.         d.put("A".toCharArray(), "¶¡@£");
  168.         d.put("B".toCharArray(), "£@¡¶");
  169.         d.put("C".toCharArray(), "¡@£$");
  170.         d.put("D".toCharArray(), "$£@¡");
  171.         d.put("E".toCharArray(), "@£$€");
  172.         d.put("F".toCharArray(), "€$£@");
  173.         d.put("G".toCharArray(), "£$€¥");
  174.         d.put("H".toCharArray(), "¥€$£");
  175.         d.put("I".toCharArray(), "$€¥{");
  176.         d.put("J".toCharArray(), "{¥€$");
  177.         d.put("K".toCharArray(), "€¥{[");
  178.         d.put("L".toCharArray(), "[{¥€");
  179.         d.put("M".toCharArray(), "¥{[]");
  180.         d.put("N".toCharArray(), "][{¥");
  181.         d.put("O".toCharArray(), "{[]}");
  182.         d.put("P".toCharArray(), "}][{");
  183.         d.put("Q".toCharArray(), "[]}\\");
  184.         d.put("R".toCharArray(), "\\}][");
  185.         d.put("S".toCharArray(), "]}\\±");
  186.         d.put("T".toCharArray(), \\}]");
  187.         d.put("U".toCharArray(), "}\\±¶");
  188.         d.put("V".toCharArray(), "¶±\\}");
  189.         d.put("W".toCharArray(), "\\±¶¡");
  190.         d.put("X".toCharArray(), "¡¶±\\");
  191.         d.put("Y".toCharArray(), "±¶¡@");
  192.         d.put("Z".toCharArray(), "@¡¶±");
  193.        
  194.         d.put("0".toCharArray(), "¶±¡\\");
  195.         d.put("1".toCharArray(), "\\¡±¶");
  196.         d.put("2".toCharArray(), \\@}");
  197.         d.put("3".toCharArray(), "}@\\¡");
  198.         d.put("4".toCharArray(), "@}£]");
  199.         d.put("5".toCharArray(), "]£}@");
  200.         d.put("6".toCharArray(), "£]$[");
  201.         d.put("7".toCharArray(), "[$]£");
  202.         d.put("8".toCharArray(), "$[€{");
  203.         d.put("9".toCharArray(), "[€]$");
  204.        
  205.         d.put(" ".toCharArray(), "_/|\\_");
  206.        
  207.         return d;
  208.     }
  209.    
  210.     public static void main(String[] args) throws Exception {
  211.         IsaKrypt p = new IsaKrypt();
  212.         p.start();
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement