KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.Text;
46
47 /**
48  * RSA Private Key implementation
49  *
50  * @author Stan Bailes
51  */

52 public class RSAPrivateKey extends RSAKey implements PrivateKey {
53     private final BigInteger JavaDoc ZERO = BigInteger.ZERO;
54     private final BigInteger JavaDoc ONE = BigInteger.ONE;
55     private final BigInteger JavaDoc TWO = new BigInteger JavaDoc("2");
56     private final BigInteger JavaDoc THREE = new BigInteger JavaDoc("3");
57
58     private BigInteger JavaDoc p; // first prime
59
private BigInteger JavaDoc q; // second prime
60

61     private BigInteger JavaDoc d; // decryption exponent
62
private BigInteger JavaDoc dP; // d % (p-1)
63
private BigInteger JavaDoc dQ; // d % (q-1)
64
private BigInteger JavaDoc qInv; // p.modInverse(q)
65

66     /**
67      * Default constructor
68      */

69     public RSAPrivateKey() {}
70
71     /**
72      * Initialize from string representation
73      */

74     public void init(String JavaDoc s) {
75         String JavaDoc[] v = Text.extractN(s, "*:*:*:*:*:*:*:*");
76         this.p = new BigInteger JavaDoc(v[2]);
77         this.q = new BigInteger JavaDoc(v[3]);
78         super.init(v[4], Integer.parseInt(v[1]), p.multiply(q));
79         init();
80     }
81
82     /**
83      * Return the string representation
84      */

85     public String JavaDoc toString() {
86         return "RSAPriv:" + size + ":" + p + ":" + q + ":" +
87             dP + ":" + dQ + ":" + qInv + ":" + text;
88     }
89
90     /**
91      * Create a new keypair
92      */

93     public void init(String JavaDoc text, int siz, Random JavaDoc random) {
94         int s2 = (siz / 2);
95         int offset = (int)(random.nextDouble() * 10) - 5;
96         offset += (offset < 0) ? -5.0 : 5.0;
97
98         do {
99             this.p = makePrime(s2 + offset, random);
100             this.q = makePrime(s2 - offset, random);
101             this.n = p.multiply(q);
102         } while (n.bitLength() != siz);
103
104         super.init(text, siz, n);
105         init();
106     }
107
108     /**
109      * Decrypt a buffer (in chunks)
110      */

111     public void f(ByteBuffer JavaDoc cipher, ByteBuffer JavaDoc plain) {
112         engine(cipher, plain);
113     }
114
115     /**
116      * Return the public portion of the key
117      */

118     public PublicKey getPublicKey() {
119         RSAPublicKey pub = new RSAPublicKey();
120         pub.init(text, size, n);
121         return pub;
122     }
123     
124     private final void init() {
125         BigInteger JavaDoc p1 = p.subtract(BigInteger.ONE);
126         BigInteger JavaDoc q1 = q.subtract(BigInteger.ONE);
127         BigInteger JavaDoc m = p1.multiply(q1); // M = (p-1)*(q-1)
128

129         this.d = e.modInverse(m);
130
131         dP = d.remainder(p1);
132         dQ = d.remainder(q1);
133         qInv = p.modInverse(q);
134     }
135
136     final private BigInteger JavaDoc makePrime(int size, Random JavaDoc random) {
137         BigInteger JavaDoc bn = new BigInteger JavaDoc(size, 100, random);
138         while (bn.subtract(ONE).remainder(THREE).equals(ZERO)) {
139             bn = new BigInteger JavaDoc(size, 100, random);
140         }
141         return bn;
142     }
143
144     final protected BigInteger JavaDoc engine(BigInteger JavaDoc c) {
145         if (false) {
146             return c.modPow(d, n);
147         } else {
148             BigInteger JavaDoc m1 = c.modPow(dP, p);
149             BigInteger JavaDoc m2 = c.modPow(dQ, q);
150             BigInteger JavaDoc h = ((m2.subtract(m1)).multiply(qInv)).remainder(q);
151             if (h.compareTo(BigInteger.ZERO) < 0) {
152                 h = h.add(q);
153             }
154             return m1.add(h.multiply(p));
155         }
156     }
157
158 }
159
Popular Tags