DES Encryption OutputStream Error

OK. I'm confused. What am I doing wrong in this code that I get a weird null runtime everytime with an odd java result?

This was developed on a Windows XP 32bit system using the Java Studio Enterprise 8.1 IDE and JDK 1.5.0_06 (however, whether I run it inside or outside of the IDE I get the same result).

Exception in thread"main" java.lang.NullPointerException

at javax.crypto.CipherOutputStream.write(DashoA12275)

at foc.mcjunkin.utility.Encrypter.encrypt(Encrypter.java:81)

at foc.mcjunkin.utility.Encrypter.main(Encrypter.java:72)

Java Result: 1

/*

* Encrypter.java

*

* This class encrypts or decrypts files or streams using DES encryption.

*

* Created on June, 2007.

*

* @author Robertp

*/

package foc.mcjunkin.utility;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.spec.AlgorithmParameterSpec;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;

import javax.crypto.spec.PBEParameterSpec;

publicclass Encrypter{

/** The password used to encrypt and decrypt files. Under absolutely no

* circumstances should this key be changed! If changed, any data

* previously stored will not be accessible! */

privatefinalstatic String password ="password";

/** The java cryptography cipher for encryption. */

private Cipher ecipher;

/** The java cryptography cipher for decryption. */

private Cipher dcipher;

/** The number of iterations on the encryption. */

privatefinalint iterations = 19;

// 8-byte Salt

privatefinalstaticbyte[] salt ={

(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,

(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03

};

/** Create a new instance of the class. */

publicvoid Encrypter()throws Exception{

KeySpec keySpec =new PBEKeySpec(password.toCharArray(), salt, iterations);

SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

AlgorithmParameterSpec paramSpec =new PBEParameterSpec(salt, iterations);

ecipher = Cipher.getInstance(key.getAlgorithm());

dcipher = Cipher.getInstance(key.getAlgorithm());

ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

}

/** Testing... */

publicstaticvoid main(String[] args)throws Exception{

File f1 =new File("c:\\junk.in");// Any old data.

if (!f1.exists()){

thrownew Exception("The input file c:\\junk.in did not exist.");

}

File f2 =new File("c:\\junk.out");

if (f2.exists()){f2.delete();}

f2.createNewFile();

FileInputStream in =new FileInputStream(f1);

FileOutputStream out =new FileOutputStream(f2);

new Encrypter().encrypt(in, out);

}

/** Encrypt an InputStream and write it to an OutputStream. */

publicvoid encrypt(InputStream in, OutputStream out)throws Exception{

byte[] buffer =newbyte[1024];

out =new CipherOutputStream(out, this.ecipher);

int i = 0;

while ((i = in.read(buffer)) >= 0){

out.write(buffer, 0, i);

}

out.flush();

out.close();

}

/** Decrypt an InputStream and write it to an OutputStream. */

publicvoid decrypt(InputStream in, OutputStream out)throws Exception{

byte[] buffer =newbyte[1024];

in =new CipherInputStream(in, this.dcipher);

int i = 0;

while ((i = in.read(buffer)) >= 0){

out.write(buffer, 0, i);

}

out.flush();

out.close();

}

}

[7533 byte] By [rpalm01a] at [2007-11-27 6:52:39]
# 1

A silly!

The class name is Encrypter sopublic void FileEncrypter() throws Exception {

should bepublic Encrypter() throws Exception {

sabre150a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 2
Thanks....that was a typo when I posted it. It's not the actual problem. If you notice I fixed this in the post above.
rpalm01a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 3
> Thanks....that was a typo when I posted it. It's not> the actual problem. If you notice I fixed this in> the post above.Your code works for me when I fix the error I found and you have not fixed the error! Look at it carefully.
sabre150a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 4
You don't get a runtime error of any kind? Wow. I wonder what's wrong then? (I know how to fix compile errors...I'm getting a runtime error!)Message was edited by: rpalm01
rpalm01a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 5

> You don't get a runtime error of any kind? Wow. I

> wonder what's wrong then? (I know how to fix compile

> errors...I'm getting a runtime error!)

>

> Message was edited by:

> rpalm01

Look at my first post and compare what I suggest with what you have.

sabre150a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 6

OK. I'm an idiot. Thanks. It was the "Void" bit...not the FileEncryptor bit that was causing the problem.

/*

* Encrypter.java

*

* This class encrypts or decrypts files or streams using DES encryption.

*

* Created on June, 2007.

*

* @author Robertp

*/

package foc.mcjunkin.utility;

import java.io.*;

import java.security.spec.AlgorithmParameterSpec;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;

import javax.crypto.spec.PBEParameterSpec;

public class Encrypter {

/** The password used to encrypt and decrypt files. Under absolutely no

* circumstances should this key be changed! If changed, any data

* previously stored will not be accessible! */

private final static String password = "password";

/** The java cryptography cipher for encryption. */

private Cipher ecipher;

/** The java cryptography cipher for decryption. */

private Cipher dcipher;

/** The number of iterations on the encryption. */

private final int iterations = 19;

// 8-byte Salt

private final static byte[] salt = {

(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,

(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03

};

/** Create a new instance of the class. */

public void Encrypter() throws Exception {

KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterations);

SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations);

ecipher = Cipher.getInstance(key.getAlgorithm());

dcipher = Cipher.getInstance(key.getAlgorithm());

ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

}

/** Testing... */

public static void main(String[] args) throws Exception {

File f1 = new File("c:\\junk.in"); // Any old data.

if (!f1.exists()) {

throw new Exception("The input file c:\\junk.in did not exist.");

}

File f2 = new File("c:\\junk.out");

if (f2.exists()) {f2.delete();}

f2.createNewFile();

FileInputStream in = new FileInputStream(f1);

BufferedOutputStream out = new java.io.BufferedOutputStream(new java.io.FileOutputStream(f2));

new Encrypter().encrypt(in, out);

}

/** Encrypt an InputStream and write it to an OutputStream. */

public void encrypt(InputStream in, OutputStream out) throws Exception {

byte[] buffer = new byte[1024];

out = new CipherOutputStream(out, this.ecipher);

int i = 0;

while ((i = in.read(buffer)) >= 0) {

out.write(buffer, 0, i);

}

out.flush();

out.close();

}

/** Decrypt an InputStream and write it to an OutputStream. */

public void decrypt(InputStream in, OutputStream out) throws Exception {

byte[] buffer = new byte[1024];

in = new CipherInputStream(in, this.dcipher);

int i = 0;

while ((i = in.read(buffer)) >= 0) {

out.write(buffer, 0, i);

}

out.flush();

out.close();

}

}

Message was edited by:

rpalm01

rpalm01a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 7

> OK. I'm an idiot. Thanks. It was the "Void" bit...not

> the FileEncryptor bit that was causing the problem.

>

You still have not fixed it in this code!

The problem you seem to be having is in debugging. I just added a few print statements to your code and quickly found that the ciphers were null and that the 'constructor' was not being invoked.

Message was edited by:

sabre150

sabre150a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...
# 8

I know. I had fixed it and not reposted it properly. Here's the working code in case any future reader needs it...

/*

* Encrypter.java

*

* This class encrypts or decrypts files or streams using DES encryption.

*

* Created on June, 2007.

*

* @author Robertp

*/

package foc.mcjunkin.utility;

import java.io.*;

import java.security.spec.AlgorithmParameterSpec;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;

import javax.crypto.spec.PBEParameterSpec;

public class Encrypter {

/** The password used to encrypt and decrypt files. Under absolutely no

* circumstances should this key be changed! If changed, any data

* previously stored will not be accessible! */

private final static String password = "password";

/** The java cryptography cipher for encryption. */

private Cipher ecipher;

/** The java cryptography cipher for decryption. */

private Cipher dcipher;

/** The number of iterations on the encryption. */

private final int iterations = 19;

// 8-byte Salt

private final static byte[] salt = {

(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,

(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03

};

/** Create a new instance of the class. */

public Encrypter() throws Exception {

KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterations);

SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations);

ecipher = Cipher.getInstance(key.getAlgorithm());

dcipher = Cipher.getInstance(key.getAlgorithm());

ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

}

/** Testing... */

public static void main(String[] args) throws Exception {

File f1 = new File("c:\\jdk1.5.0_06\\bin\\java.exe");

if (!f1.exists()) {

throw new Exception("The input file did not exist.");

}

File f2 = new File("c:\\junk.out");

if (f2.exists()) {f2.delete();}

f2.createNewFile();

FileInputStream in = new FileInputStream(f1);

BufferedOutputStream out = new java.io.BufferedOutputStream(new java.io.FileOutputStream(f2));

Encrypter encrypter = new Encrypter();

encrypter.encrypt(in, out);

File f3 = new File("c:\\result.out");

if (f3.exists()) {f3.delete();}

f3.createNewFile();

in = new FileInputStream(f2);

out = new java.io.BufferedOutputStream(new java.io.FileOutputStream(f3));

encrypter.decrypt(in, out);

}

/** Encrypt an InputStream and write it to an OutputStream. */

public void encrypt(InputStream in, OutputStream out) throws Exception {

byte[] buffer = new byte[1024];

out = new CipherOutputStream(out, this.ecipher);

int i = 0;

while ((i = in.read(buffer)) >= 0) {

out.write(buffer, 0, i);

}

out.flush();

out.close();

}

/** Decrypt an InputStream and write it to an OutputStream. */

public void decrypt(InputStream in, OutputStream out) throws Exception {

byte[] buffer = new byte[1024];

in = new CipherInputStream(in, this.dcipher);

int i = 0;

while ((i = in.read(buffer)) >= 0) {

out.write(buffer, 0, i);

}

out.flush();

out.close();

}

}

rpalm01a at 2007-7-12 18:27:14 > top of Java-index,Security,Cryptography...