Forum Stats

  • 3,780,580 Users
  • 2,254,411 Discussions
  • 7,879,386 Comments

Discussions

How to encrypt

843811
843811 Member Posts: 49,851
Hi:


I am new to JCE and wantt to develop a application wherein I can retreive aa file from the local system and using a Password(with MD5) generate a Cryptographic Key...

The Key is then used for encryption of the file.Problem is I am unable to encrypt the file...

My code is as foloows.Sugest me as to where i am getting wrong.This is part of the complete code:

>>>>>>>>>>>>
try
{
pwdseed=txtPasswordSeed.getText();
pwd_seed=_pwd_seed.getBytes();
secretKey = new SecretKeySpec(pwd_seed, "PBEWithMD5AndDES");
}catch(Exception e7){}

lblMsgs.setText("The new seed is set");
lblMsgs.setText("Encryptiing File ........");
//Encrypt the File
try
{
String finfilename = "";//for final Filename extracted from absolute path
String fi_filename="";//for the process of getting actual file name
String infilename = txtFilename.getText();//This is the actual file with absolte path given by user

//StringTokenizer for infile to get actual file name from absolute path
StringTokenizer stzinFile=new StringTokenizer(infilename,"\\");

while(stzinFile.hasMoreTokens()){
fi_filename = stzinFile.nextToken();
}

StringTokenizer stzinFile1=new StringTokenizer(fi_filename,".");
finfilename = stzinFile1.nextToken();

//String osname = System.getProperty("os.name");

//Getting user current working directory
//if (osname.indexOf("Windows",0) != -1)
//{
//String tmpdir = System.getProperty("user.dir");//Getting system temdirectory to store ENC file
//}

String outFilename ="";
outFilename="C:"+"\\"+finfilename+".enc";

/Initialize the Cipher
cenc1=Cipher.getInstance("PBEWithMD5AndDES");
cenc1.init(Cipher.ENCRYPT_MODE,secretKey,random);

//Read a InputStream
//FilePermission per=new FilePermission(infilename,"read");

fis=new FileInputStream(infilename);
cis1 = new CipherInputStream(fis, cenc1);

//Writing the content-encrypted to a new file(*.enc)
fos=new FileOutputStream(outFilename);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fos.close();
fis1.close();
lblMsgs.setText(outFilename);
}catch(Exception e8)
{
lblMsgs.setText("There is a error");
System.err.print("The error is"+e8);
}
//txtFilename.setText("");
}
.
I am getting a java.lang.NullPointerException at e8.


Kindly let me know why I amgetting this error and why I am unable to encrypt the file.

Regards
R.Ritesh

Comments

  • 843811
    843811 Member Posts: 49,851
    Your program is working fine for me after making some changes but i could not get ur logic becos the resulting file to having the same content.

    NullPointerException usually occurs when we forget to allocate the memory for the defined object in your program u would have declared FileInputStream fis and u r using fis1 or else you haven't allocate the memory for the object which you are using inside the try catch block just go thru it again

    here i added the corrected program but i didnot change ur logic. what i have attached is working fine (creating the file filetry.enc in c:\\)

    import java.security.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.util.*;
    import java.io.*;

    public class Sam {

    String pwdseed;
    byte[] pwd_seed;
    SecretKey secretKey;
    Cipher cenc1;
    FileInputStream fis;
    FileOutputStream fos;
    CipherInputStream cis1;
    SecureRandom random;

    public Sam(){


    Provider sunJce = new com.sun.crypto.provider.SunJCE();
    Security.addProvider(sunJce);


    try
    {
    pwdseed="dddddd";
    pwd_seed=_pwd_seed.getBytes();
    secretKey = new SecretKeySpec(pwd_seed, "PBEWithMD5AndDES");
    }catch(Exception e7){}

    random = new SecureRandom();

    System.out.println("The new seed is set");
    System.out.println("Encryptiing File ........");
    //Encrypt the File

    try
    {
    String finfilename = "";//for final Filename extracted from absolute path
    String fi_filename="";//for the process of getting actual file name
    String infilename = "c:\\filetry.txt";//This is the actual file with absolte path given by user

    //StringTokenizer for infile to get actual file name from absolute path
    StringTokenizer stzinFile=new StringTokenizer(infilename,"\\");

    while(stzinFile.hasMoreTokens()){
    fi_filename = stzinFile.nextToken();
    }

    System.out.println("after tokenizer"+fi_filename);

    StringTokenizer stzinFile1=new StringTokenizer(fi_filename,".");
    finfilename = stzinFile1.nextToken();

    //String osname = System.getProperty("os.name");

    //Getting user current working directory
    //if (osname.indexOf("Windows",0) != -1)
    //{
    //String tmpdir = System.getProperty("user.dir");//Getting system temdirectory to store ENC file
    //}

    String outFilename ="";
    outFilename="C:"+"\\"+finfilename+".enc";

    //Initialize the Cipher
    cenc1=Cipher.getInstance("PBEWithMD5AndDES");
    cenc1.init(Cipher.ENCRYPT_MODE,secretKey,random);

    //Read a InputStream
    //FilePermission per=new FilePermission(infilename,"read");

    fis=new FileInputStream(infilename);
    System.out.println("fdfdfdf"+fis);
    cis1 = new CipherInputStream(fis, cenc1);
    System.out.println("aft cipher stat");
    //Writing the content-encrypted to a new file(*.enc)
    fos=new FileOutputStream(outFilename);
    byte[] buf = new byte[1024];
    int len;
    while ((len = fis.read(buf)) > 0) {
    fos.write(buf, 0, len);
    }
    fos.close();
    fis.close();
    System.out.println(outFilename);
    }catch(Exception e8)
    {
    System.out.println("There is a error");
    System.err.print("The error is"+e8);
    }
    //txtFilename.setText("");
    }

    public static void main(String a[]){
    Sam s = new Sam();
    }

    }
This discussion has been closed.