Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/crypto/Cipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package encryptdecrypt;

interface Cipher {
void encrypt(DataProcess dataProcess);
void decrypt(DataProcess dataProcess);
}
19 changes: 13 additions & 6 deletions src/crypto/Crypto.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package crypto;
package encryptdecrypt;

import java.util.Scanner;
class Crypto {
private Cipher cipher;

public class Crypto {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// write your code here
void setCipher(Cipher cipher) {
this.cipher = cipher;
}

void doMagic(DataProcess dataProcess) {
if (dataProcess.getOperation().equals("enc")) {
this.cipher.encrypt(dataProcess);
} else {
this.cipher.decrypt(dataProcess);
}
}
}
87 changes: 87 additions & 0 deletions src/crypto/DataProcess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package encryptdecrypt;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

class DataProcess {
private String algorithm;
private String operation = "enc";
private String dataOutFile = null;
private String data = "";
private int key = 0;

String getAlgorithm() {
return algorithm;
}

String getOperation() {
return operation;
}

String getDataOutFile() {
return dataOutFile;
}

String getData() {
return data;
}

int getKey() {
return key;
}

DataProcess(String[] args) {
boolean checker = false;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-mode": {
this.operation = args[++i];
break;
}
case "-key": {
this.key = Integer.parseInt(args[++i]);
break;
}
case "-data": {
this.data = args[++i];
checker = true;
break;
}
case "-in": {
this.data = DataProcess.dataFromFile(args[++i]);
checker = true;
break;
}
case "-out": {
this.dataOutFile = args[++i];
break;
}
case "-alg": {
this.algorithm = args[++i];
break;
}
}
}
if (!checker) {
Scanner sc = new Scanner(System.in);
this.data = sc.nextLine();
this.key = sc.nextInt();
sc.close();
}
}

private static String dataFromFile(String dataInFile) {
StringBuilder str = new StringBuilder();
try {
FileReader in = new FileReader(dataInFile);
Scanner scanner = new Scanner(in);
while (scanner.hasNext()) {
str.append(scanner.nextLine()).append("\n");
}
} catch (FileNotFoundException e) {
System.out.println("There is no file with such a name: " + dataInFile);
}
return str.toString();
}
}
19 changes: 19 additions & 0 deletions src/crypto/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package encryptdecrypt;

public class Main {
public static void main(String[] args) {
Crypto crypto = new Crypto();
DataProcess dataProcess = new DataProcess(args);

switch (dataProcess.getAlgorithm()) {
case "unicode":
crypto.setCipher(new UnicodeAlg());
crypto.doMagic(dataProcess);
break;
case "shift":
crypto.setCipher(new ShiftAlg());
crypto.doMagic(dataProcess);
break;
}
}
}
57 changes: 57 additions & 0 deletions src/crypto/ShiftAlg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package encryptdecrypt;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

class ShiftAlg implements Cipher {
private String abc = "abcdefghijklmnopqrstuvwxyz";

@Override
public void encrypt(DataProcess dataProcess) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < dataProcess.getData().length(); i++) {
if (Character.isLetter(dataProcess.getData().charAt(i))) {
int num = abc.indexOf(dataProcess.getData().charAt(i)) + dataProcess.getKey();
result.append(abc.charAt(num > abc.length() - 1 ? num - abc.length() : num));
} else {
result.append(dataProcess.getData().charAt(i));
}
}
if (!dataProcess.getDataOutFile().isEmpty()) {
try {
FileWriter fw = new FileWriter(new File(dataProcess.getDataOutFile()));
fw.write(result.toString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(result);
}
}

@Override
public void decrypt(DataProcess dataProcess) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < dataProcess.getData().length(); i++) {
if (Character.isLetter(dataProcess.getData().charAt(i))) {
int num = abc.indexOf(dataProcess.getData().charAt(i)) - dataProcess.getKey();
result.append(abc.charAt(num < 0 ? num + abc.length() : num));
} else {
result.append(dataProcess.getData().charAt(i));
}
}
if (!dataProcess.getDataOutFile().isEmpty()) {
try {
FileWriter fw = new FileWriter(new File(dataProcess.getDataOutFile()));
fw.write(result.toString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(result);
}
}
}
49 changes: 49 additions & 0 deletions src/crypto/UnicodeAlg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package encryptdecrypt;

import java.io.File;
import java.io.FileWriter;

class UnicodeAlg implements Cipher{

@Override
public void encrypt(DataProcess dataProcess) {
int[] numChars = new int[dataProcess.getData().length()];
StringBuilder result = new StringBuilder();
for (int i = 0; i < numChars.length; i++) {
numChars[i] = Character.codePointAt(dataProcess.getData(), i);
result.append((char) (numChars[i] + dataProcess.getKey()));
}
if (dataProcess.getDataOutFile() != null) {
try {
FileWriter fw = new FileWriter(new File(dataProcess.getDataOutFile()));
fw.write(result.toString());
fw.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
} else {
System.out.println(result);
}
}

@Override
public void decrypt(DataProcess dataProcess) {
int[] numChars = new int[dataProcess.getData().length()];
StringBuilder result = new StringBuilder();
for (int i = 0; i < numChars.length; i++) {
numChars[i] = Character.codePointAt(dataProcess.getData(), i);
result.append((char) (numChars[i] - dataProcess.getKey()));
}
if (dataProcess.getDataOutFile() != null) {
try {
FileWriter fw = new FileWriter(new File(dataProcess.getDataOutFile()));
fw.write(result.toString());
fw.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
} else {
System.out.println(result);
}
}
}