0

So, I just want to add caesar cipher on my java chat application, how do I add them? I want to encrypt client server and then decrypt server on the server.

By the way, here is my java chat code :

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class chat
{
    static void Client() throws IOException 
    {
        String ip;
        BufferedReader cmb=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Masukkan IP Server : ");
        ip=cmb.readLine();
        Socket client=null;
        client=new Socket(""+ip,8888);
        BufferedReader sin=new BufferedReader(new
        InputStreamReader(client.getInputStream()));
        PrintStream sout=new PrintStream(client.getOutputStream());
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        String s;

        while (true)
        {
            System.out.print("Client : ");
            s=stdin.readLine();
            sout.println(s);
            s=sin.readLine();
            System.out.print("Server : "+s+"\n");
            if(s.equalsIgnoreCase("Bye"))
            break;
        }
        stdin.close();
        sout.close();
        sin.close();
        client.close();
    }
    static void Server() throws IOException {
    ServerSocket server=null;
    Socket client=null;

    try
    {
        server=new ServerSocket(8888);
        System.out.println("Server telah online");
        client=server.accept();
        System.out.println("Client telah masuk dan bisa online");
    }

    catch(IOException e)
    {
        System.out.println(e.getMessage());
        System.exit(-1);
    }

    System.out.println("Silahkan chat");
    InputStream masuk=client.getInputStream();
    OutputStream keluar=client.getOutputStream();
    BufferedReader in=new BufferedReader(new
    InputStreamReader(client.getInputStream()));
    PrintStream out=new PrintStream(client.getOutputStream());
    BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
    String pesan;

    while (true)
    {
        pesan=in.readLine();
        if(pesan.equalsIgnoreCase("stop"))
        {
            out.println("sampai jumpa");
            break;
        }
    System.out.println("Client chat : "+pesan);
    System.out.print("Server : ");
    pesan=stdin.readLine();
    out.println(pesan);
    }

    server.close();
    client.close();
    in.close();
    out.close();
    stdin.close();
}

public static void main(String args[]) throws IOException 
{
    int pil;
    System.out.println("Masukkan Angka yang akan Anda Pilih");
    System.out.println("1. Server");
    System.out.println("2. Client");
    System.out.println("3. Keluar");
    System.out.println();
    System.out.println("Masukkan Pilihan : ");
    Scanner input = new Scanner(System.in);
    pil = Integer.parseInt(input.next());
    switch(pil)
    {
    case 1:
    Server();
    break;
    case 2:
    Client();
    break;
    case 3:
    System.out.println();
    System.out.println("Terima Kasih Telah Menggunakan Aplikasi Ini");
    break;

    default :
    System.out.println("Maaf Keyword Yang Anda Masukkan Salah");
    System.out.println("Silahkan Ulangi Lagi");
    break;
    }
}}

I know how the caesar cipher works, but I dont know how to implement them on my code.

Black Cat
  • 87
  • 1
  • 2
  • 8
  • Sentence should be all upper-case and with no spaces, numbers or other symbols, just upper-case alphabet. Than change `String` to `char[]`, select key (in range from 0 to 26 assuming you will work on English alphabet). From each `char` in `char[]` subtract 65 and add key in modulo 26. Then add 65 to all `char`s in array and transform `char[]` back to `String`. – Matjaž Jul 06 '15 at 07:32
  • possible duplicate of [Java, How to implement a Shift Cipher (Caesar Cipher)](http://stackoverflow.com/questions/19108737/java-how-to-implement-a-shift-cipher-caesar-cipher) – vzamanillo Jul 06 '15 at 08:15
  • @MatjažMav I don't really understand, could you explain me more detail? thanks by the way! – Black Cat Jul 06 '15 at 09:07
  • Check my answer below, its all there. – Matjaž Jul 06 '15 at 09:38

1 Answers1

0

As I described in comment:

public String encrypt(String input, int key){
    char[] chars = input.toUpperCase().replaceAll("[^A-Z]", "").toCharArray();
    for (int i = 0; i < chars.length; i++){
        chars[i] -= 65;
        chars[i] = (char)((chars[i] + key) % 26);
        chars[i] += 65;
    }
    return String.valueOf(chars);
}

public String decrypt(String input, int key){
    char[] chars = input.toUpperCase().replaceAll("[^A-Z]", "").toCharArray();
    for (int i = 0; i < chars.length; i++){
        chars[i] -= 65;
        chars[i] = (char)((chars[i] - key) % 26);
        chars[i] += 65;
    }
    return String.valueOf(chars);
}

Here is running example: http://goo.gl/sSsn2m

Matjaž
  • 2,096
  • 3
  • 35
  • 55
  • On client side, before you send text to server. – Matjaž Jul 06 '15 at 08:53
  • I've add the code, but it didn't work, I mean it didn't do anything – Black Cat Jul 06 '15 at 13:21
  • You asked how to implement Caesar cipher, and this method take input string and key (number between 0 and 25). As result it returns encrypted input. I don't know where is problem, but I think you should start with basic console application and learn. I'm not sure, did you write this code on your own? – Matjaž Jul 06 '15 at 13:30
  • I have edited answer, please take a look at running example. – Matjaž Jul 06 '15 at 13:38
  • argh, I'm confused lol, I've changed string to char but another function to run the code is wrong, nevermind then – Black Cat Jul 06 '15 at 14:36