KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > crypto > cipher > DESede


1
2 package ch.ethz.ssh2.crypto.cipher;
3
4 /*
5  This file was shamelessly taken (and modified) from the Bouncy Castle Crypto package.
6  Their licence file states the following:
7
8  Copyright (c) 2000 - 2004 The Legion Of The Bouncy Castle
9  (http://www.bouncycastle.org)
10
11  Permission is hereby granted, free of charge, to any person obtaining a copy
12  of this software and associated documentation files (the "Software"), to deal
13  in the Software without restriction, including without limitation the rights
14  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the Software is
16  furnished to do so, subject to the following conditions:
17
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  THE SOFTWARE.
28  */

29
30 /**
31  * DESede.
32  *
33  * @author See comments in the source file
34  * @version $Id: DESede.java,v 1.3 2005/08/11 12:47:27 cplattne Exp $ethz.ch
35  *
36  */

37 public class DESede extends DES
38 {
39     private int[] key1 = null;
40     private int[] key2 = null;
41     private int[] key3 = null;
42
43     private boolean encrypt;
44
45     /**
46      * standard constructor.
47      */

48     public DESede()
49     {
50     }
51
52     /**
53      * initialise a DES cipher.
54      *
55      * @param encrypting
56      * whether or not we are for encryption.
57      * @param key
58      * the parameters required to set up the cipher.
59      * @exception IllegalArgumentException
60      * if the params argument is inappropriate.
61      */

62     public void init(boolean encrypting, byte[] key)
63     {
64         key1 = generateWorkingKey(encrypting, key, 0);
65         key2 = generateWorkingKey(!encrypting, key, 8);
66         key3 = generateWorkingKey(encrypting, key, 16);
67
68         encrypt = encrypting;
69     }
70
71     public String JavaDoc getAlgorithmName()
72     {
73         return "DESede";
74     }
75
76     public int getBlockSize()
77     {
78         return 8;
79     }
80
81     public void transformBlock(byte[] in, int inOff, byte[] out, int outOff)
82     {
83         if (key1 == null)
84         {
85             throw new IllegalStateException JavaDoc("DESede engine not initialised!");
86         }
87
88         if (encrypt)
89         {
90             desFunc(key1, in, inOff, out, outOff);
91             desFunc(key2, out, outOff, out, outOff);
92             desFunc(key3, out, outOff, out, outOff);
93         }
94         else
95         {
96             desFunc(key3, in, inOff, out, outOff);
97             desFunc(key2, out, outOff, out, outOff);
98             desFunc(key1, out, outOff, out, outOff);
99         }
100     }
101
102     public void reset()
103     {
104     }
105 }
106
Popular Tags