-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model classes, and concrete product classes crated
- Loading branch information
1 parent
776cd7b
commit 7e1b19f
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |