Galera o meu problema é seguinte:
Estou querendo pegar uma imagem criptografa-la ,depois com a chave q foi usada,descriptogra-la,ai sim poder visualiza-la.Mas ta dando erro,a imagem fica destorcida,não completa na verdade,acho q se perde os dados de-la.
Classe que uso para Criptografar
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class hjEncrypt {
public static void main(String args[]) {
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(new SecureRandom());
SecretKey key = kg.generateKey();
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
Class spec = Class.forName("javax.crypto.spec.DESKeySpec");
DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec);
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("keyfile"));
oos.writeObject(ks.getKey());
Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("Imagem.cripto"), c);
//FileOutputStream saida=new FileOutputStream("TripleDES2.class");
FileInputStream arq = new FileInputStream ("eu23.png");
//DataInputStream in = new DataInputStream (arq);
int size=arq.available();
byte[] ler=new byte[size];
arq.read(ler,0,size);
//DataOutputStream pw = new DataOutputStream(cos);
//DataOutputStream pw = new DataOutputStream(cos);
cos.write(ler,0,size);
//saida.write(ler,0,size);
//saida.close();
//pw.write("Vinicius Alves das Neves".getBytes(),0,);
cos.close();
/*
PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos));
pw.println("Vinicius Alves das Neves");
pw.close();
*/
oos.writeObject(c.getIV());
oos.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
-------------------------------------------------------------------------------------
Classe q uso para descriptografar
-------------------------------------------------------------------------------------
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class hjDecrypt {
public static void main(String args[]) {
try {
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("keyfile"));
DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey key = skf.generateSecret(ks);
Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
c.init(Cipher.DECRYPT_MODE, key,
new IvParameterSpec((byte[]) ois.readObject()));
FileInputStream classeCripto=new FileInputStream("Imagem.cripto");
CipherInputStream cis = new CipherInputStream(classeCripto, c);
FileOutputStream arq = new FileOutputStream ("eu232.png");
DataInputStream pw = new DataInputStream(cis);
int tam=classeCripto.available();
byte[] ler=new byte[tam];
cis.read(ler,0,tam);
arq.write(ler,0,tam);
} catch (Exception e) {
System.out.println(e);
}
}
}
Alguem pode me ajudar?Grato!