KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > publickey > RsaPrivateCrtKey


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.publickey;
21
22 import java.io.IOException JavaDoc;
23 import java.math.BigInteger JavaDoc;
24
25 import com.maverick.crypto.digests.SHA1Digest;
26 import com.maverick.crypto.security.SecureRandom;
27
28 /**
29  *
30  * @author Lee David Painter
31  */

32 public class RsaPrivateCrtKey
33     extends RsaPrivateKey {
34
35   protected BigInteger JavaDoc publicExponent;
36   protected BigInteger JavaDoc primeP;
37   protected BigInteger JavaDoc primeQ;
38   protected BigInteger JavaDoc primeExponentP;
39   protected BigInteger JavaDoc primeExponentQ;
40   protected BigInteger JavaDoc crtCoefficient;
41
42   /*public RsaPrivateCrtKey(BigInteger modulus,
43                           BigInteger publicExponent,
44                           BigInteger privateExponent,
45                           BigInteger primeP, BigInteger primeQ,
46                           BigInteger crtCoefficient) {
47     this(modulus, publicExponent, privateExponent, primeP, primeQ,
48          Rsa.getPrimeExponent(privateExponent, primeP),
49          Rsa.getPrimeExponent(privateExponent, primeQ),
50          crtCoefficient);
51   }*/

52
53   public RsaPrivateCrtKey(BigInteger JavaDoc modulus,
54                           BigInteger JavaDoc publicExponent,
55                           BigInteger JavaDoc privateExponent,
56                           BigInteger JavaDoc primeP, BigInteger JavaDoc primeQ,
57                           BigInteger JavaDoc primeExponentP,
58                           BigInteger JavaDoc primeExponentQ,
59                           BigInteger JavaDoc crtCoefficient) {
60     super(modulus, privateExponent);
61     this.publicExponent = publicExponent;
62     this.primeP = primeP;
63     this.primeQ = primeQ;
64     this.primeExponentP = primeExponentP;
65     this.primeExponentQ = primeExponentQ;
66     this.crtCoefficient = crtCoefficient;
67   }
68
69   /* (non-Javadoc)
70  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#getPublicExponent()
71  */

72 public BigInteger JavaDoc getPublicExponent() {
73     return publicExponent;
74   }
75
76   /* (non-Javadoc)
77  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#getPrimeP()
78  */

79 public BigInteger JavaDoc getPrimeP() {
80     return primeP;
81   }
82
83   /* (non-Javadoc)
84  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#getPrimeQ()
85  */

86 public BigInteger JavaDoc getPrimeQ() {
87     return primeQ;
88   }
89
90   /* (non-Javadoc)
91  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#getPrimeExponentP()
92  */

93 public BigInteger JavaDoc getPrimeExponentP() {
94     return primeExponentP;
95   }
96
97   /* (non-Javadoc)
98  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#getPrimeExponentQ()
99  */

100 public BigInteger JavaDoc getPrimeExponentQ() {
101     return primeExponentQ;
102   }
103
104   /* (non-Javadoc)
105  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#getCrtCoefficient()
106  */

107 public BigInteger JavaDoc getCrtCoefficient() {
108     return crtCoefficient;
109   }
110
111   /* (non-Javadoc)
112  * @see com.maverick.crypto.publickey.RsaPrivateCrtKeyInterface#sign(byte[])
113  */

114 public byte[] sign(byte[] msg) throws IOException JavaDoc {
115
116
117     SHA1Digest hash = new SHA1Digest();
118     hash.update(msg, 0, msg.length);
119
120     byte[] data = new byte[hash.getDigestSize()];
121     hash.doFinal(data, 0);
122
123
124       byte[] tmp = new byte[data.length + ASN_SHA1.length];
125       System.arraycopy(ASN_SHA1, 0, tmp, 0, ASN_SHA1.length);
126       System.arraycopy(data, 0, tmp, ASN_SHA1.length, data.length);
127       data = tmp;
128
129       BigInteger JavaDoc dataInt = new BigInteger JavaDoc(1, data);
130       int mLen = (getModulus().bitLength() + 7) / 8;
131
132       dataInt = Rsa.padPKCS1(dataInt, 1, mLen);
133
134       BigInteger JavaDoc signatureInt = null;
135
136       BigInteger JavaDoc primeP = getPrimeP();
137       BigInteger JavaDoc primeQ = getPrimeQ();
138       BigInteger JavaDoc primeExponentP = getPrimeExponentP();
139       BigInteger JavaDoc primeExponentQ = getPrimeExponentQ();
140       BigInteger JavaDoc crtCoefficient = getCrtCoefficient();
141
142       signatureInt = Rsa.doPrivateCrt(dataInt,
143                                       primeP, primeQ,
144                                       primeExponentP,
145                                       primeExponentQ,
146                                       crtCoefficient);
147
148       byte[] sig = unsignedBigIntToBytes(signatureInt, mLen);
149
150       return sig;
151   }
152
153 }
154
Popular Tags