Hello,
Am Building a client - server application along with Encryption and decryption.
Now the thing is i need a servlet which accepts the data i.e., text or file from the file
and encrypts it using my encryption function and handles the encrypted file to the
client and client will do its work,it sends the encrypted file to server.
I have used familiar technique CIPHER for encryption and decryption of the files.
The bottom line my servlet should act as the master with controls the whole process.
Here is the code of all process.
import java.net.*;
import java.io.*;
public class Rec1 {
public static void main(String[] ar) {
int port = 6666; // just a random port. make sure you enter something between 1025 and 65535.
try {
ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
System.out.println("Waiting for a client...");
Socket socket = ss.accept(); // make the server listen for a connection, and let you know when it gets one.
System.out.println("Got a client :)");
System.out.println();
// Get the input and output streams of the socket, so that you can receive and send data to the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
// DataOutputStream out = new DataOutputStream(sout);
String line = null;
byte[] buff = new byte [1024];
int received= 0;
while(true) {
line = in.readUTF(); // wait for the client to send a line of text.
System.out.println("The client will send me this file : " + line);
// System.out.println("I'm sending it back...");
FileOutputStream op = new FileOutputStream ("C:/Received/" + line );
while((received = in.read ( buff )) > 0 )
op.write ( buff , 0 , received );
op.close();
in.close();
// out.writeUTF(line); // send the same line back to the client.
// out.flush(); // flush the stream to ensure that the data reaches the other end.
System.out.println("Finished... ");//waiting for next file\n");
// System.out.println();
//}
} catch(Exception e) {
e.printStackTrace();
}
}
}//server program
{code}
{code}import java.net.*;
import java.io.*;
public class Sen1 {
public static void main(String[] ar) {
int serverPort = 6666; // make sure you give the port number on which the server is listening.
String address = "127.0.0.1"; // this is the IP address of the server program's computer. // the address given here means "the same computer as the client".
try {
InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
//System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
System.out.println("Connected to Client");
// Get the input and output streams of the socket, so that you can receive and send data to the client.
//InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
//DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
//System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
//System.out.println();
// while(true) {
System.out.println( "enter the name of the file to be sent ");
line = keyboard.readLine(); // wait for the user to type in something and press enter.
System.out.println("Sending this file to the server...");
out.writeUTF(line); // send the above line to the server.
out.flush(); // flush the stream to ensure that the data reaches the other end.
FileInputStream fin = new FileInputStream ( line );
//line = in.readUTF(); // wait for the server to send a line of text.
// System.out.println("The server was very polite. It sent me this : " + line);
//System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
//System.out.println();
int sent = 0;
byte[] buff = new byte[1024];
while (( sent = fin.read( buff )) > 0)
out.write ( buff , 0 , sent );
System.out.println ("Sent successfully!");
fin.close();
out.flush();
out.close();
//}
} catch(Exception x) {
x.printStackTrace();
}
}
}//client program
{code}
{code}package encrypt;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
class DesEncrypter {
byte[] buf = new byte[1024];
Cipher ecipher;
Cipher dcipher;
DesEncrypter(SecretKey key) throws Exception{
byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
public void encrypt(InputStream in, OutputStream out) throws Exception{
out = new CipherOutputStream(out, ecipher);
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
out.close();
}
public void decrypt(InputStream in, OutputStream out) throws Exception{
in = new CipherInputStream(in, dcipher);
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
out.close();
}
}
/*public class encrypt {
public static void main(String[] argv) throws Exception {
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
DesEncrypter encrypter = new DesEncrypter(key);
encrypter.encrypt(new FileInputStream("keyin.txt"), new FileOutputStream("ciphertext.txt"));
encrypter.decrypt(new FileInputStream("ciphertext.txt"), new FileOutputStream("cleartext2.txt"));
}//DESencryption code
}*/
{code}