Hi,
I don't know if this question makes any sense, but i need to get some answers.

I am working on a project, where i am using a file encrytion/decrytion program.Now i have this .java program which is working perfectly from the command prompt and encryting/decryting files

Now , the task at hand is to use this progarm using web interface,(i am using JSP for dynamic web pages),
where the user needs to browse and select the file from his local system and on sending the mail to the receiver, the file should get encryted and get stored on a directory on the server(eg:WEb-INF\user) and a link to the encrypted file should be send to the receiver, where the receiver upon clickin the link, the file should get decrypted using the program and get stored on his local directory.

Please help me out, how to implement this in my project..

Recommended Answers

All 5 Replies

As per what i understand you want to use java progam then you can use like this:-

here com.test.TestJavaProjet is the Java class

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="com.test.TestJavaProjet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%TestJavaProjet a = new  TestJavaProjet();

%>
</body>
</html>

This is just a demo.
Please give your code snippet and explain more about your problem so that we can help further and guide.

Thanks for your response, but how to send the mail with the relative path of the encrypted file, and how to decrypt the file when the receiver clicks on the link of the encrypt file.

this is the java program for encrypting/decrypting

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class EncryptDecryptFileStreamWithDES{

    private static Cipher ecipher;
    private static Cipher dcipher;

    // 8-byte initialization vector
    private static byte[] iv = {

  (byte)0xB2, (byte)0x12, (byte)0xD5, (byte)0xB2,

  (byte)0x44, (byte)0x21, (byte)0xC3, (byte)0xC3
    };

    public static void main(String[] args) {

        try {

            SecretKey key = KeyGenerator.getInstance("DES").generateKey();

            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);

encrypt(new FileInputStream("E:JOBS.xlsx"), new FileOutputStream("E:encrypted.dat"));

decrypt(new FileInputStream("E:encrypted.dat"), new FileOutputStream("E:cleartext-reversed.xlsx"));

        }
        catch (FileNotFoundException e) {
            System.out.println("File Not Found:" + e.getMessage());
            return;
        }
        catch (InvalidAlgorithmParameterException e) {
            System.out.println("Invalid Alogorithm Parameter:" + e.getMessage());
            return;
        }
        catch (NoSuchAlgorithmException e) {
            System.out.println("No Such Algorithm:" + e.getMessage());
            return;
        }
        catch (NoSuchPaddingException e) {
            System.out.println("No Such Padding:" + e.getMessage());
            return;
        }
        catch (InvalidKeyException e) {
            System.out.println("Invalid Key:" + e.getMessage());
            return;
        }

    }

    private static void encrypt(InputStream is, OutputStream os) {

  try {

    byte[] buf = new byte[1024];

// bytes at this stream are first encoded

    os = new CipherOutputStream(os, ecipher);

// read in the clear text and write to out to encrypt

int numRead = 0;

while ((numRead = is.read(buf)) >= 0) {

    os.write(buf, 0, numRead);

}

// close all streams

os.close();

  }

  catch (IOException e) {

    System.out.println("I/O Error:" + e.getMessage());

  }

    }

    private static void decrypt(InputStream is, OutputStream os) {

  try {

    byte[] buf = new byte[1024];

// bytes read from stream will be decrypted

    CipherInputStream cis = new CipherInputStream(is, dcipher);

// read in the decrypted bytes and write the clear text to out

int numRead = 0;

while ((numRead = cis.read(buf)) >= 0) {

    os.write(buf, 0, numRead);

}

// close all streams

cis.close();

is.close();

os.close();

  }

  catch (IOException e) {

    System.out.println("I/O Error:" + e.getMessage());

  }

    }

}

You have to do two things:-
1) Make encrypt() ,decrypt() public

 public static void decrypt(InputStream is, OutputStream os)




  public static void encrypt(InputStream is, OutputStream os) {

2)Call decrypt method to decrypt file

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="com.test.EncryptDecryptFileStreamWithDES" %>
    <%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%EncryptDecryptFileStreamWithDES a = new  EncryptDecryptFileStreamWithDES();
a.decrypt(new FileInputStream("E:/encrypted.dat"), new FileOutputStream("E:/cleartext-reversed.xlsx"));
%>
</body>
</html>

Hope you have mail server setup.Then just send the mail with URL of the location of your file and when user clicks on that,decrypt it.

yes i have a mail server set up.. but is it possible that my mail automatically recognize the path of the file where it is gettig saved after encryption , instead of mentioning the path manually?

can you please provide me the implemenation on a jsp ???? I am very much confused..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.