001: package com.quadcap.crypto;
002:
003: /* Copyright 2002 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Random;
042:
043: import java.nio.ByteBuffer;
044:
045: /**
046: * Base class for symmetric ciphers
047: *
048: * @author Stan Bailes
049: */
050: public abstract class AbstractSymmetricKey implements SymmetricKey {
051: /**
052: * Initialize key from serialized representation
053: */
054: public abstract void init(String s) throws Exception;
055:
056: /**
057: * Initialize: Create a random key
058: */
059: public abstract void init(Random r);
060:
061: /**
062: * The key's block size
063: */
064: public abstract int getBlockSize();
065:
066: /**
067: * Class wrapper for encryption as a key operation
068: */
069: class EncryptionKey implements Key {
070: public void f(ByteBuffer m, ByteBuffer c) {
071: encrypt(m, c);
072: }
073:
074: public int blockSize() {
075: return getBlockSize();
076: }
077: }
078:
079: protected EncryptionKey ekey = new EncryptionKey();
080:
081: /**
082: * Return the key used for decryption
083: */
084: public Key getEncryptionKey() {
085: return ekey;
086: }
087:
088: /**
089: * Encrypt a buffer (must be multiple of 8 bytes)
090: */
091: public abstract void encrypt(ByteBuffer plain, ByteBuffer code);
092:
093: /**
094: * Class wrapper for decryption as a key operation
095: */
096: class DecryptionKey implements Key {
097: AbstractSymmetricKey k = null;
098:
099: public DecryptionKey(AbstractSymmetricKey k) {
100: this .k = k;
101: }
102:
103: public int blockSize() {
104: return getBlockSize();
105: }
106:
107: public void f(ByteBuffer m, ByteBuffer c) {
108: if (k == null) {
109: decrypt(m, c);
110: } else {
111: k.decrypt(m, c);
112: }
113: }
114: }
115:
116: protected DecryptionKey dkey = new DecryptionKey(this );
117:
118: /**
119: * Return the key used for encryption
120: */
121: public Key getDecryptionKey() {
122: return dkey;
123: }
124:
125: /**
126: * Decrypt a buffer (must be a multiple of 8 bytes)
127: */
128: public abstract void decrypt(ByteBuffer code, ByteBuffer plain);
129:
130: }
|