/* * Copyright (C) 2011 www.itcsolutions.eu * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1, or (at your * option) any later version. * * This file is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * */ /** * * @author Catalin - www.itcsolutions.eu * @version dec 2011 * */ import java.io.*; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class DES_ECB_CBC { Cipher eCipher; Cipher dCipher; // Buffer used to transport the bytes from one stream to another byte[] buf = new byte[8]; //input buffer - 8 bytes block byte[] obuf = new byte[1024]; //output buffer byte[] key = null; public DES_ECB_CBC() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { key = "SECRET!!".getBytes(); InitDESCiphersECBMode(); } public DES_ECB_CBC(byte[] keyBytes) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { key = new byte[keyBytes.length]; System.arraycopy(keyBytes, 0 , key, 0, keyBytes.length); InitDESCiphersECBMode(); } private void InitDESCiphersECBMode() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException{ eCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); dCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // ECB mode does NOT requires an initialization vector; only CBC mode eCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, key.length, "DES")); dCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, 0, key.length, "DES")); } private void InitDESCiphersCBCMode(byte[] iv) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException{ //set a default IV if not received if(iv == null) //the IV size is also 8 bytes = size of a block iv = new byte[]{(byte)0x8E, 0x12, 0x39, (byte)0x9C,0x07, 0x72, 0x6F, 0x5A }; IvParameterSpec ivparam = new IvParameterSpec(iv, 0, iv.length); eCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); dCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // CBC requires an initialization vector eCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, key.length, "DES"),ivparam); dCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, 0, key.length, "DES"),ivparam); } public void encrypt(InputStream in, long length, OutputStream out) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { try { // Bytes written to out will be encrypted // Read in the cleartext bytes and write to out to encrypt int numRead = 0; //number of bytes read from input int numProc = 0; //number of bytes processed long bytesToRead = length; while ((numRead = in.read(buf)) >= 0) { if (bytesToRead > buf.length) numProc = eCipher.update(buf, 0, numRead, obuf, 0); else numProc = eCipher.doFinal(buf, 0, numRead, obuf, 0); out.write(obuf, 0, numProc); bytesToRead-=numRead; } in.close(); out.flush(); out.close(); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } public void decrypt(InputStream in,long length, OutputStream out) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException{ try { // Bytes read from in will be decrypted // Read in the decrypted bytes and write the cleartext to out int numRead = 0; //number of bytes read from input int numProc = 0; //number of bytes processed long bytesToRead = length; while ((numRead = in.read(buf)) >= 0) { if (bytesToRead > buf.length) numProc = dCipher.update(buf, 0, numRead, obuf, 0); else numProc = dCipher.doFinal(buf, 0, numRead, obuf, 0); out.write(obuf, 0, numProc); bytesToRead-=numRead; } out.flush(); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } }