KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigInteger JavaDoc;
43 import java.nio.ByteBuffer JavaDoc;
44
45 import com.quadcap.util.Debug;
46 import com.quadcap.util.Util;
47
48 /**
49  * Implementation detail; factoring operations common to public and
50  * private RSA keys.
51  *
52  * @author Stan Bailes
53  */

54 public abstract class RSAKey implements Key {
55     static final BigInteger JavaDoc e = new BigInteger JavaDoc("3");
56     protected BigInteger JavaDoc n;
57     String JavaDoc text;
58     Random JavaDoc random;
59     int size;
60     byte[] buf;
61
62     protected void init(String JavaDoc text, int size, BigInteger JavaDoc n) {
63         this.text = text;
64         this.size = size;
65         this.n = n;
66         this.buf = new byte[blockSize()];
67     }
68                            
69     /**
70      * Run the algorithm (encryption or decyption) on the specified
71      * buffers
72      */

73     public void engine(ByteBuffer JavaDoc in, ByteBuffer JavaDoc out) {
74         int byteCnt = blockSize();
75         Debug.println("BLOCK SIZE: " + byteCnt + " bytes");
76         Debug.println("in: [" + in.position() + "/" + in.limit() + "-" +
77                       in.capacity() + "]");
78         Debug.println("out: [" + out.position() + "/" + out.limit() + "-" +
79                       out.capacity() + "]");
80         while (in.remaining() > 0) {
81             int len = Math.min(byteCnt, in.remaining());
82             in.get(buf, 0, len);
83             BigInteger JavaDoc pi = new BigInteger JavaDoc(1, buf);
84             BigInteger JavaDoc xi = engine(pi);
85             byte[] t = xi.toByteArray();
86             if (t.length > byteCnt) {
87                 if (t.length > byteCnt + 1) {
88                     throw new RuntimeException JavaDoc("---- t.length = " + t.length);
89                 } else {
90                     out.put(t, 0, byteCnt);
91                 }
92             } else {
93                 if (t.length < byteCnt) {
94                     int lim = byteCnt - t.length;
95                     while (lim-- > 0) out.put((byte)0);
96                 }
97                 out.put(t);
98             }
99             for (int i = 0; i < buf.length; i++) buf[i] = 0;
100         }
101     }
102
103     /**
104      * The derived class actually implements this
105      */

106     public abstract void f(ByteBuffer JavaDoc src, ByteBuffer JavaDoc dst);
107     
108     /**
109      * The derived class (public/private) implements this as appropriate
110      */

111     protected abstract BigInteger JavaDoc engine(BigInteger JavaDoc x);
112     
113     final Random JavaDoc getRandom() {
114         if (random == null) {
115             random = new Random JavaDoc();
116         }
117         return random;
118     }
119
120     final void randomBytes(byte[] buf, int pos, int cnt) {
121         byte[] tbuf = new byte[cnt];
122         getRandom().nextBytes(tbuf);
123         System.arraycopy(tbuf, 0, buf, pos, cnt);
124     }
125
126     public int blockSize() {
127         return size >> 3;
128     }
129         
130 }
131
Popular Tags