hi all,
i'm encrypting and decrypting a file in the same program. But the Decrypted file doesn't appear well some parts are (seemingly still encrypted)
here is the code i use:
System: JDK1.3.1
JCE 1.2.2
Thank you
CODE]
/*
* test.java
*
* Created on 4 december 2002, 14:19
*/
package com.dastt.sejo.denc;
import java.io.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
/**
*
* @author U97488
*/
public class
test {
/** Creates a new instance of test */
public test() {
}
/**
* @param args the command line arguments
*/
public static void main(
String[] args) {
try{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
File in = new File("c:/tmp/test.txt");
File out = new File("c:/tmp/test_encrypted.txt");
Key key=null;
byte b[] = new byte[1024];
try {
String enckey="012345678901234567890123456789012345678901234";
byte[] rawkey = enckey.getBytes();
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
key = keyfactory.generateSecret(keyspec);
Cipher ci = Cipher.getInstance("DESede/CBC/NoPadding");
byte[] initValue = new byte[8];
//setIV(initValue);
IvParameterSpec ivSpec = new IvParameterSpec(initValue);
AlgorithmParameters algParams = AlgorithmParameters.getInstance("DESede");
algParams.init(ivSpec);
ci.init(ci.ENCRYPT_MODE, key);
CipherInputStream cis = new CipherInputStream((InputStream)new FileInputStream(in), ci);
InputStreamReader isr = new InputStreamReader(cis, "ISO-8859-1");
StringBuffer strbfr = new StringBuffer();
char c[] = new char[1024];
while(isr.read(c)!=-1){
strbfr.append(c);
}
ci.init(ci.DECRYPT_MODE, key, algParams);
CipherOutputStream cos = new CipherOutputStream((OutputStream)new FileOutputStream("c:/tmp/testJoc.txt"), ci);
OutputStreamWriter osr = new OutputStreamWriter(cos, "ISO-8859-1");
osr.write(strbfr.toString());
osr.flush();
osr.close();
cos.close();
cis.close();
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
[/CODE]