import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
public class CipherExample {
private Cipher m_encrypter;
private Cipher m_decrypter;
public void init(SecretKey key) {
byte[] initVector = new byte[] {0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x1,
0x02};
AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
try {
m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);
} catch (InvalidAlgorithmParameterException ex) {
} catch (InvalidKeyException ex) {
} catch (NoSuchPaddingException ex) {
} catch (NoSuchAlgorithmException ex) {
}
}
public void write(byte[] bytes, OutputStream out) {
CipherOutputStream cos = new CipherOutputStream(out, m_encrypter);
try {
cos.write(bytes, 0, bytes.length);
cos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void read(byte[] bytes, InputStream in) {
CipherInputStream cis = new CipherInputStream(in, m_decrypter);
int pos = 0, intValue;
try {
while ((intValue = cis.read()) != -1) {
bytes[pos] = (byte) intValue;
pos++;
}
} catch (IOException ex) {
}
}
public byte[] encrypt(byte[] input) {
try {
return (m_encrypter.doFinal(input));
} catch (BadPaddingException ex) {
return null;
} catch (IllegalBlockSizeException ex) {
return null;
}
}
public byte[] decrypt(byte[] input) {
try {
return (m_decrypter.doFinal(input));
} catch (BadPaddingException ex) {
return null;
} catch (IllegalBlockSizeException ex) {
return null;
}
}
public static void main(String[] args) {
CipherExample ce=new CipherExample();
try
{
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
ce.init(key);
System.out.println("Testing encrypt/decypt of bytes");
byte[] clearText=new byte[]{65,73,82,68,65,78,67,69};
byte[] encryptedText=ce.encrypt(clearText);
byte[] decryptedText=ce.decrypt(encryptedText);
String clearTextAsString =new String(clearText);
String encTextAsString=new String(encryptedText);
String decTextAsString=new String(decryptedText);
System.out.println(" cleartext: "+clearTextAsString);
System.out.println(" encrypted: "+encTextAsString);
System.out.println(" decrypted: "+decTextAsString);
System.out.println("/nTesting encrypting of a file/n");
FileInputStream fis=new FileInputStream("c:/cipherTest.in");
FileOutputStream fos=new FileOutputStream("c:/cipherTest.out");
int dataInputSize=fis.available();
byte[] inputBytes=new byte[dataInputSize];
fis.read(inputBytes);
ce.write(inputBytes,fos);
fos.flush();
fis.close();
fos.close();
String inputFileAsString=new String(inputBytes);
System.out.println("INPUT FILE CONTENTS/n"+inputFileAsString+"/n");
System.out.println("File encrypted and saved to dist/n");
fis=new FileInputStream("c:/cipherTest.out");
byte[] decrypted=new byte[dataInputSize];
ce.read(decrypted,fis);
fis.close();
String decryptedAsString=new String(decrypted);
System.out.println("DESCRYPTED FILE:/n"+decryptedAsString+"/n");
}catch(Exception e)
{
e.printStackTrace();
}
}
}