Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by asd ( 14 years ago )
import java.net.*;
import java.util.*;
import java.io.*;
public class Server{
public static void main(String[] args) throws Exception{
ServerSocket mysock = new ServerSocket(1234);
Socket mylink = mysock.accept();
System.out.println("new client connected");
DataInputStream input = new DataInputStream(mylink.getInputStream());
DataOutputStream output = new DataOutputStream(mylink.getOutputStream());
String request="";
String temp=null;
String sendstring="";
String cipher="";
String plaintext="";
Scanner inp = new Scanner(System.in);
while(!request.equals("QUIT")){
request=input.readUTF();
plaintext=decipherCaesar(request);
System.out.println("cipher:"+request);
System.out.println("plaintext:"+plaintext);
temp=inp.next();
sendstring=caesarCipher(temp);
output.writeUTF(sendstring);
}
mylink.close();
}
public static String caesarCipher(String x){
String temp = "";
x = x.trim(); //remove whitespace
x = x.toLowerCase(); //In java, 'A' != 'a', so we want the letters in one case
//This section establishes the alphabet in an ArrayList.
ArrayList<Character> reference = new ArrayList<Character>();
for(char i = 'a'; i <= 'a' + 25; i++) reference.add(i);
//This section handles the encryption
for(int i = 0; i < x.length(); i++){
//Checks to see if the character is a letter
if(!Character.isLetter(x.charAt(i))) temp += x.charAt(i);
else{temp += reference.get((reference.indexOf(x.charAt(i)) + 3)&);}
}
return temp;
}
public static String decipherCaesar(String x){
String temp = "";
x = x.trim(); //remove whitespace
x = x.toLowerCase(); //In java, 'A' != 'a', so we want the letters in one case
//This section establishes the alphabet in an ArrayList.
ArrayList<Character> reference = new ArrayList<Character>();
for(char i = 'a'; i <= 'a' + 25; i++) reference.add(i);
//This section handles the encryption
for(int i = 0; i < x.length(); i++){
//Checks to see if the character is a letter
if(!Character.isLetter(x.charAt(i))) temp += x.charAt(i);
else{temp += reference.get((reference.indexOf(x.charAt(i)) + 23)&);}
}
return temp;
}
public static String cipher2(String y){
String plaintext=y;
String ciphertext=null;
char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] plain = plaintext.toLowerCase().toCharArray();
for(int i = 0;i<plain.length;i++){
for(int j = 0 ; j<26;j++){
if(j<=22){
if(plain[i]==chars[j]){
plain[i] = chars[j+3];
break;
}
}//End nested If
else if(plain[i] == chars[j]){
plain[i] = chars [j-23];
} //End else
} //End nested for loop
} //End of For loop
ciphertext = String.valueOf(plain);
return ciphertext;
}
public static String decipher2(String y){
String ciphertext=y;
String plaintext=null;
char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] cipher = ciphertext.toLowerCase().toCharArray();
for(int i = 0;i<cipher.length;i++){
for(int j = 0 ; j<26;j++){
if(j>=3 && cipher[i]==chars[j]){
cipher[i] = chars[j-3];
break;
}
if(cipher[i] == chars[j] && j<3){
System.out.println("Replacing "+cipher[i]+" by "+chars[23+j]+" "+j+23+"");
cipher[i] = chars[23+j];
break;
} //End IF
} //End nested for loop
} //End of For loop
plaintext = String.valueOf(cipher);
return plaintext;
}
}
Revise this Paste