Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by sniff ( 7 years ago )
package encryptdecrypt;

import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void encrypt(int len, char[] str, int key) {
        for (int i = 0; i < len; i++) {
            str[i] += key;
        }
    }

    public static void decrypt(int len, char[] str, int key) {
        for (int i = 0; i < len; i++) {
            str[i] -= key;
        }
    }

    public static void main(String[] args) {
        String mode = "enc";
        int key = 0;
        String data = null;
        String current;
        String inputFile = null;
        String outputFile = null;
        PrintWriter writer;
        char[] str;

        try {
            for (int i = 0; i < args.length; i++) {
                current = args[i];
                if (current.equals("-mode")) {
                    mode = args[++i];
                } else if (current.equals("-key")) {
                    key = Integer.parseInt(args[++i]);
                } else if (current.equals("-data")) {
                    data = args[++i];
                } else if (current.equals("-in")) {
                    inputFile = args[++i];
                } else if (current.equals("-out")) {
                    outputFile = args[++i];
                }
            }

            if (data != null) {
                str = data.toCharArray();
            } else if (inputFile != null) {
                str = new String (Files.readAllBytes(Paths.get(inputFile))).toCharArray();
            } else {
                data = "";
                str = data.toCharArray();
            }
            if (mode.equals("enc")) {
                encrypt(str.length, str, key);
            } else if (mode.equals(("dec"))) {
                decrypt(str.length, str, key);
            }

            if (outputFile != null) {
                writer = new PrintWriter(outputFile);
                writer.println(str);
            } else {
                System.out.println(str);
            }
        } catch (Exception e) {
            System.out.println("Error");
        }
    }
}

 

Revise this Paste

Your Name: Code Language: