KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > crypto > AbstractSymmetricKey


1 package com.quadcap.crypto;
2
3 /* Copyright 2002 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Random JavaDoc;
42
43 import java.nio.ByteBuffer JavaDoc;
44
45 /**
46  * Base class for symmetric ciphers
47  *
48  * @author Stan Bailes
49  */

50 public abstract class AbstractSymmetricKey implements SymmetricKey {
51     /**
52      * Initialize key from serialized representation
53      */

54     public abstract void init(String JavaDoc s) throws Exception JavaDoc;
55
56     /**
57      * Initialize: Create a random key
58      */

59     public abstract void init(Random JavaDoc r);
60
61     /**
62      * The key's block size
63      */

64     public abstract int getBlockSize();
65
66     /**
67      * Class wrapper for encryption as a key operation
68      */

69     class EncryptionKey implements Key {
70         public void f(ByteBuffer JavaDoc m, ByteBuffer JavaDoc c) {
71             encrypt(m, c);
72         }
73         public int blockSize() { return getBlockSize(); }
74     }
75     protected EncryptionKey ekey = new EncryptionKey();
76
77     /**
78      * Return the key used for decryption
79      */

80     public Key getEncryptionKey() { return ekey; }
81
82     /**
83      * Encrypt a buffer (must be multiple of 8 bytes)
84      */

85     public abstract void encrypt(ByteBuffer JavaDoc plain, ByteBuffer JavaDoc code);
86
87     /**
88      * Class wrapper for decryption as a key operation
89      */

90     class DecryptionKey implements Key {
91         AbstractSymmetricKey k = null;
92         public DecryptionKey(AbstractSymmetricKey k) {
93             this.k = k;
94         }
95         public int blockSize() { return getBlockSize(); }
96         public void f(ByteBuffer JavaDoc m, ByteBuffer JavaDoc c) {
97             if (k == null) {
98                 decrypt(m, c);
99             } else {
100                 k.decrypt(m, c);
101             }
102         }
103     }
104     protected DecryptionKey dkey = new DecryptionKey(this);
105
106     /**
107      * Return the key used for encryption
108      */

109     public Key getDecryptionKey() { return dkey; }
110
111     /**
112      * Decrypt a buffer (must be a multiple of 8 bytes)
113      */

114     public abstract void decrypt(ByteBuffer JavaDoc code, ByteBuffer JavaDoc plain);
115
116 }
117
Popular Tags