jdkdsapublickey.java

来自「bouncycastle 是一个JAVA安全提供者」· Java 代码 · 共 126 行

JAVA
126
字号
package org.bouncycastle.jce.provider;import java.io.IOException;import java.math.BigInteger;import java.security.interfaces.DSAParams;import java.security.interfaces.DSAPublicKey;import java.security.spec.DSAParameterSpec;import java.security.spec.DSAPublicKeySpec;import org.bouncycastle.asn1.ASN1Sequence;import org.bouncycastle.asn1.DERInteger;import org.bouncycastle.asn1.x509.AlgorithmIdentifier;import org.bouncycastle.asn1.x509.DSAParameter;import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;import org.bouncycastle.crypto.params.DSAPublicKeyParameters;public class JDKDSAPublicKey    implements DSAPublicKey{    private BigInteger      y;    private DSAParams       dsaSpec;    JDKDSAPublicKey(        DSAPublicKeySpec    spec)    {        this.y = spec.getY();        this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG());    }    JDKDSAPublicKey(        DSAPublicKey    key)    {        this.y = key.getY();        this.dsaSpec = key.getParams();    }    JDKDSAPublicKey(        DSAPublicKeyParameters  params)    {        this.y = params.getY();        this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());    }    JDKDSAPublicKey(        BigInteger        y,        DSAParameterSpec  dsaSpec)    {        this.y = y;        this.dsaSpec = dsaSpec;    }    JDKDSAPublicKey(        SubjectPublicKeyInfo    info)    {        DSAParameter            params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());        DERInteger              derY = null;        try        {            derY = (DERInteger)info.getPublicKey();        }        catch (IOException e)        {            throw new IllegalArgumentException("invalid info structure in DSA public key");        }        this.y = derY.getValue();        this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());    }    public String getAlgorithm()    {        return "DSA";    }    public String getFormat()    {        return "X.509";    }    public byte[] getEncoded()    {        SubjectPublicKeyInfo    info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(y));        return info.getDEREncoded();    }    public DSAParams getParams()    {        return dsaSpec;    }    public BigInteger getY()    {        return y;    }    public String toString()    {        StringBuffer    buf = new StringBuffer();        String          nl = System.getProperty("line.separator");        buf.append("DSA Public Key" + nl);        buf.append("            y: " + this.getY().toString(16) + nl);        return buf.toString();    }        public boolean equals(        Object o)    {        if (!(o instanceof DSAPublicKey))        {            return false;        }                DSAPublicKey other = (DSAPublicKey)o;                return this.getY().equals(other.getY())             && this.getParams().getG().equals(other.getParams().getG())             && this.getParams().getP().equals(other.getParams().getP())             && this.getParams().getQ().equals(other.getParams().getQ());    }}

⌨️ 快捷键说明

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