/* * EncryptHandler.java * * Created on den 8 januari 2005, 11:50 */ import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.io.*; /** * * @author Henrik Johansson */ public class EncryptDecrypt { private String algorithm = "Blowfish/ECB/PKCS5Padding"; private int cipherMode = Cipher.ENCRYPT_MODE; private String dataInput; private String keyName; private String dataOutput; private int bufSize = 1024; /** Creates a new instance of EncryptHandler */ public EncryptDecrypt() { } /** Creates a new instance of EncryptHandler */ public EncryptDecrypt(String dataInput, String keyName, String dataOutput, int cipherMode) { this.dataInput = dataInput; this.keyName = keyName; this.dataOutput = dataOutput; this.cipherMode = cipherMode; } public void encrypt() { try { // ladda nyckeln FileInputStream fis = new FileInputStream(keyName); ObjectInputStream ois = new ObjectInputStream(fis); SecretKey sk = (SecretKey) ois.readObject(); ois.close(); fis.close(); Cipher c = Cipher.getInstance(algorithm); c.init(cipherMode, sk); // input FileInputStream input = new FileInputStream(dataInput); // output FileOutputStream output = new FileOutputStream(dataOutput); // encrypt byte[] buf = new byte[bufSize]; int offset = 0; int count = 0; while((count = input.read(buf, offset, bufSize)) != -1) { byte[] out = c.update(buf, 0, count); output.write(out, offset, out.length); } byte[] out = c.doFinal(); output.write(out, offset, out.length); output.close(); input.close(); } catch(Exception e) { e.printStackTrace(); } } public void setCipherMode(int cipherMode) { this.cipherMode = cipherMode; } public void setDataInputName(String dataInput) { this.dataInput = dataInput; } public void setKeyName(String keyName) { this.keyName = keyName; } public void setDataOutputName(String dataOutput) { this.dataOutput = dataOutput; } }