Java Code Examples for org.bouncycastle.asn1.DERSequence#getEncoded()

The following examples show how to use org.bouncycastle.asn1.DERSequence#getEncoded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignerUtil.java    From xipki with Apache License 2.0 6 votes vote down vote up
public static byte[] dsaSigPlainToX962(byte[] signature) throws XiSecurityException {
  Args.notNull(signature, "signature");
  if (signature.length % 2 != 0) {
    throw new XiSecurityException("signature.lenth must be even, but is odd");
  }
  byte[] ba = new byte[signature.length / 2];
  ASN1EncodableVector sigder = new ASN1EncodableVector();

  System.arraycopy(signature, 0, ba, 0, ba.length);
  sigder.add(new ASN1Integer(new BigInteger(1, ba)));

  System.arraycopy(signature, ba.length, ba, 0, ba.length);
  sigder.add(new ASN1Integer(new BigInteger(1, ba)));

  DERSequence seq = new DERSequence(sigder);
  try {
    return seq.getEncoded();
  } catch (IOException ex) {
    throw new XiSecurityException("IOException, message: " + ex.getMessage(), ex);
  }
}
 
Example 2
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * 将SEC1标准的私钥字节流恢复为PKCS8标准的字节流
 *
 * @param sec1Key
 * @return
 * @throws IOException
 */
public static byte[] convertECPrivateKeySEC1ToPKCS8(byte[] sec1Key) throws IOException {
    /**
     * 参考org.bouncycastle.asn1.pkcs.PrivateKeyInfo和
     * org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey,逆向拼装
     */
    X962Parameters params = getDomainParametersFromName(SM2Util.JDK_EC_SPEC, false);
    ASN1OctetString privKey = new DEROctetString(sec1Key);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(0)); //版本号
    v.add(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params)); //算法标识
    v.add(privKey);
    DERSequence ds = new DERSequence(v);
    return ds.getEncoded(ASN1Encoding.DER);
}
 
Example 3
Source File: SM2Util.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * @param mode         指定密文结构,旧标准的为C1C2C3,新的[《SM2密码算法使用规范》 GM/T 0009-2012]标准为C1C3C2
 * @param curveLength  曲线长度,SM2的话就是256位。
 * @param digestLength 摘要长度,如果是SM2的话因为默认使用SM3摘要,SM3摘要长度为32字节。
 * @param cipher       根据mode不同,需要输入的密文C1C2C3排列顺序不同。C1为65字节第1字节为压缩标识,这里固定为0x04,后面64字节为xy分量各32字节。C3为32字节。C2长度与原文一致。
 * @return 按指定mode DER编码后的密文
 * @throws Exception
 */
public static byte[] encodeSM2CipherToDER(Mode mode, int curveLength, int digestLength, byte[] cipher)
        throws Exception {

    byte[] c1x = new byte[curveLength];
    byte[] c1y = new byte[curveLength];
    byte[] c2 = new byte[cipher.length - c1x.length - c1y.length - 1 - digestLength];
    byte[] c3 = new byte[digestLength];

    int startPos = 1;
    System.arraycopy(cipher, startPos, c1x, 0, c1x.length);
    startPos += c1x.length;
    System.arraycopy(cipher, startPos, c1y, 0, c1y.length);
    startPos += c1y.length;
    if (mode == Mode.C1C2C3) {
        System.arraycopy(cipher, startPos, c2, 0, c2.length);
        startPos += c2.length;
        System.arraycopy(cipher, startPos, c3, 0, c3.length);
    } else if (mode == Mode.C1C3C2) {
        System.arraycopy(cipher, startPos, c3, 0, c3.length);
        startPos += c3.length;
        System.arraycopy(cipher, startPos, c2, 0, c2.length);
    } else {
        throw new Exception("Unsupported mode:" + mode);
    }

    ASN1Encodable[] arr = new ASN1Encodable[4];
    arr[0] = new ASN1Integer(c1x);
    arr[1] = new ASN1Integer(c1y);
    if (mode == Mode.C1C2C3) {
        arr[2] = new DEROctetString(c2);
        arr[3] = new DEROctetString(c3);
    } else if (mode == Mode.C1C3C2) {
        arr[2] = new DEROctetString(c3);
        arr[3] = new DEROctetString(c2);
    }
    DERSequence ds = new DERSequence(arr);
    return ds.getEncoded(ASN1Encoding.DER);
}
 
Example 4
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 5 votes vote down vote up
/**
 * 将SEC1标准的私钥字节流恢复为PKCS8标准的字节流
 *
 * @param sec1Key
 * @return
 * @throws IOException
 */
public static byte[] convertECPrivateKeySEC1ToPKCS8(byte[] sec1Key) throws IOException {
    /**
     * 参考org.bouncycastle.asn1.pkcs.PrivateKeyInfo和
     * org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey,逆向拼装
     */
    X962Parameters params = getDomainParametersFromName(SM2Util.JDK_EC_SPEC, false);
    ASN1OctetString privKey = new DEROctetString(sec1Key);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(0)); //版本号
    v.add(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params)); //算法标识
    v.add(privKey);
    DERSequence ds = new DERSequence(v);
    return ds.getEncoded(ASN1Encoding.DER);
}
 
Example 5
Source File: KeyIdentifierGenerator.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private byte[] encodeRsaPublicKeyAsBitString(RSAPublicKey rsaPublicKey) throws IOException {
	ASN1EncodableVector vec = new ASN1EncodableVector();
	vec.add(new ASN1Integer(rsaPublicKey.getModulus()));
	vec.add(new ASN1Integer(rsaPublicKey.getPublicExponent()));

	DERSequence derSequence = new DERSequence(vec);
	return derSequence.getEncoded();
}
 
Example 6
Source File: Spkac.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
private byte[] encodeRsaPublicKeyAsBitString(RSAPublicKey rsaPublicKey) throws SpkacException {
	try {
		ASN1EncodableVector vec = new ASN1EncodableVector ();
		vec.add(new ASN1Integer(rsaPublicKey.getModulus()));
		vec.add(new ASN1Integer(rsaPublicKey.getPublicExponent()));
		DERSequence derSequence = new DERSequence(vec);
		return derSequence.getEncoded(ASN1Encoding.DER);
	} catch (Exception ex) {
		throw new SpkacException(res.getString("NoEncodeRsaPublicKey.exception.message"), ex);
	}
}
 
Example 7
Source File: CertificateTrustPoint.java    From signer with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void parse(ASN1Primitive derObject) {
    ASN1Sequence derSequence = ASN1Object.getDERSequence(derObject);
    DERSequence x509Sequence = (DERSequence) derSequence.getObjectAt(0).toASN1Primitive();
    try {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(x509Sequence.getEncoded());
        this.trustpoint = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(byteArrayInputStream);
    } catch (Throwable error) {
        error.printStackTrace();
    }

    int total = derSequence.size();

    if (total > 0) {
        for (int i = 0; i < total; i++) {
            ASN1Primitive object = derSequence.getObjectAt(i).toASN1Primitive();
            if (object instanceof DERTaggedObject) {
                DERTaggedObject derTaggedObject = (DERTaggedObject) object;
                TAG tag = TAG.getTag(derTaggedObject.getTagNo());
                switch (tag) {
                    case pathLenConstraint:
                        this.pathLenConstraint = new PathLenConstraint();
                        this.pathLenConstraint.parse(object);
                        break;
                    case acceptablePolicySet:
                        this.acceptablePolicySet = new AcceptablePolicySet();
                        this.acceptablePolicySet.parse(object);
                        break;
                    case nameConstraints:
                        this.nameConstraints = new NameConstraints();
                        this.nameConstraints.parse(object);

                        break;
                    case policyConstraints:
                        this.policyConstraints = new PolicyConstraints();
                        this.policyConstraints.parse(object);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
Example 8
Source File: OpenSslPvkUtil.java    From keystore-explorer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * OpenSSL encode a private key.
 *
 * @return The encoding
 * @param privateKey
 *            The private key
 * @throws CryptoException
 *             Problem encountered while getting the encoded private key
 */
public static byte[] get(PrivateKey privateKey) throws CryptoException {
	// DER encoding for each key type is a sequence
	ASN1EncodableVector vec = new ASN1EncodableVector();

	if (privateKey instanceof ECPrivateKey) {
		try {
			ECPrivateKey ecPrivKey = (ECPrivateKey) privateKey;
			org.bouncycastle.asn1.sec.ECPrivateKey keyStructure = EccUtil.convertToECPrivateKeyStructure(ecPrivKey);
			return keyStructure.toASN1Primitive().getEncoded();
		} catch (IOException e) {
			throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), e);
		}
	} else if (privateKey instanceof RSAPrivateCrtKey) {
		RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;

		vec.add(new ASN1Integer(VERSION));
		vec.add(new ASN1Integer(rsaPrivateKey.getModulus()));
		vec.add(new ASN1Integer(rsaPrivateKey.getPublicExponent()));
		vec.add(new ASN1Integer(rsaPrivateKey.getPrivateExponent()));
		vec.add(new ASN1Integer(rsaPrivateKey.getPrimeP()));
		vec.add(new ASN1Integer(rsaPrivateKey.getPrimeQ()));
		vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentP()));
		vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentQ()));
		vec.add(new ASN1Integer(rsaPrivateKey.getCrtCoefficient()));
	} else {
		DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
		DSAParams dsaParams = dsaPrivateKey.getParams();

		BigInteger primeModulusP = dsaParams.getP();
		BigInteger primeQ = dsaParams.getQ();
		BigInteger generatorG = dsaParams.getG();
		BigInteger secretExponentX = dsaPrivateKey.getX();

		// Derive public key from private key parts, ie Y = G^X mod P
		BigInteger publicExponentY = generatorG.modPow(secretExponentX, primeModulusP);

		vec.add(new ASN1Integer(VERSION));
		vec.add(new ASN1Integer(primeModulusP));
		vec.add(new ASN1Integer(primeQ));
		vec.add(new ASN1Integer(generatorG));
		vec.add(new ASN1Integer(publicExponentY));
		vec.add(new ASN1Integer(secretExponentX));
	}
	DERSequence derSequence = new DERSequence(vec);

	try {
		return derSequence.getEncoded();
	} catch (IOException ex) {
		throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), ex);
	}
}