Encrypting and decrypting a file with a KeyPair

Hello All,

I am trying to encrypt a file using a private key and decrypt it using a public key but I get the following error:

java.lang.NullPointerException

at javax.crypto.CipherOutputStream.write(DashoA12275)

at DesEncrypterAsy.encrypt(DesEncrypterAsy.java:49)

at DesEncrypterAsy.main(DesEncrypterAsy.java:104)

Can someone help?

the code is below:

import java.io.*;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.*;

import java.security.spec.*;

import javax.crypto.spec.SecretKeySpec;

import javax.crypto.spec.*;

import javax.crypto.Cipher;

import javax.crypto.*;

public class DesEncrypterAsy {

Cipher ecipher;

Cipher dcipher;

DesEncrypterAsy(PrivateKey key) {

// Create an 8-byte initialization vector

byte[] iv = new byte[]{

(byte)0x8E, 0x12, 0x39, (byte)0x9C,

0x07, 0x72, 0x6F, 0x5A

};

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

try {

ecipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING");

dcipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING");

//ecipher = Cipher.getInstance("DES");

//dcipher = Cipher.getInstance("DES");

// CBC requires an initialization vector

ecipher.init(Cipher.ENCRYPT_MODE, key);

dcipher.init(Cipher.DECRYPT_MODE, key);

/*} catch (java.security.InvalidAlgorithmParameterException e) {*/

} catch (javax.crypto.NoSuchPaddingException e) {

} catch (java.security.NoSuchAlgorithmException e) {

} catch (java.security.InvalidKeyException e) {

}

}

// Buffer used to transport the bytes from one stream to another

byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out) {

try {

// Bytes written to out will be encrypted

out = new CipherOutputStream(out, ecipher);

// Read in the cleartext bytes and write to out to encrypt

int numRead = 0;

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

out.write(buf, 0, numRead);

}

out.close();

} catch (java.io.IOException e) {

}

}

public void decrypt(InputStream in, OutputStream out) {

try {

// Bytes read from in will be decrypted

in = new CipherInputStream(in, dcipher);

// Read in the decrypted bytes and write the cleartext to out

int numRead = 0;

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

out.write(buf, 0, numRead);

}

out.close();

} catch (java.io.IOException e) {

}

}

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

{

try

{

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

kpg.initialize(512); // 512 is the keysize.

KeyPair kp = kpg.generateKeyPair();

PublicKey pubk = kp.getPublic();

PrivateKey prvk = kp.getPrivate();

System.out.println(kp.getPublic());

System.out.println(kp.getPrivate());

DesEncrypterAsy encrypter = new DesEncrypterAsy(prvk);

File f = new File("toencrypt");

File files[] = f.listFiles();

for(int i=0;i<files.length;i++)

if(files.isFile())

{

// do your stuff

// Encrypt

encrypter.encrypt(new FileInputStream("toencrypt" + File.separator + files.getName()),

new FileOutputStream( "todecrypt" + File.separator + files.getName()));

// Decrypt

//-encrypter.decrypt(new FileInputStream("ciphertext"),

//- new FileOutputStream("cleartext2e"));

}

}

catch(Exception cyex) {cyex.printStackTrace();}

}

}

Thanks

Zab>

[3721 byte] By [ochomoonyangoa] at [2007-11-26 16:48:11]
# 1
> at DesEncrypterAsy.encrypt(DesEncrypterAsy.java:49)So which is line 49? If it iswhile ((numRead = in.read(buf)) >= 0) {then your input stream 'in' is null.
sabre150a at 2007-7-8 23:15:42 > top of Java-index,Security,Cryptography...
# 2
Hi,I have tried to check if "in" is nullif(in == null){System.out.println("gghhgghhgg");}it is not null.
ochomoonyangoa at 2007-7-8 23:15:42 > top of Java-index,Security,Cryptography...
# 3
But which is line 49? That will tell you most of the story!
sabre150a at 2007-7-8 23:15:42 > top of Java-index,Security,Cryptography...
# 4

Please use the tags - makes things MUCH easier.

Second - first thing I noticed is your class is using the private key for both the encrypt and decrypt stream. That will Not Work.

Third - your class assumes "toencrypt" a) is a directory that b) contains files that c) are not zero-length. I think.

Anyway - repost using code-tags, so more ppl can help.

Grant

ggaineya at 2007-7-8 23:15:42 > top of Java-index,Security,Cryptography...
# 5

My code reads files from a folder toencrypt, it "encrypts" them and writes them to a nother folder "todecrypt".

On decryption, the files are read from "todecrypt" decrypted and then written to a folder "clear2".

The error I get is:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes

at com.sun.crypto.provider.RSACipher.a(DashoA13*..)

at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)

at javax.crypto.Cipher.doFinal(DashoA13*..)

at DesEncrypterAsy.decrypt(DesEncrypterAsy.java:83)

at DesEncrypterAsy.main(DesEncrypterAsy.java:121)

that is when I initialize the key with size 1024.

The code is shown below, with Private key doing the encryption and public key decrypting:

import java.io.*;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.*;

import java.security.spec.*;

import javax.crypto.spec.SecretKeySpec;

import javax.crypto.spec.*;

import javax.crypto.Cipher;

import javax.crypto.*;

public class DesEncrypterAsy {

Cipher ecipher;

Cipher dcipher;

DesEncrypterAsy(PrivateKey prvKey, PublicKey pubKey) {

// Create an 8-byte initialization vector

byte[] iv = new byte[]{

(byte)0x8E, 0x12, 0x39, (byte)0x9C,

0x07, 0x72, 0x6F, 0x5A

};

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

try {

ecipher = Cipher.getInstance("RSA");

dcipher = Cipher.getInstance("RSA");

//ecipher = Cipher.getInstance("DES");

//dcipher = Cipher.getInstance("DES");

//ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

//dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

// CBC requires an initialization vector

ecipher.init(Cipher.ENCRYPT_MODE, prvKey);

dcipher.init(Cipher.DECRYPT_MODE, pubKey);

//System.out.println(prvKey);

/*} catch (java.security.InvalidAlgorithmParameterException e) {*/

} catch (javax.crypto.NoSuchPaddingException e) {

} catch (java.security.NoSuchAlgorithmException e) {

} catch (java.security.InvalidKeyException e) {

}

catch(Exception exptt){ exptt.printStackTrace(); }

}

// Buffer used to transport the bytes from one stream to another

byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out) {

try {

// Bytes written to out will be encrypted

out = new CipherOutputStream(out, ecipher);

if(ecipher == null)

{

System.out.println("gghhgghhgg");

}

// Read in the cleartext bytes and write to out to encrypt

int offset = 0;

int numRead = 0;

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

System.out.println(buf);

out.write(buf, 0, numRead);

}

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public void decrypt(InputStream in, OutputStream out) {

try {

// Bytes read from in will be decrypted

//in = new CipherInputStream(in, dcipher);

if(in == null)

{

System.out.println("gghhgghhgg");

}

System.out.println("in the decrypt");

// Read in the decrypted bytes and write the cleartext to out

int numRead = 0;

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

System.out.println(dcipher.doFinal(buf));

out.write(buf, 0, numRead);

out.close(); return;

}

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

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

{

try

{

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

kpg.initialize(1024); // 512 is the keysize.

KeyPair kp = kpg.generateKeyPair();

PublicKey pubk = kp.getPublic();

PrivateKey prvk = kp.getPrivate();

DesEncrypterAsy encrypter = new DesEncrypterAsy(prvk, pubk);

//kyalon fkngemu2005

File f = new File("toencrypt");

File files[] = f.listFiles();

for(int i=0;i<files.length;i++)

if(files.isFile())

{

// do your stuff

// Encrypt

//encrypter.encrypt(new FileInputStream("toencrypt" + File.separator + files.getName()),

// new FileOutputStream( "todecrypt" + File.separator + files.getName()));

// decrypt

encrypter.decrypt(new FileInputStream("todecrypt" + File.separator + files.getName()),

new FileOutputStream( "clear2" + File.separator + files.getName()));

// Decrypt

//encrypter.encrypt(new FileInputStream("ciphertext"),

// new FileOutputStream("cleartext2e"));

}

}

catch(Exception cyex) {cyex.printStackTrace();}

}

}

thanks>

ochomoonyangoa at 2007-7-8 23:15:42 > top of Java-index,Security,Cryptography...
# 6
Cipher.doFinal() obviously shouldn't be called in a loop, but why call it at all? Why not just use CipherInputStream.read()?
ejpa at 2007-7-8 23:15:42 > top of Java-index,Security,Cryptography...