'how use Substitution cipher with Socket in java

I'm trying to use substitution cipher to encrypt and decrypt Socket massage in the java app.

this is the server.java file :

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Serveur2 {

    public static void main(String[] args) {
        
        try {
            System.out.println("Waiting Connection Request...\n");
            ServerSocket ss = new ServerSocket(3401);
            Socket CltSo = ss.accept();
            InputStreamReader is = new InputStreamReader(CltSo.getInputStream());
            OutputStreamWriter os = new OutputStreamWriter(CltSo.getOutputStream());
                        char[] arr = new char[500];
            System.out.println("Connection established ...\n");
            is.read(arr);
                        String starr = new String(arr);
            System.out.println("A message received...\n" + starr);
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}

and this is client.java file :

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
//import java.util.Scanner;

public class Client2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            System.out.println("Connection attempt...\n");
            Socket Cltsoc = new Socket("localhost", 3401);
            System.out.println("Successful connection...\n");
            InputStreamReader is = new InputStreamReader(Cltsoc.getInputStream());
            OutputStreamWriter os =  new OutputStreamWriter(Cltsoc.getOutputStream());
                        InputStreamReader estd = new InputStreamReader(System.in);
                        char[] arr = new char[500];
            System.out.println("Enter message ...\n");
                        estd.read(arr);
            os.write(arr);
            System.out.println("Massage send...\n"+ new String(arr));
                        os.flush();
            Cltsoc.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

my code work well , but I want to Encrypt message with function y=ax+b mod 26 (with a=3, b=5)

  • View encrypted message
  • Exchange the encrypted message with the server Decrypt the message using the inverse function ( x=(y/a - b/a) mod 26), in our example x=(9y+7)mod26
  • Send the decrypted message to the Outputstream client

can someone help me with this !

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source