public class RawRSASignature
extends java.security.SignatureSpi
Digital signatures are used for ensuring data authentication and data integrity. RSA PKCS#1v1.5 based signature algorithms use a proper hash function for creating a message digest of the message to be signed. This class requires that the application does the hashing. Subsequently this message digest is encrypted with the RSA private key of the entity going to sign the message. Message and encrypted message digest together are sent to the intended receiver that verifies the signature by decrypting the received encrypted message digest with the corresponding RSA public key, and comparing it with the hash value derived from the received original message after hashing it with the same hash function as used by the entity that has sent the message.
This class follows the guidelines presented in PKCS#1) for implementing a signature algorithm based on the RSA encryption method.
An application wishing to sign some message or to verify some signature, generally
has to perform four steps (in the following example, HASH
has to be
replaced by the name of the desired hash algorithm):
AlgorithmID hashAlgorithm = AlgorithmID.HASH; // e.g. AlgorithmID.sha1 MessageDigest hashEngine = MessageDigest.getInstance(hashAlgorithm.getImplementationName()); byte[] rawHash = hashEngine.digest(data); byte[] preparedHash = new DigestInfo(hashAlgorithm, rawHash).toByteArray();
getInstance
method, e.g.:
Signature rawRsaSignatureEngine = Signature.getInstance("RSA", "IAIK");
rawRsaSignatureEngine.initSign(rsaPrivateKey);
rawRsaSignatureEngine.initVerify(rsaPublicKey);
sign
method returning the signature as byte array.
Otherwise, if the Signature object has been initialized for verifying, first the
prepared hash to be verified is supplied to the Signature object, and subsequently the
signature is verified by calling the verify
method, supplied with
the byte array holding the corresponding signature value:
rawRsaSignatureEngine.update(preparedHash); byte[] signature = rawRsaSignatureEngine.sign();
rawRsaSignatureEngine.update(preparedHash); System.out.println("Signature " + (rawRsaSignatureEngine.verify(signature) ? "correct!" : "incorrect!"));
Signature
Modifier and Type | Field and Description |
---|---|
protected java.io.ByteArrayOutputStream |
dataBuffer_
Data buffer to which the DigestInfo is written when supplied via an update method.
|
Constructor and Description |
---|
RawRSASignature()
Creates a RSA Signature object.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
engineGetParameter(java.lang.String param)
This method is not implemented and only throws an InvalidParameterException
|
protected void |
engineInitSign(java.security.PrivateKey privateKey)
SPI: Initializes this Signature object with the given
RSA private key for going to sign some data.
|
protected void |
engineInitSign(java.security.PrivateKey privateKey,
java.security.SecureRandom random)
SPI: Initializes this Signature object with the given RSA private
key and the given SecureRandom generator for going to sign some data.
|
protected void |
engineInitVerify(java.security.PublicKey pk)
SPI: Initializes this Signature object with the given
RSA public key for performing a signature verification.
|
protected void |
engineSetParameter(java.lang.String param,
java.lang.Object value)
This method is not implemented and only throws an InvalidParameterException
|
protected byte[] |
engineSign()
SPI: Returns a byte array holding the signature resulting from all
already performed prepared hash update operations.
|
protected void |
engineUpdate(byte b)
SPI: Updates the data to be signed or verified
with the specified byte.
|
protected void |
engineUpdate(byte[] b,
int off,
int len)
SPI: Updates the data to be signed or verified with the
specified number of bytes, beginning at the specified offset within the given byte array.
|
protected boolean |
engineVerify(byte[] sigBytes)
Verifies the given signature of a message according to PKCS#1.
|
protected java.io.ByteArrayOutputStream dataBuffer_
public RawRSASignature()
Applications use
for creating a Signature object.Signature.getInstance("RSA");
Signature.getInstance(java.lang.String)
protected void engineInitVerify(java.security.PublicKey pk) throws java.security.InvalidKeyException
engineInitVerify
in class java.security.SignatureSpi
pk
- the RSA public key belonging to the RSA private key that has been used for signing.java.security.InvalidKeyException
- if a key encoding error occursprotected void engineInitSign(java.security.PrivateKey privateKey) throws java.security.InvalidKeyException
engineInitSign
in class java.security.SignatureSpi
privateKey
- the RSA private key to be used for signing.java.security.InvalidKeyException
- if a key encoding error occursprotected void engineInitSign(java.security.PrivateKey privateKey, java.security.SecureRandom random) throws java.security.InvalidKeyException
engineInitSign
in class java.security.SignatureSpi
privateKey
- the RSA private key to be used for signing.random
- the random number generatorjava.security.InvalidKeyException
- if a key decoding error occursprotected byte[] engineSign() throws java.security.SignatureException
engineSign
in class java.security.SignatureSpi
java.security.SignatureException
- if an error occurs when creating the signatureprotected boolean engineVerify(byte[] sigBytes) throws java.security.SignatureException
PKCS#1
defines a signature as bit string, which has to be converted into an octet
string, RSA decrypted with the signer's RSA public key
giving the prepared hash, which is normally an DER encoded
DigestInfo
object.
This prepared hash is compared to the prepared hash provided by the
application. If they are equal, the verification succeeded and
true
is returned, otherwise false
is
returned.
Please notice that first step of bit-string-to-octet-string conversion already has to be done when calling this verify method. In this way the supplied sigBytes value has to be the octet string signature value.
engineVerify
in class java.security.SignatureSpi
sigBytes
- the signature bytes to be verifiedtrue
if signature is OK, false
otherwisejava.security.SignatureException
- if an error occurs when verifying the signatureprotected void engineUpdate(byte b)
engineUpdate
in class java.security.SignatureSpi
b
- the byte to be used for updating.protected void engineUpdate(byte[] b, int off, int len)
engineUpdate
in class java.security.SignatureSpi
b
- the byte array holding the data to be used for this update operation.off
- the offset, indicating the start position within the given byte array.len
- the number of bytes to be obtained from the given byte array, starting at the given position.protected void engineSetParameter(java.lang.String param, java.lang.Object value) throws java.security.InvalidParameterException
engineSetParameter
in class java.security.SignatureSpi
java.security.InvalidParameterException
- This Method is not supportedprotected java.lang.Object engineGetParameter(java.lang.String param) throws java.security.InvalidParameterException
engineGetParameter
in class java.security.SignatureSpi
java.security.InvalidParameterException
- This Method is not supported