KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > engines > DESedeEngine


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.engines;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25  * a class that provides a basic DESede (or Triple DES) engine.
26  */

27 public class DESedeEngine
28     extends DESEngine {
29   protected static final int BLOCK_SIZE = 8;
30
31   private int[] workingKey1 = null;
32   private int[] workingKey2 = null;
33   private int[] workingKey3 = null;
34
35   private boolean forEncryption;
36
37   /**
38    * standard constructor.
39    */

40   public DESedeEngine() {
41   }
42
43   /**
44    * initialise a DESede cipher.
45    *
46    * @param forEncryption whether or not we are for encryption.
47    * @param params the parameters required to set up the cipher.
48    * @exception IllegalArgumentException if the params argument is
49    * inappropriate.
50    */

51   public void init(
52       boolean encrypting,
53       byte[] key) {
54
55     byte[] keyMaster = key;
56     byte[] key1 = new byte[8], key2 = new byte[8], key3 = new byte[8];
57
58     this.forEncryption = encrypting;
59
60     if (keyMaster.length == 24) {
61       System.arraycopy(keyMaster, 0, key1, 0, key1.length);
62       System.arraycopy(keyMaster, 8, key2, 0, key2.length);
63       System.arraycopy(keyMaster, 16, key3, 0, key3.length);
64
65       workingKey1 = generateWorkingKey(encrypting, key1);
66       workingKey2 = generateWorkingKey(!encrypting, key2);
67       workingKey3 = generateWorkingKey(encrypting, key3);
68     }
69     else { // 16 byte key
70
System.arraycopy(keyMaster, 0, key1, 0, key1.length);
71       System.arraycopy(keyMaster, 8, key2, 0, key2.length);
72
73       workingKey1 = generateWorkingKey(encrypting, key1);
74       workingKey2 = generateWorkingKey(!encrypting, key2);
75       workingKey3 = workingKey1;
76     }
77   }
78
79   public String JavaDoc getAlgorithmName() {
80     return "DESede";
81   }
82
83   public int getBlockSize() {
84     return BLOCK_SIZE;
85   }
86
87   public int processBlock(
88       byte[] in,
89       int inOff,
90       byte[] out,
91       int outOff) throws IOException JavaDoc {
92     if (workingKey1 == null) {
93       throw new IllegalStateException JavaDoc("DESede engine not initialised");
94     }
95
96     if ( (inOff + BLOCK_SIZE) > in.length) {
97       throw new IOException JavaDoc("input buffer too short");
98     }
99
100     if ( (outOff + BLOCK_SIZE) > out.length) {
101       throw new IOException JavaDoc("output buffer too short");
102     }
103
104     if (forEncryption) {
105       desFunc(workingKey1, in, inOff, out, outOff);
106       desFunc(workingKey2, out, outOff, out, outOff);
107       desFunc(workingKey3, out, outOff, out, outOff);
108     }
109     else {
110       desFunc(workingKey3, in, inOff, out, outOff);
111       desFunc(workingKey2, out, outOff, out, outOff);
112       desFunc(workingKey1, out, outOff, out, outOff);
113     }
114
115     return BLOCK_SIZE;
116   }
117
118   public void reset() {
119   }
120 }
121
Popular Tags