any_elgamal_pkcs1signature.java

来自「jpeg2000编解码」· Java 代码 · 共 498 行 · 第 1/2 页

JAVA
498
字号
// $Id: Any_ElGamal_PKCS1Signature.java,v 1.1.1.1 2002/08/27 12:32:10 grosbois Exp $//// $Log: Any_ElGamal_PKCS1Signature.java,v $// Revision 1.1.1.1  2002/08/27 12:32:10  grosbois// Add cryptix 3.2//// Revision 1.4  2000/08/17 11:40:54  edwin// java.* -> xjava.*//// Revision 1.3  1997/12/14 17:37:58  hopwood// + Committed changes below.//// Revision 1.2.1  1997/12/10  hopwood// + initInternal was checking p before it was set.// + Cosmetics.//// Revision 1.2  1997/12/10 01:17:46  raif// + documentation.// + use cryptix.util.core.BI to process BigIntegers while//   signing and verifying signatures. Needed to eliminate size//   uncertainties when converting BigInteger to/from byte arrays.// + added tests for null objects in initInternal() and engineInitSign()//   methods.// + changed the AlgId semantics to align with RSA implementations.//// Revision 1.1  1997/12/07 06:37:27  hopwood// + Major overhaul of ElGamal to match RSA.//// Revision 1.1.1.1.1  1997/11/22  hopwood// + Renamed this class to Any_RSA_PKCS1Signature from//   RawElGamalSignature.//// Revision 1.1.1.1  1997/11/03 22:36:56  hopwood// + Imported to CVS (tagged as 'start').//// $Endlog$/* * Copyright (c) 1997 Systemics Ltd * on behalf of the Cryptix Development Team.  All rights reserved. */package cryptix.provider.elgamal;import cryptix.CryptixException;import cryptix.util.core.BI;import cryptix.util.core.Debug;import cryptix.util.core.Hex;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Random;import java.security.Signature;import java.security.MessageDigest;import java.security.SecureRandom;import java.security.PublicKey;import java.security.PrivateKey;import java.security.SignatureException;import java.security.InvalidKeyException;import java.security.InvalidParameterException;import xjava.security.interfaces.ElGamalPublicKey;import xjava.security.interfaces.ElGamalPrivateKey;/** * An abstract class to digest a message and sign/verify the resulting * hash value, using any JCA MessageDigest algorithm with the ElGamal * digital signature scheme, and formatting and padding conventions * based on PKCS#1. * <p> * <b>References:</b> * <ol> *   <li> <a href="mailto:schneier@counterpane.com">Bruce Schneier</a>, *        "Section 19.6 ElGamal," *        <cite>Applied Cryptography, 2nd edition</cite>, *        John Wiley &amp; Sons, 1996. *        <p> *   <li> PKCS#1 *        An RSA Laboratories Technical Note<br> *        Version 1.5<br> *        Revised November 1, 1993<br> *        An "RSA Data Security, Inc. Public-Key Cryptography Standard (PKCS)" * </ol> * <p> * <b>$Revision: 1.1.1.1 $</b> * @author David Hopwood * @since  Cryptix 2.2.2 * * @see java.security.interfaces.ElGamalKey * @see ElGamalCipher * @see java.security.Signature */public abstract class Any_ElGamal_PKCS1Signatureextends Signature{// Debugging methods and vars.//............................................................................    private static final boolean DEBUG = Debug.GLOBAL_DEBUG;    private static final int debuglevel =        DEBUG ? Debug.getLevel("ElGamal", "Any_ElGamal_PKCS1Signature") : 0;    private static final PrintWriter err = DEBUG ? Debug.getOutput() : null;    private static void debug(String s) { err.println("Any_ElGamal_PKCS1Signature: " + s); }// Constants and variables//............................................................................    /**     * The minimum length of <i>p</i> in bits.     */    private static final int MIN_BITLENGTH = 256;    private static final int POSITIVE = 1;    private static final BigInteger ZERO = BigInteger.valueOf(0);    private static final BigInteger ONE = BigInteger.valueOf(1);    private BigInteger p;    private BigInteger g;    private BigInteger x; // null if the state is ENCRYPT    private BigInteger y;    private int primeLen;    private Random rng;    private MessageDigest md;// Constructor//............................................................................    /**     * Constructor for an Any_ElGamal_PKCS1Signature.     *     * @param  mdAlgorithm  the standard JCA algorithm name of the message     *                      digest to be used.     */    protected Any_ElGamal_PKCS1Signature (String mdAlgorithm) {        super(mdAlgorithm + "/ElGamal/PKCS#1");        try { md = MessageDigest.getInstance(mdAlgorithm); }        catch (Exception e) {            throw new CryptixException(getAlgorithm() +                ": Unable to instantiate the " + mdAlgorithm +                " MessageDigest\n" + e);        }    }// Signature abstract methods implementation//............................................................................    /**     * <b>SPI</b>: Initializes the cipher for signing, using the     * given private key. The key object must implement     * java.security.interfaces.ElGamalPrivateKey.     * <p>     * The input to this algorithm will be padded on the left with random     * bits, up to the size of a block, before signing.     *     * @param key   the private key     * @exception InvalidKeyException if !(key instanceof     *          java.security.interfaces.ElGamalPrivateKey)     */    protected void engineInitSign(PrivateKey key) throws InvalidKeyException {        if (!(key instanceof ElGamalPrivateKey))            throw new InvalidKeyException("ElGamal: signing key does not implement java.security.interfaces.ElGamalPrivateKey");        ElGamalPrivateKey privateKey = (ElGamalPrivateKey) key;        BigInteger newX = privateKey.getX();        if (newX == null) throw new InvalidKeyException("ElGamal: getX() == null");        initInternal(privateKey.getP(), privateKey.getG(),                     newX, privateKey.getY());        if (rng == null)            rng = new SecureRandom();    }    /**     * <b>SPI</b>: Initializes the cipher for verification, using the     * given public key. The key object must implement     * java.security.interfaces.ElGamalPublicKey.     *     * @param key   the public key     * @exception InvalidKeyException if !(key instanceof     *          java.security.interfaces.ElGamalPublicKey)     */    protected void engineInitVerify(PublicKey key) throws InvalidKeyException {        if (!(key instanceof ElGamalPublicKey))            throw new InvalidKeyException("ElGamal: verification key does not implement java.security.interfaces.ElGamalPublicKey");        ElGamalPublicKey publicKey = (ElGamalPublicKey) key;        initInternal(publicKey.getP(), publicKey.getG(),                     null, publicKey.getY());        if (rng == null)            rng = new SecureRandom();    }    private void initInternal(BigInteger newP, BigInteger newG,                              BigInteger newX, BigInteger newY)            throws InvalidKeyException{        if (newP == null) throw new InvalidKeyException("ElGamal: getP() == null");        if (newG == null) throw new InvalidKeyException("ElGamal: getG() == null");        if (newY == null) throw new InvalidKeyException("ElGamal: getY() == null");        if (newP.bitLength() < MIN_BITLENGTH)            throw new InvalidKeyException("ElGamal: getP().bitLength() < " + MIN_BITLENGTH);        if (newP.compareTo(ONE) <= 0)            throw new InvalidKeyException("ElGamal: getP() < 2");        p = newP;        g = newG;        x = newX;        y = newY;        primeLen = (p.bitLength() + 7) / 8;        md.reset();    }    /**     * Updates the data to be signed or verified, using one byte.     *     * @param  b  the byte to use for the update process.     * @exception SignatureException if the engine is not initialised properly.     */    protected void engineUpdate (byte b)    throws SignatureException {        if (state != VERIFY && state != SIGN)            throw new SignatureException(getAlgorithm() + ": Not initialized");        md.update(b);    }    /**     * Updates the data to be signed or verified, using the specified     * sub-array of bytes, starting at the specified offset.     *     * @param  in      the array of bytes.     * @param  offset  the offset to start from in <i>in</i>.     * @param  length  the number of bytes to use, starting at <i>offset</i>.     * @exception SignatureException if the engine is not initialised properly.     */    protected void engineUpdate (byte[] in, int offset, int length)    throws SignatureException {        if (state != VERIFY && state != SIGN)            throw new SignatureException(getAlgorithm() + ": Not initialized");        md.update(in, offset, length);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?