Skip to content

Commit

Permalink
Media transfer now works on localhost, more testing pending. Few mino…
Browse files Browse the repository at this point in the history
…r updates to the encryption-decryption programs.
  • Loading branch information
ThePurpleJedi committed Feb 12, 2022
1 parent 9ee713a commit 2376017
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 157 deletions.
12 changes: 1 addition & 11 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/com/mercurys/encryption/Decryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ public class Decryption {

private String chars;
private String key;
private int actualMessageLength;
private int actualMessageLength = 0;

public Decryption() {
this.actualMessageLength = 0;
this.chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
this.key = "";
}
Expand All @@ -30,7 +29,7 @@ public String decrypt(final String msg) {
this.reTranslateMessageUsingKey(words);
words = this.perfectSquareReSwap(words);
words = this.unReverseMessage(words);
this.antiRotateBy7(words);
this.antiRotateMessageBy7(words);
return Decryption.getFinalDecryptedMessage(words);
}

Expand Down Expand Up @@ -124,7 +123,7 @@ private String[] unReverseMessage(String[] words) {
/**
* Rotates the letters back by 7
*/
private void antiRotateBy7(final String[] words) {
private void antiRotateMessageBy7(final String[] words) {
for (int i = 0; i < words.length; i++) {
final StringBuilder encodedWord = new StringBuilder();

Expand Down
1 change: 0 additions & 1 deletion src/com/mercurys/encryption/Encryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ private void generateRandomKey(char[] key) {
}
key[i] = c;
}
System.out.println("key length = " + key.length);
}

private void translateUsingKey(String[] words, char[] key) {
Expand Down
52 changes: 22 additions & 30 deletions src/com/mercurys/threads/ReadMediaThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.file.*;
import java.text.MessageFormat;
import java.util.Scanner;
import java.text.*;
import java.util.Calendar;

public class ReadMediaThread extends Thread {

private final Decryption decryption = new Decryption();
private final Scanner sc = new Scanner(System.in);
private final BufferedReader reader;
private final String sentBy;
private DataInputStream inputStream;
private boolean exit;
private int i;


public ReadMediaThread(final Socket socket, final String sentBy) throws IOException {
if (!socket.isClosed()) {
Expand All @@ -28,7 +27,6 @@ public ReadMediaThread(final Socket socket, final String sentBy) throws IOExcept
reader = new BufferedReader(new InputStreamReader(this.inputStream));
this.sentBy = sentBy;
this.exit = false;
this.i = 0;
}

@Override
Expand All @@ -55,12 +53,10 @@ private void readAndPrintMessages(final BufferedReader reader) throws IOExceptio
inBoundLine = reader.readLine();
if (inBoundLine == null) {
break;
} else if (inBoundLine.startsWith("/image")) {
} else if (decryption.decrypt(inBoundLine).startsWith("/image")) {
final BufferedImage image = this.getImageAsBufferedImage();
this.downloadBufferedImage(image);
//TODO: Why is it printing the byte array
} else {
System.out.println(inBoundLine);
} else if (decryption.getKeyFromMessage(inBoundLine.split(" +")).length() == 88) {
System.out.println(this.sentBy + ": " + this.decryption.decrypt(inBoundLine));
}
}
Expand All @@ -72,37 +68,33 @@ private void downloadBufferedImage(final BufferedImage image) {
}
final String s = System.getProperty("file.separator");
final String downloadPath =
MessageFormat.format("{0}{1}Pictures{1}Mercurys{1}ReceivedImage{2}.png",
System.getProperty("user.home"), s, this.i++);
MessageFormat.format("{0}{1}Pictures{1}Mercurys{1}{3}{1}ReceivedImage{2}.png",
System.getProperty("user.home"), s, new SimpleDateFormat("HHmmss")
.format(Calendar.getInstance().getTime()), new SimpleDateFormat("dd.MM.yyyy")
.format(Calendar.getInstance().getTime()));
try {
Files.createDirectories(Paths.get(downloadPath.substring(0, downloadPath.lastIndexOf(s)) + s));
ImageIO.write(image, "png", new File(downloadPath));
} catch (final IOException e) {
e.printStackTrace();
}
System.out.println("The image file has been downloaded as " + downloadPath);
System.out.println("[M. Console]: The image file has been downloaded as " + downloadPath);
}

private BufferedImage getImageAsBufferedImage() {
System.out.println("[Mercurys]: Attention! " +
"The user at the other end is attempting to send you an image file. " +
"\nDo you wish to download it? [Y/n]");

final String perm = this.sc.next();
if (perm.equalsIgnoreCase("Y")) {
try {
final byte[] imgSize = new byte[4];
this.inputStream.readFully(imgSize);
final byte[] imgArr = new byte[ByteBuffer.wrap(imgSize).asIntBuffer().get()];
this.inputStream.readFully(imgArr);
return ImageIO.read(new ByteArrayInputStream(imgArr));
System.out.println("[M. Console]: Attention! " +
"The user at the other end is attempting to send you an image file.");
System.out.println("[M. Console]: Reading image file...");
try {
final byte[] imgSize = new byte[4];
this.inputStream.readFully(imgSize);
final byte[] imgArr = new byte[ByteBuffer.wrap(imgSize).asIntBuffer().get()];
this.inputStream.readFully(imgArr);
return ImageIO.read(new ByteArrayInputStream(imgArr));

} catch (final IOException e) {
System.out.println("Exception Occurred!");
e.printStackTrace();
return null;
}
} else {
} catch (final IOException e) {
System.out.println("Exception Occurred!");
e.printStackTrace();
return null;
}
}
Expand Down
50 changes: 0 additions & 50 deletions src/com/mercurys/unfinished/DownloadTester.java

This file was deleted.

59 changes: 0 additions & 59 deletions src/com/mercurys/unfinished/Imagician.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/com/mercurys/unfinished/MediaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,4 @@ private void closeConnection(final ReadMediaThread readMediaThread) throws IOExc
this.outputStream.close();
System.out.println("Closing connection... Goodbye!");
}
}

}
Binary file added src/resources/images/RealImageTest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2376017

Please sign in to comment.