Skip to content

Commit

Permalink
First Commit.
Browse files Browse the repository at this point in the history
Model classes, and concrete product classes crated
  • Loading branch information
gulbalasalamov committed Feb 27, 2021
1 parent 776cd7b commit 7e1b19f
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
56 changes: 56 additions & 0 deletions model/AlgBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package model;

import java.io.*;

public abstract class AlgBase {
public String data;

public void encrypt(File inputFile, File outputFile) {
readData(inputFile);
encode();
writeData(outputFile);
}

public void decrypt(File inputFile, File outputFile) {
readData(inputFile);
decode();
writeData(outputFile);
}

public abstract void encode();

public abstract void decode();

private void readData(File inputFile) {
if ("".equals(data)) {
if (inputFile != null) {
readMsg(inputFile);
}
}
}

public void readMsg(File inputFile) {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
StringBuilder msg = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
msg.append(line);
}
data = msg.toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private void writeData(File outputFile) {
if (outputFile != null) {
try (FileWriter writer = new FileWriter(outputFile)) {
writer.write(data);
} catch (IOException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println(data);
}
}
}
58 changes: 58 additions & 0 deletions product/AlgShift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package product;

import model.AlgBase;

public class AlgShift extends AlgBase {
String mode;
int key;

public AlgShift(String data, String mode, int key) {
this.data = data;
this.mode = mode;
this.key = key;
}

@Override
public void decode() {
StringBuilder decryptedMsg = new StringBuilder(data);
for (int i = 0; i < decryptedMsg.length(); i++) {
String s = decryptedMsg.substring(i, i + 1);
if (s.matches("[a-z]")) {
char c = (char) (decryptedMsg.charAt(i) - key);
if (c < 'a') {
c = (char) ('z' + c - 'a' + 1);
}
decryptedMsg.setCharAt(i, c);
} else if (s.matches("[A-Z]")) {
char c = (char) (decryptedMsg.charAt(i) - key);
if (c < 'A') {
c = (char) ('Z' + c - 'A' + 1);
}
decryptedMsg.setCharAt(i, c);
}
}
data = decryptedMsg.toString();
}

@Override
public void encode() {
StringBuilder encryptedMsg = new StringBuilder(data);
for (int i = 0; i < encryptedMsg.length(); i++) {
String s = encryptedMsg.substring(i, i + 1);
if (s.matches("[a-z]")) {
char c = (char) (encryptedMsg.charAt(i) + key);
if (c > 'z') {
c = (char) ('a' + c - 'z' - 1);
}
encryptedMsg.setCharAt(i, c);
} else if (s.matches("[A-Z]")) {
char c = (char) (encryptedMsg.charAt(i) + key);
if (c > 'Z') {
c = (char) ('A' + c - 'Z' - 1);
}
encryptedMsg.setCharAt(i, c);
}
}
data = encryptedMsg.toString();
}
}
32 changes: 32 additions & 0 deletions product/AlgUnicode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package product;

import model.AlgBase;

public class AlgUnicode extends AlgBase {
String mode;
int key;

public AlgUnicode(String data, String mode, int key) {
this.data = data;
this.mode = mode;
this.key = key;
}

@Override
public void encode() {
StringBuilder encryptedMsg = new StringBuilder();
for (int i = 0; i < data.length(); i++) {
encryptedMsg.append((char) (data.charAt(i) + key));
}
data = encryptedMsg.toString();
}

@Override
public void decode() {
StringBuilder encryptedMsg = new StringBuilder();
for (int i = 0; i < data.length(); i++) {
encryptedMsg.append((char) (data.charAt(i) - key));
}
data = encryptedMsg.toString();
}
}

0 comments on commit 7e1b19f

Please sign in to comment.