1 19 20 package com.maverick.crypto.publickey; 21 22 import java.io.IOException ; 23 import java.math.BigInteger ; 24 25 import com.maverick.crypto.digests.SHA1Digest; 26 import com.maverick.crypto.security.SecureRandom; 27 28 32 public class RsaPrivateCrtKey 33 extends RsaPrivateKey { 34 35 protected BigInteger publicExponent; 36 protected BigInteger primeP; 37 protected BigInteger primeQ; 38 protected BigInteger primeExponentP; 39 protected BigInteger primeExponentQ; 40 protected BigInteger crtCoefficient; 41 42 52 53 public RsaPrivateCrtKey(BigInteger modulus, 54 BigInteger publicExponent, 55 BigInteger privateExponent, 56 BigInteger primeP, BigInteger primeQ, 57 BigInteger primeExponentP, 58 BigInteger primeExponentQ, 59 BigInteger 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 72 public BigInteger getPublicExponent() { 73 return publicExponent; 74 } 75 76 79 public BigInteger getPrimeP() { 80 return primeP; 81 } 82 83 86 public BigInteger getPrimeQ() { 87 return primeQ; 88 } 89 90 93 public BigInteger getPrimeExponentP() { 94 return primeExponentP; 95 } 96 97 100 public BigInteger getPrimeExponentQ() { 101 return primeExponentQ; 102 } 103 104 107 public BigInteger getCrtCoefficient() { 108 return crtCoefficient; 109 } 110 111 114 public byte[] sign(byte[] msg) throws IOException { 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 dataInt = new BigInteger (1, data); 130 int mLen = (getModulus().bitLength() + 7) / 8; 131 132 dataInt = Rsa.padPKCS1(dataInt, 1, mLen); 133 134 BigInteger signatureInt = null; 135 136 BigInteger primeP = getPrimeP(); 137 BigInteger primeQ = getPrimeQ(); 138 BigInteger primeExponentP = getPrimeExponentP(); 139 BigInteger primeExponentQ = getPrimeExponentQ(); 140 BigInteger 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 |