ecutil.java

来自「内容:基于jdk1.4的加密算法的具体实现」· Java 代码 · 共 181 行

JAVA
181
字号
package org.bouncycastle.jce.provider;import java.security.InvalidKeyException;import java.security.PrivateKey;import java.security.PublicKey;import org.bouncycastle.asn1.DERObjectIdentifier;import org.bouncycastle.asn1.nist.NISTNamedCurves;import org.bouncycastle.asn1.sec.SECNamedCurves;import org.bouncycastle.asn1.x9.X962NamedCurves;import org.bouncycastle.asn1.x9.X9ECParameters;import org.bouncycastle.crypto.params.AsymmetricKeyParameter;import org.bouncycastle.crypto.params.ECDomainParameters;import org.bouncycastle.crypto.params.ECPrivateKeyParameters;import org.bouncycastle.crypto.params.ECPublicKeyParameters;import org.bouncycastle.jce.interfaces.ECPrivateKey;import org.bouncycastle.jce.interfaces.ECPublicKey;import org.bouncycastle.jce.spec.ECParameterSpec;/** * utility class for converting jce/jca ECDSA, ECDH, and ECDHC * objects into their org.bouncycastle.crypto counterparts. */public class ECUtil{    /**     * Returns a sorted array of middle terms of the reduction polynomial.     * @param k The unsorted array of middle terms of the reduction polynomial     * of length 1 or 3.     * @return the sorted array of middle terms of the reduction polynomial.     * This array always has length 3.     */    static int[] convertMidTerms(        int[] k)    {        int[] res = new int[3];                if (k.length == 1)        {            res[0] = k[0];        }        else        {            if (k.length != 3)            {                throw new IllegalArgumentException("Only Trinomials and pentanomials supported");            }            if (k[0] < k[1] && k[0] < k[2])            {                res[0] = k[0];                if (k[1] < k[2])                {                    res[1] = k[1];                    res[2] = k[2];                }                else                {                    res[1] = k[2];                    res[2] = k[1];                }            }            else if (k[1] < k[2])            {                res[0] = k[1];                if (k[0] < k[2])                {                    res[1] = k[0];                    res[2] = k[2];                }                else                {                    res[1] = k[2];                    res[2] = k[0];                }            }            else            {                res[0] = k[2];                if (k[0] < k[1])                {                    res[1] = k[0];                    res[2] = k[1];                }                else                {                    res[1] = k[1];                    res[2] = k[0];                }            }        }        return res;    }    static AsymmetricKeyParameter generatePublicKeyParameter(        PublicKey    key)        throws InvalidKeyException    {        if (key instanceof ECPublicKey)        {            ECPublicKey    k = (ECPublicKey)key;            ECParameterSpec s = k.getParameters();            return new ECPublicKeyParameters(                            k.getQ(),                            new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));        }        throw new InvalidKeyException("can't identify EC public key.");    }    static AsymmetricKeyParameter generatePrivateKeyParameter(        PrivateKey    key)        throws InvalidKeyException    {        if (key instanceof ECPrivateKey)        {            ECPrivateKey  k = (ECPrivateKey)key;            ECParameterSpec s = k.getParameters();            return new ECPrivateKeyParameters(                            k.getD(),                            new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));        }                                throw new InvalidKeyException("can't identify EC private key.");    }    public static DERObjectIdentifier getNamedCurveOid(        String name)    {        DERObjectIdentifier oid = X962NamedCurves.getOID(name);                if (oid == null)        {            oid = SECNamedCurves.getOID(name);            if (oid == null)            {                oid = NISTNamedCurves.getOID(name);            }        }        return oid;    }        public static X9ECParameters getNamedCurveByOid(        DERObjectIdentifier oid)    {        X9ECParameters params = X962NamedCurves.getByOID(oid);                if (params == null)        {            params = SECNamedCurves.getByOID(oid);            if (params == null)            {                params = NISTNamedCurves.getByOID(oid);            }        }        return params;    }    public static String getCurveName(        DERObjectIdentifier oid)    {        String name = X962NamedCurves.getName(oid);                if (name == null)        {            name = SECNamedCurves.getName(oid);            if (name == null)            {                name = NISTNamedCurves.getName(oid);            }        }        return name;    }}

⌨️ 快捷键说明

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