Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Isak Andersson
- // License: Public Domain
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.HashMap;
- import java.util.Scanner;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class IsaKrypt {
- private JFileChooser chooser;
- private FileNameExtensionFilter filter;
- private HashMap<char [], String> encryptionDict;
- private String key;
- public IsaKrypt() {
- chooser = new JFileChooser();
- filter = new FileNameExtensionFilter("Supported files", "txt", "isakrypt");
- chooser.setFileFilter(filter);
- encryptionDict = initDictionary();
- }
- public void start() throws Exception {
- String filepath = pickFile();
- if (getFileExtension(filepath).equals("txt")) {
- System.out.println("It is a .txt file! Let's encrypt!");
- encypt(filepath);
- }
- else if (getFileExtension(filepath).equals("isakrypt")) {
- System.out.println("It is a .isakrypt file! Let's decrypt!");
- decrypt(filepath);
- }
- else {
- throw new Exception("Unsupported file format. Isakrypt only accepts .txt and .isakrypt");
- }
- }
- private String pickFile() throws Exception {
- int dialogVal = chooser.showOpenDialog(null);
- String fp = "";
- if (dialogVal == JFileChooser.APPROVE_OPTION) {
- System.out.println("File to use: " + chooser.getSelectedFile().getName());
- System.out.println("Absolute path: " + chooser.getSelectedFile().getAbsolutePath());
- fp = chooser.getSelectedFile().getAbsolutePath();
- }
- else {
- throw new Exception("No file was selected");
- }
- return fp;
- }
- private String getFileExtension(String fp) throws Exception {
- String ext = "";
- System.out.println("Entering for loop.... filepath length is: "+fp.length());
- for (int index = fp.length()-1; index >= 0; index--) {
- System.out.println("Index: "+index);
- if (fp.charAt(index) == '.') {
- ext = fp.substring(index+1);
- break;
- }
- }
- // if (ext.equals("")) {
- // throw new Exception("No file extension was found");
- // }
- return ext;
- }
- private String read(String filepath) throws Exception {
- StringBuilder builder = new StringBuilder();
- String txt = "";
- try {
- Scanner mrscanner = new Scanner(new File(filepath));
- while(mrscanner.hasNext()) {
- builder.append(mrscanner.nextLine());
- builder.append("\n");
- }
- txt = builder.toString();
- if (txt.equals("")) {
- throw new Exception("Empty string, SOMETHING is wrong, file empty?");
- }
- } catch (FileNotFoundException e) {
- System.err.println("File could not be read");
- e.printStackTrace();
- }
- return txt;
- }
- // Kallas om filen har .txt extension
- private void encypt(String fp) throws Exception {
- String filecontent = read(fp);
- String kryptedVersion = "";
- StringBuilder cryptBuilder = new StringBuilder();
- System.out.println("File content: "+filecontent);
- key = JOptionPane.showInputDialog(null, "Enter your encryption key");
- char[] valueOnIndex;
- for (int index = 0; index < filecontent.length(); index++) {
- 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
- valueOnIndex = String.valueOf(filecontent.charAt(index)).toCharArray();
- if (encryptionDict.containsKey(valueOnIndex)) {
- // Append to string
- cryptBuilder.append(encryptionDict.get(valueOnIndex));
- }
- else {
- // If a character is not in the map, just add it as it is
- cryptBuilder.append(valueOnIndex);
- }
- }
- System.out.println("Encrypted version: \n" + cryptBuilder.toString());
- }
- // Kallas om filen hap .isakrypt extension
- private void decrypt(String fp) throws Exception {
- String filecontent = read(fp);
- }
- private HashMap<char [], String> initDictionary() {
- HashMap<char [], String> d = new HashMap<char [], String>();
- d.put("a".toCharArray(), "!\"#¤");
- d.put("b".toCharArray(), "¤#\"!");
- d.put("c".toCharArray(), "\"#¤%");
- d.put("d".toCharArray(), "%¤#\"");
- d.put("e".toCharArray(), "#¤%&");
- d.put("f".toCharArray(), "&%¤#");
- d.put("g".toCharArray(), "¤%&/");
- d.put("h".toCharArray(), "/&%¤");
- d.put("i".toCharArray(), "%&/(");
- d.put("j".toCharArray(), "(/&%");
- d.put("k".toCharArray(), "&/()");
- d.put("l".toCharArray(), ")(/&");
- d.put("m".toCharArray(), "/()=");
- d.put("n".toCharArray(), "=)(/");
- d.put("o".toCharArray(), "()=?");
- d.put("p".toCharArray(), "?=)(");
- d.put("q".toCharArray(), ")=?!");
- d.put("r".toCharArray(), "!?=)");
- d.put("s".toCharArray(), "=?!\"");
- d.put("t".toCharArray(), "\"!?=");
- d.put("u".toCharArray(), "?!\"#");
- d.put("v".toCharArray(), "#\"!?");
- d.put("w".toCharArray(), ";:*^");
- d.put("x".toCharArray(), "^*:;");
- d.put("y".toCharArray(), ":*^>");
- d.put("z".toCharArray(), ">^*:");
- d.put("A".toCharArray(), "¶¡@£");
- d.put("B".toCharArray(), "£@¡¶");
- d.put("C".toCharArray(), "¡@£$");
- d.put("D".toCharArray(), "$£@¡");
- d.put("E".toCharArray(), "@£$€");
- d.put("F".toCharArray(), "€$£@");
- d.put("G".toCharArray(), "£$€¥");
- d.put("H".toCharArray(), "¥€$£");
- d.put("I".toCharArray(), "$€¥{");
- d.put("J".toCharArray(), "{¥€$");
- d.put("K".toCharArray(), "€¥{[");
- d.put("L".toCharArray(), "[{¥€");
- d.put("M".toCharArray(), "¥{[]");
- d.put("N".toCharArray(), "][{¥");
- d.put("O".toCharArray(), "{[]}");
- d.put("P".toCharArray(), "}][{");
- d.put("Q".toCharArray(), "[]}\\");
- d.put("R".toCharArray(), "\\}][");
- d.put("S".toCharArray(), "]}\\±");
- d.put("T".toCharArray(), "±\\}]");
- d.put("U".toCharArray(), "}\\±¶");
- d.put("V".toCharArray(), "¶±\\}");
- d.put("W".toCharArray(), "\\±¶¡");
- d.put("X".toCharArray(), "¡¶±\\");
- d.put("Y".toCharArray(), "±¶¡@");
- d.put("Z".toCharArray(), "@¡¶±");
- d.put("0".toCharArray(), "¶±¡\\");
- d.put("1".toCharArray(), "\\¡±¶");
- d.put("2".toCharArray(), "¡\\@}");
- d.put("3".toCharArray(), "}@\\¡");
- d.put("4".toCharArray(), "@}£]");
- d.put("5".toCharArray(), "]£}@");
- d.put("6".toCharArray(), "£]$[");
- d.put("7".toCharArray(), "[$]£");
- d.put("8".toCharArray(), "$[€{");
- d.put("9".toCharArray(), "[€]$");
- d.put(" ".toCharArray(), "_/|\\_");
- return d;
- }
- public static void main(String[] args) throws Exception {
- IsaKrypt p = new IsaKrypt();
- p.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement