📄 rsaprivkeycrt.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: RSAPrivKeyCrt.java,v 1.7 1999/02/02 00:58:02 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/RSAPrivKeyCrt.java,v $
* $Revision: 1.7 $
* $Date: 1999/02/02 00:58:02 $
* $State: Exp $
*/
import java.math.BigInteger;
import java.security.interfaces.RSAPrivateCrtKey;
import java.io.*;
/**
* A class for ABA RSA private keys that can use the Chinese
* Remainder Theorem.
*/
public class RSAPrivKeyCrt extends RSAPrivKey implements RSAPrivateCrtKey
{
public final static String ident = "$Id: RSAPrivKeyCrt.java,v 1.7 1999/02/02 00:58:02 leachbj Exp $";
//==================================
// Protected Interface
//==================================
/**
* The public exponent.
*/
protected BigInteger exponent;
/**
* The larger of the two prime factors.
*/
protected BigInteger p;
/**
* The smaller of the two prime factors.
*/
protected BigInteger q;
/**
* The multiplicative inverse of q % p [calculated].
*/
protected BigInteger qInv;
/**
* p-1 [calculated].
*/
protected BigInteger pMinus1;
/**
* q-1 [calculated].
*/
protected BigInteger qMinus1;
protected BigInteger dP;
protected BigInteger dQ;
/**
* Construct an empty RSAPrivKey.
*/
public RSAPrivKeyCrt()
{
}
public RSAPrivKeyCrt(byte[] encKey)
{
pkcs8Decode(encKey);
}
/**
* Construct an RSAPrivKeyCrt the appropriate big numbers
*
* @param modulus the modulus.
* @param exponent the public exponent.
* @param d the private exponent.
* @param p the prime p.
* @param q the prime q.
* @param dP the prime exponent p.
* @param dQ the prime exponent q.
* @param qInv the CRT coefficient.
*/
public RSAPrivKeyCrt(
BigInteger modulus,
BigInteger exponent,
BigInteger d,
BigInteger p,
BigInteger q,
BigInteger dP,
BigInteger dQ,
BigInteger qInv)
{
super(modulus, d);
this.exponent = exponent;
this.p = p;
this.q = q;
this.dP = dP;
this.dQ = dQ;
this.qInv = qInv;
}
/**
* Returns the crtCoefficient.
*
* @return the crtCoefficient.
*/
public BigInteger getCrtCoefficient()
{
return qInv;
}
public byte[] getEncoded()
{
return pkcs8Encode();
}
public String getFormat()
{
return "PKCS#8";
}
/**
* Returns the primeExponentP.
*
* @return the primeExponentP.
*/
public BigInteger getPrimeExponentP()
{
return dP;
}
/**
* Returns the primeExponentQ.
*
* @return the primeExponentQ.
*/
public BigInteger getPrimeExponentQ()
{
return dQ;
}
/**
* Returns the prime P.
*
* @return the prime P.
*/
public BigInteger getPrimeP()
{
return p;
}
/**
* Returns the prime Q.
*
* @return the prime Q.
*/
public BigInteger getPrimeQ()
{
return q;
}
/**
* Returns the public exponent.
*
* @return the public exponent.
*/
public BigInteger getPublicExponent()
{
return exponent;
}
private void pkcs8Decode(byte[] encKey)
{
ByteArrayInputStream bin = new ByteArrayInputStream(encKey);
try
{
int tag,len;
// PrivateKeyInfo, SEQUENCE
tag = DER.readTag(bin);
len = DER.readLen(bin);
bin.skip(DER.version.length);
bin.skip(DER.rsaEncryptionAlgorithmIdentifier.length);
// PrivateKey, OCTET STRING
tag = DER.readTag(bin);
len = DER.readLen(bin);
// RSAPrivateKey, SEQUENCE
tag = DER.readTag(bin);
len = DER.readLen(bin);
bin.skip(DER.version.length);
modulus = DER.readDERint(bin);
exponent = DER.readDERint(bin);
d = DER.readDERint(bin);
p = DER.readDERint(bin);
q = DER.readDERint(bin);
dP = DER.readDERint(bin);
dQ = DER.readDERint(bin);
qInv = DER.readDERint(bin);
}
catch (IOException e)
{
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
/*
PrivateKeyInfo ::= SEQUENCE {
version Version,
privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
privateKey PrivateKey,
attributes [0] IMPLICIT Attributes OPTIONAL }
Version ::= INTEGER
PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
PrivateKey ::= OCTET STRING
Attributes ::= SET OF Attribute
EncryptedPrivateKeyInfo ::= SEQUENCE {
encryptionAlgorithm EncryptionAlgorithmIdentifier,
encryptedData EncryptedData }
EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
EncryptedData ::= OCTET STRING
*/
private byte[] pkcs8Encode()
{
try
{
ByteArrayOutputStream bout;
/*
* construct the RSAPrivateKey data
*/
bout = new ByteArrayOutputStream();
bout.write(DER.version);
DER.writeDERint(bout, modulus);
DER.writeDERint(bout, exponent);
DER.writeDERint(bout, d);
DER.writeDERint(bout, p);
DER.writeDERint(bout, q);
DER.writeDERint(bout, dP);
DER.writeDERint(bout, dQ);
DER.writeDERint(bout, qInv);
byte[] privateKey = bout.toByteArray();
/*
* prepend the SEQUENCE header
*/
bout = new ByteArrayOutputStream();
bout.write(DER.SEQUENCE | DER.CONSTRUCTED);
DER.writeDERlen(bout, privateKey.length);
bout.write(privateKey);
privateKey = bout.toByteArray();
/*
* create the PrivateKeyInfo data
*/
bout = new ByteArrayOutputStream();
bout.write(DER.version);
bout.write(DER.rsaEncryptionAlgorithmIdentifier);
bout.write(DER.OCTET_STRING);
DER.writeDERlen(bout, privateKey.length);
bout.write(privateKey);
byte[] privateKeyInfo = bout.toByteArray();
/*
* prepend the SEQUENCE header
*/
bout = new ByteArrayOutputStream();
bout.write(DER.SEQUENCE | DER.CONSTRUCTED);
DER.writeDERlen(bout, privateKeyInfo.length);
bout.write(privateKeyInfo);
return bout.toByteArray();
}
catch (IOException e)
{
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
/**
* Generate a String representation of this key.
*
* @return The key as a string.
*/
public String toString()
{
return modulus.toString(16) + "."
+ exponent.toString(16) + "."
+ d.toString(16) + "."
+ p.toString(16) + "."
+ q.toString(16) + "."
+ dP.toString(16) + "."
+ dQ.toString(16) + "."
+ qInv.toString(16);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -