🤬
  • ■ ■ ■ ■ ■ ■
    Projects/4_Server_for_file_transfers/README.md
     1 +# Server for file transfers
     2 + 
     3 +## :information_source: technologies used
     4 + 
     5 +* Java 11
     6 + 
     7 +## :information_source: How use?
     8 +```bash
     9 +# Clone the repository
     10 +$ git clone https://github.com/kurogai/100-redteam-projects
     11 + 
     12 +# Enter the repository
     13 +$ cd 100-redteam-projects/4_Server_for_file_transfers
     14 + 
     15 +# Open a terminal and run
     16 +$ cd src/com/server/ && java Server.java
     17 + 
     18 +# Open a new terminal and run
     19 +$ cd src/com/client/ && java Client.java
     20 +```
     21 + 
     22 +## Developer
     23 + 
     24 +[<img src="https://avatars.githubusercontent.com/u/32443720?v=4" width=115 > <br> <sub> Augusto Savi </sub>](https://github.com/AugustoSavi) |
     25 +| :---: |
     26 + 
  • ■ ■ ■ ■ ■ ■
    Projects/4_Server_for_file_transfers/src/com/client/Client.java
     1 +package com.client;
     2 + 
     3 +import javax.swing.*;
     4 +import java.awt.*;
     5 +import java.io.DataInputStream;
     6 +import java.io.DataOutputStream;
     7 +import java.io.File;
     8 +import java.io.FileInputStream;
     9 +import java.net.Socket;
     10 + 
     11 +public class Client {
     12 + private static DataOutputStream dataOutputStream = null;
     13 + private static DataInputStream dataInputStream = null;
     14 + private static String HOST= "localhost";
     15 + private static Integer PORT= 5000;
     16 + 
     17 + public static void main(String[] args) {
     18 + while (true) {
     19 + try {
     20 + JFileChooser file = new JFileChooser() {
     21 + @Override
     22 + protected JDialog createDialog(Component parent) throws HeadlessException {
     23 + // intercept the dialog created by JFileChooser
     24 + JDialog dialog = super.createDialog(parent);
     25 + dialog.setModal(true); // set modality (or setModalityType)
     26 + dialog.setAlwaysOnTop(true);
     27 + return dialog;
     28 + }
     29 + };
     30 + file.setFileSelectionMode(JFileChooser.FILES_ONLY);
     31 + 
     32 + int i = file.showSaveDialog(null);
     33 + if (1 == i) {
     34 + System.out.println("Arquivo não informado");
     35 + break;
     36 + } else {
     37 + Socket socket = new Socket(HOST,PORT);
     38 + 
     39 + dataInputStream = new DataInputStream(socket.getInputStream());
     40 + dataOutputStream = new DataOutputStream(socket.getOutputStream());
     41 + 
     42 + File arquivo = file.getSelectedFile();
     43 + sendFile(arquivo.getPath());
     44 + dataInputStream.close();
     45 + dataInputStream.close();
     46 + socket.close();
     47 + }
     48 + } catch (Exception e) {
     49 + e.printStackTrace();
     50 + }
     51 + }
     52 + }
     53 + 
     54 + private static void sendFile(String path) throws Exception{
     55 + int bytes = 0;
     56 + File file = new File(path);
     57 + FileInputStream fileInputStream = new FileInputStream(file);
     58 + 
     59 + // send file size
     60 + dataOutputStream.writeUTF(file.getName());
     61 + dataOutputStream.writeLong(file.length());
     62 + 
     63 + // break file into chunks
     64 + byte[] buffer = new byte[4*1024];
     65 + while ((bytes=fileInputStream.read(buffer))!=-1){
     66 + dataOutputStream.write(buffer,0,bytes);
     67 + dataOutputStream.flush();
     68 + }
     69 + fileInputStream.close();
     70 + }
     71 +}
  • ■ ■ ■ ■ ■ ■
    Projects/4_Server_for_file_transfers/src/com/server/Server.java
     1 +package com.server;
     2 + 
     3 +import java.io.DataInputStream;
     4 +import java.io.DataOutputStream;
     5 +import java.io.FileOutputStream;
     6 +import java.net.ServerSocket;
     7 +import java.net.Socket;
     8 + 
     9 +public class Server {
     10 + private static DataOutputStream dataOutputStream = null;
     11 + private static DataInputStream dataInputStream = null;
     12 + private static Integer PORT= 5000;
     13 + 
     14 + public static void main(String[] args) {
     15 + while (true){
     16 + try(ServerSocket serverSocket = new ServerSocket(PORT)){
     17 + System.out.println("listening to port: " + PORT);
     18 + Socket clientSocket = serverSocket.accept();
     19 + System.out.println(clientSocket+" connected.");
     20 + 
     21 + dataInputStream = new DataInputStream(clientSocket.getInputStream());
     22 + dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
     23 + 
     24 + receiveFile(dataInputStream.readUTF());
     25 + 
     26 + dataInputStream.close();
     27 + dataOutputStream.close();
     28 + clientSocket.close();
     29 + } catch (Exception e){
     30 + e.printStackTrace();
     31 + }
     32 + }
     33 + }
     34 + 
     35 + private static void receiveFile(String fileName) throws Exception{
     36 + int bytes = 0;
     37 + FileOutputStream fileOutputStream = new FileOutputStream(fileName);
     38 + 
     39 + long size = dataInputStream.readLong(); // read file size
     40 + byte[] buffer = new byte[4*1024];
     41 + while (size > 0 && (bytes = dataInputStream.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1) {
     42 + fileOutputStream.write(buffer,0,bytes);
     43 + size -= bytes; // read upto file size
     44 + }
     45 + fileOutputStream.close();
     46 + }
     47 +}
     48 + 
  • ■ ■ ■ ■ ■ ■
    Projects/5_Caesar_Cipher_tool/.gitignore
     1 +/target/
     2 +!.mvn/wrapper/maven-wrapper.jar
     3 + 
     4 +### STS ###
     5 +.apt_generated
     6 +.classpath
     7 +.factorypath
     8 +.project
     9 +.settings
     10 +.springBeans
     11 +.sts4-cache
     12 + 
     13 +### IntelliJ IDEA ###
     14 +.idea
     15 +*.iws
     16 +*.iml
     17 +*.ipr
     18 + 
     19 +### NetBeans ###
     20 +/nbproject/private/
     21 +/build/
     22 +/nbbuild/
     23 +/dist/
     24 +/nbdist/
     25 +/.nb-gradle/
     26 + 
     27 +*.xml.versionsBackup
  • ■ ■ ■ ■ ■ ■
    Projects/5_Caesar_Cipher_tool/README.md
     1 +<h1 align="center">Caesar Cipher tool</h1>
     2 + 
     3 +## :information_source: technologies used
     4 + 
     5 +* Java
     6 + 
     7 +<h2 align="center"> Encrypt </h2>
     8 + 
     9 +![image](https://user-images.githubusercontent.com/32443720/161679424-cc0fc73f-2837-4ece-b8a0-5faec3cb016b.png)
     10 + 
     11 +<h2 align="center"> Decrypt </h2>
     12 + 
     13 +![image](https://user-images.githubusercontent.com/32443720/161679536-6b60c399-3a74-49b1-9788-7c6b52155ded.png)
     14 + 
     15 + 
     16 + 
     17 +```java
     18 +private String cipher(String message, int offset) {
     19 + StringBuilder result = new StringBuilder();
     20 + for (char character : message.toCharArray()) {
     21 + if (character != ' ') {
     22 + int originalAlphabetPosition = character - 'a';
     23 + int newAlphabetPosition = (originalAlphabetPosition + offset) % 26;
     24 + char newCharacter = (char) ('a' + newAlphabetPosition);
     25 + result.append(newCharacter);
     26 + } else {
     27 + result.append(character);
     28 + }
     29 + }
     30 + return result.toString();
     31 +}
     32 +```
     33 + 
     34 +## :books: References
     35 + 
     36 +https://www.geeksforgeeks.org/caesar-cipher-in-cryptography/
  • ■ ■ ■ ■ ■ ■
    Projects/5_Caesar_Cipher_tool/src/Main.java
     1 +import javax.swing.*;
     2 +import javax.swing.border.EmptyBorder;
     3 +import javax.swing.event.DocumentEvent;
     4 +import javax.swing.event.DocumentListener;
     5 +import javax.swing.text.BadLocationException;
     6 +import javax.swing.text.NumberFormatter;
     7 +import java.text.NumberFormat;
     8 + 
     9 +public class Main extends JFrame {
     10 + private final JLabel labelInput, labelOutput, labelOffset;
     11 + private final JScrollPane scrollPaneInput, scrollPaneOutput;
     12 + private final JTextArea textAreaInput, textAreaOutput;
     13 + private final JFormattedTextField jTextFieldOffset;
     14 + private final JToggleButton jToggleButton;
     15 + private int offset = 3;
     16 + private String message = "";
     17 + 
     18 + public Main() {
     19 + JPanel contentPane = new JPanel();
     20 + setTitle("Caesar Cipher tool");
     21 + setResizable(false);
     22 + setBounds(100, 100, 800, 600);
     23 + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
     24 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     25 + setContentPane(contentPane);
     26 + contentPane.setLayout(null);
     27 + 
     28 + //toggle
     29 + jToggleButton = new JToggleButton("Decrypt");
     30 + jToggleButton.setBounds(90, 5 , 100,20);
     31 + jToggleButton.addActionListener(actionEvent -> {
     32 + if (jToggleButton.isSelected()) {
     33 + jToggleButton.setText("Encrypt");
     34 + cipherDecipher(message,offset);
     35 + }
     36 + else {
     37 + jToggleButton.setText("Decrypt");
     38 + cipherDecipher(message,offset);
     39 + }
     40 + });
     41 + contentPane.add(jToggleButton);
     42 + 
     43 + //offset
     44 + labelOffset = new JLabel("Offset:");
     45 + labelOffset.setBounds(265, 0 , 146,29);
     46 + 
     47 + NumberFormat longFormat = NumberFormat.getIntegerInstance();
     48 + NumberFormatter numberFormatter = new NumberFormatter(longFormat);
     49 + numberFormatter.setValueClass(Long.class); //optional, ensures you will always get a long value
     50 + numberFormatter.setMinimum(1L); //Optional
     51 + numberFormatter.setMaximum(28L); //Optional
     52 + 
     53 + jTextFieldOffset = new JFormattedTextField(numberFormatter);
     54 + jTextFieldOffset.setBounds(320, 5 , 30,20);
     55 + jTextFieldOffset.setText("3");
     56 + jTextFieldOffset.getDocument().addDocumentListener(new DocumentListener() {
     57 + @Override
     58 + public void insertUpdate(DocumentEvent documentEvent) {
     59 + offset = Integer.parseInt(jTextFieldOffset.getText());
     60 + cipherDecipher(message,offset);
     61 + }
     62 + 
     63 + @Override
     64 + public void removeUpdate(DocumentEvent documentEvent) {}
     65 + 
     66 + @Override
     67 + public void changedUpdate(DocumentEvent documentEvent) {
     68 + offset = Integer.parseInt(jTextFieldOffset.getText());
     69 + cipherDecipher(message,offset);
     70 + }
     71 + });
     72 + 
     73 + contentPane.add(labelOffset);
     74 + contentPane.add(jTextFieldOffset);
     75 + 
     76 + //input
     77 + labelInput = new JLabel("Input:");
     78 + labelInput.setBounds(10, 0, 146, 29);
     79 + contentPane.add(labelInput);
     80 + 
     81 + scrollPaneInput = new JScrollPane();
     82 + scrollPaneInput.setEnabled(false);
     83 + scrollPaneInput.setBounds(10, 30, 780, 250);
     84 + 
     85 + textAreaInput = new JTextArea();
     86 + textAreaInput.getDocument().addDocumentListener(new DocumentListener() {
     87 + @Override
     88 + public void insertUpdate(DocumentEvent documentEvent) {
     89 + try {
     90 + message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength());
     91 + cipherDecipher(message,offset);
     92 + } catch (BadLocationException e) {
     93 + e.printStackTrace();
     94 + }
     95 + }
     96 + 
     97 + @Override
     98 + public void removeUpdate(DocumentEvent documentEvent) {
     99 + try {
     100 + message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength());
     101 + cipherDecipher(message,offset);
     102 + } catch (BadLocationException e) {
     103 + e.printStackTrace();
     104 + }
     105 + 
     106 + }
     107 + 
     108 + @Override
     109 + public void changedUpdate(DocumentEvent documentEvent) {
     110 + try {
     111 + message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength());
     112 + cipherDecipher(message,offset);
     113 + } catch (BadLocationException e) {
     114 + e.printStackTrace();
     115 + }
     116 + 
     117 + }
     118 + });
     119 + scrollPaneInput.setViewportView(textAreaInput);
     120 + contentPane.add(scrollPaneInput);
     121 + 
     122 + //output
     123 + labelOutput = new JLabel("Output:");
     124 + labelOutput.setBounds(10, 275, 146, 29);
     125 + 
     126 + scrollPaneOutput = new JScrollPane();
     127 + scrollPaneOutput.setEnabled(false);
     128 + scrollPaneOutput.setBounds(10, 300, 780, 250);
     129 + 
     130 + textAreaOutput = new JTextArea();
     131 + scrollPaneOutput.setViewportView(textAreaOutput);
     132 + 
     133 + contentPane.add(labelOutput);
     134 + contentPane.add(scrollPaneOutput);
     135 + scrollPaneOutput.setViewportView(textAreaOutput);
     136 + }
     137 + 
     138 + private void cipherDecipher(String message, int offset){
     139 + if (!jToggleButton.isSelected()) {
     140 + textAreaOutput.setText(cipher(message, offset));
     141 + }
     142 + else{
     143 + textAreaOutput.setText(cipher(message,26 - (offset % 26)));
     144 + }
     145 + }
     146 + 
     147 + private String cipher(String message, int offset) {
     148 + StringBuilder result = new StringBuilder();
     149 + for (char character : message.toCharArray()) {
     150 + if (character != ' ') {
     151 + int originalAlphabetPosition = character - 'a';
     152 + int newAlphabetPosition = (originalAlphabetPosition + offset) % 26;
     153 + char newCharacter = (char) ('a' + newAlphabetPosition);
     154 + result.append(newCharacter);
     155 + } else {
     156 + result.append(character);
     157 + }
     158 + }
     159 + return result.toString();
     160 + }
     161 + 
     162 + public static void main(String[] args){
     163 + Main main = new Main();
     164 + main.setVisible(true);
     165 + }
     166 +}
     167 + 
  • ■ ■ ■ ■ ■ ■
    README.md
    skipped 34 lines
    35 35  [1] | TCP chat server | :heavy_check_mark:
    36 36  [2] | UDP chat server | :x:
    37 37  [3] | Multi-threaded UDP or TCP chat server | :x:
    38  -[4] | Server for file transfers | :x:
    39  -[5] | Caesar Cipher tool | :x:
     38 +[4] | Server for file transfers | :heavy_check_mark:
     39 +[5] | Caesar Cipher tool | :heavy_check_mark:
    40 40  [6] | TCP chat server -> The messages should be encoded with Caesar Cipher | :x:
    41 41  [7] | ROT13 Cipher | :x:
    42 42  [8] | UDP Chat server -> The messages should be encoded with ROT13 Cipher | :x:
    skipped 125 lines
Please wait...
Page is in error, reload to recover