Cipher jpeg stream

T

terry

Hi,

Why the following code cannot work? It is used to encode and decode
image file. Could anyone help me?

import java.security.spec.*;
import javax.crypto.spec.*;
import java.lang.*;
import java.text.*;
import java.io.*;
import java.sql.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.crypto.*;
import javax.imageio.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;


class DESImageIcon
{
Debug debug=new Debug();
Ut ut=new Ut();
ImageIcon ii;
byte[] iv;
AlgorithmParameterSpec paramSpec;
Cipher ecipher;
Cipher dcipher;
SecretKey key;

DESImageIcon()
{
iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C,0x07, 0x72, 0x6F,
0x5A};
paramSpec = new IvParameterSpec(iv);


try
{
key = KeyGenerator.getInstance("DES").generateKey();
ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

}
catch (java.security.InvalidAlgorithmParameterException e)
{
e.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e)
{
e.printStackTrace();
}
catch (java.security.NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (java.security.InvalidKeyException e)
{
e.printStackTrace();
}

}

boolean read(String file)
{
try
{


CipherInputStream cis=new CipherInputStream(new FileInputStream(file),
dcipher);
JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(cis);
ii=new ImageIcon((BufferedImage)decoder.decodeAsBufferedImage());
cis.close();
}
catch (java.io.IOException e)
{
e.printStackTrace();
return false;
}

return false;
}

boolean write(String file)
{
try
{
Image image=ii.getImage();
BufferedImage bi=new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
bi.getGraphics().drawImage(image,0,0,null);

CipherOutputStream cos=new CipherOutputStream(new
FileOutputStream(file),ecipher);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(cos);
encoder.encode(bi);
cos.close();
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top