# Re: Using JCE 1.2 DES implementation as a stream cipher

Jan Luehe (luehe@laguna.eng.sun.com)
Thu, 25 Feb 1999 17:05:48 -0800 (PST)

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: Barry: "Big break!"
- Previous message: Govind Seshadri: "Using JCE 1.2 DES implementation as a stream cipher"
- Maybe in reply to: Govind Seshadri: "Using JCE 1.2 DES implementation as a stream cipher"

    Date: Thu, 25 Feb 1999 17:05:48 -0800 (PST)

    From: Jan Luehe <luehe@laguna.eng.sun.com>

    Subject: Re: Using JCE 1.2 DES implementation as a stream cipher

    To: java-security@java.Sun.COM, gseshadri@str.com

Are you sure you are getting this exception when you initialize
the cipher for encryption?

You should be getting this exception only when you initialize
for decryption, because you are initializing the cipher
in CFB mode, which requires an IV parameter.

When you initialize your cipher for encryption, and do not
pass an IV, the cipher implementation automatically generates
a random one, which you can retrieve using either the
Cipher.getIV() or Cipher.getParameters() methods.

You need to pass that same IV to the method that
initializes your cipher for decryption, in order for
decryption to work.

Jan

> "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();
> }
> }
>
>
>
>

- Next message: Barry: "Big break!"
- Previous message: Govind Seshadri: "Using JCE 1.2 DES implementation as a stream cipher"
- Maybe in reply to: Govind Seshadri: "Using JCE 1.2 DES implementation as a stream cipher"