Using JCE 1.2 DES implementation as a stream cipher

Govind Seshadri (gseshadri@str.com)
Thu, 25 Feb 1999 18:10:04 -0600

Date: Thu, 25 Feb 1999 18:10:04 -0600
From: "Govind Seshadri" <gseshadri@str.com>
To: java-security@java.sun.com
Subject: Using JCE 1.2 DES implementation as a stream cipher

This is a multi-part message in MIME format.
--------------E63C82751FA1DA1B4A69238A
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello all!

I am trying to implement a custom socket that
provides DES-encryption (using the JCE 1.2 API) for use within
RMI.

Can the JCE DES implementation be used as a stream cipher?

I always get the following runtime error when I try to init the cipher
for either encryption or decryption:

"java.security.InvalidKeyException: Parameters missing"

I would greatly appreciate if you could detect something wrong with the
following program (especially with the code that initializes and obtains

the DES cipher instance for use with CipherInput/Output streams)

Thanks,
Govind Seshadri
-------------------------
import java.io.*;
import java.net.*;
import javax.crypto.*;
import java.security.*;
import java.util.*;

class BootstrapCrypto {
private static BootstrapCrypto instance=null;
private Cipher cipher=null;
private Key desKey=null;

public Cipher getCipher() {
return cipher;
}
public Key getKey() {
return desKey;
}

private BootstrapCrypto() {
try {
Provider sunJce = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunJce);
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(new SecureRandom());
desKey = keyGen.generateKey();
cipher = Cipher.getInstance("DES/CFB/PKCS5Padding");
} catch (Exception e) {
System.out.println("ERROR IN CIPHER INIT:"+e.toString());
}
}

public static BootstrapCrypto aBootstrapCrypto() {
if (instance == null)
instance = new BootstrapCrypto();
return instance;
}
}

class EncryptSocket extends Socket {

private InputStream in = null;
private OutputStream out = null;
private Cipher cipher=null;
private Key desKey=null;

public EncryptSocket() throws IOException {
super();
BootstrapCrypto myBootstrapCrypto =
BootstrapCrypto.aBootstrapCrypto();
cipher = myBootstrapCrypto.getCipher();
desKey = myBootstrapCrypto.getKey();
}

public EncryptSocket(String host, int port) throws IOException {
super(host, port);
BootstrapCrypto myBootstrapCrypto =
BootstrapCrypto.aBootstrapCrypto();
cipher = myBootstrapCrypto.getCipher();
desKey = myBootstrapCrypto.getKey();
}

public InputStream getInputStream() throws IOException {
try {
cipher.init(Cipher.DECRYPT_MODE,desKey);
} catch (Exception e) {
//THIS EXCEPTION IS BEING THROWN
System.out.println(e.toString());
throw new IOException("Could not init cipher");
}

if (in == null)
in = new CipherInputStream(super.getInputStream(), cipher);
return in;
}

public OutputStream getOutputStream() throws IOException {
try {
cipher.init(Cipher.ENCRYPT_MODE,desKey);
} catch (Exception e) {
//THIS EXCEPTION IS BEING THROWN!
System.out.println(e.toString());
throw new IOException("Could not init cipher");
}

if (out == null)
out = new CipherOutputStream(super.getOutputStream(),cipher);
return out;
}

public synchronized void close() throws IOException {
OutputStream o = getOutputStream();
o.flush();
super.close();
}
}

--------------E63C82751FA1DA1B4A69238A
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Description: Card for Govind Seshadri
Content-Disposition: attachment; filename="vcard.vcf"
Content-Transfer-Encoding: 7bit

begin: vcard
fn: Govind Seshadri
n: Seshadri;Govind
org: Strategic Technology Resources
adr;dom: 100 E Wisconsin Ave;;;Milwaukee;WI;53202;
email;internet: gseshadri@str.com
title: Senior Technical Architect
tel;work: 414 347 1303
note: http://www.str.com
x-mozilla-cpt: ;0
x-mozilla-html: TRUE
version: 2.1
end: vcard

--------------E63C82751FA1DA1B4A69238A--