Search in sources :

Example 6 with DEROutputStream

use of org.gudy.bouncycastle.asn1.DEROutputStream in project nuls by nuls-io.

the class SM2Utils method encrypt.

public static byte[] encrypt(byte[] publicKey, byte[] data) throws IOException {
    if (publicKey == null || publicKey.length == 0) {
        return null;
    }
    if (data == null || data.length == 0) {
        return null;
    }
    byte[] source = new byte[data.length];
    System.arraycopy(data, 0, source, 0, data.length);
    Cipher cipher = new Cipher();
    SM2 sm2 = SM2.Instance();
    ECPoint userKey = sm2.ecc_curve.decodePoint(publicKey);
    ECPoint c1 = cipher.initEnc(sm2, userKey);
    cipher.encrypt(source);
    byte[] c3 = new byte[32];
    cipher.dofinal(c3);
    DERInteger x = new DERInteger(c1.getX().toBigInteger());
    DERInteger y = new DERInteger(c1.getY().toBigInteger());
    DEROctetString derDig = new DEROctetString(c3);
    DEROctetString derEnc = new DEROctetString(source);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(x);
    v.add(y);
    v.add(derDig);
    v.add(derEnc);
    DERSequence seq = new DERSequence(v);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DEROutputStream dos = new DEROutputStream(bos);
    dos.writeObject(seq);
    return bos.toByteArray();
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ECPoint(org.bouncycastle.math.ec.ECPoint) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERInteger(org.bouncycastle.asn1.DERInteger) DEROutputStream(org.bouncycastle.asn1.DEROutputStream)

Example 7 with DEROutputStream

use of org.gudy.bouncycastle.asn1.DEROutputStream in project structr by structr.

the class SignedJarBuilder method writeSignatureBlock.

/**
 * Write the certificate file with a digital signature.
 */
private void writeSignatureBlock(final JarOutputStream jos, final CMSTypedData data, final X509Certificate publicKey, final PrivateKey privateKey) throws IOException, CertificateEncodingException, OperatorCreationException, CMSException {
    final List<X509Certificate> certList = new ArrayList<>();
    certList.add(publicKey);
    final JcaCertStore certs = new JcaCertStore(certList);
    final CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    final ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1with" + privateKey.getAlgorithm()).build(privateKey);
    gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build()).setDirectSignature(true).build(sha1Signer, publicKey));
    gen.addCertificates(certs);
    final CMSSignedData sigData = gen.generate(data, false);
    final ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded());
    final DEROutputStream dos = new DEROutputStream(jos);
    dos.writeObject(asn1.readObject());
}
Also used : CMSSignedDataGenerator(org.bouncycastle.cms.CMSSignedDataGenerator) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) JcaSignerInfoGeneratorBuilder(org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ArrayList(java.util.ArrayList) ContentSigner(org.bouncycastle.operator.ContentSigner) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) JcaDigestCalculatorProviderBuilder(org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) DEROutputStream(org.bouncycastle.asn1.DEROutputStream)

Example 8 with DEROutputStream

use of org.gudy.bouncycastle.asn1.DEROutputStream in project BiglyBT by BiglySoftware.

the class X509ExtensionsGenerator method addExtension.

/**
 * Add an extension with the given oid and the passed in value to be included
 * in the OCTET STRING associated with the extension.
 *
 * @param oid  OID for the extension.
 * @param critical  true if critical, false otherwise.
 * @param value the ASN.1 object to be included in the extension.
 */
public void addExtension(DERObjectIdentifier oid, boolean critical, DEREncodable value) {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    try {
        dOut.writeObject(value);
    } catch (IOException e) {
        throw new IllegalArgumentException("error encoding value: " + e);
    }
    this.addExtension(oid, critical, bOut.toByteArray());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DEROutputStream(org.gudy.bouncycastle.asn1.DEROutputStream)

Example 9 with DEROutputStream

use of org.gudy.bouncycastle.asn1.DEROutputStream in project BiglyBT by BiglySoftware.

the class JCEECPrivateKey method getEncoded.

/**
 * Return a PKCS8 representation of the key. The sequence returned
 * represents a full PrivateKeyInfo object.
 *
 * @return a PKCS8 representation of the key.
 */
@Override
public byte[] getEncoded() {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    X962Parameters params = null;
    if (ecSpec instanceof ECNamedCurveParameterSpec) {
        params = new X962Parameters(X962NamedCurves.getOID(((ECNamedCurveParameterSpec) ecSpec).getName()));
    } else {
        X9ECParameters ecP = new X9ECParameters(ecSpec.getCurve(), ecSpec.getG(), ecSpec.getN(), ecSpec.getH(), ecSpec.getSeed());
        params = new X962Parameters(ecP);
    }
    PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.getDERObject()), new ECPrivateKeyStructure(this.getD()).getDERObject());
    try {
        dOut.writeObject(info);
        dOut.close();
    } catch (IOException e) {
        throw new RuntimeException("Error encoding EC private key");
    }
    return bOut.toByteArray();
}
Also used : X962Parameters(org.gudy.bouncycastle.asn1.x9.X962Parameters) X9ECParameters(org.gudy.bouncycastle.asn1.x9.X9ECParameters) ECNamedCurveParameterSpec(org.gudy.bouncycastle.jce.spec.ECNamedCurveParameterSpec) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ECPrivateKeyStructure(org.gudy.bouncycastle.asn1.sec.ECPrivateKeyStructure) IOException(java.io.IOException) PrivateKeyInfo(org.gudy.bouncycastle.asn1.pkcs.PrivateKeyInfo) AlgorithmIdentifier(org.gudy.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 10 with DEROutputStream

use of org.gudy.bouncycastle.asn1.DEROutputStream in project BiglyBT by BiglySoftware.

the class JCEECPublicKey method getEncoded.

@Override
public byte[] getEncoded() {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    X962Parameters params = null;
    if (ecSpec instanceof ECNamedCurveParameterSpec) {
        params = new X962Parameters(X962NamedCurves.getOID(((ECNamedCurveParameterSpec) ecSpec).getName()));
    } else {
        X9ECParameters ecP = new X9ECParameters(ecSpec.getCurve(), ecSpec.getG(), ecSpec.getN(), ecSpec.getH(), ecSpec.getSeed());
        params = new X962Parameters(ecP);
    }
    ASN1OctetString p = (ASN1OctetString) (new X9ECPoint(this.getQ()).getDERObject());
    SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.getDERObject()), p.getOctets());
    try {
        dOut.writeObject(info);
        dOut.close();
    } catch (IOException e) {
        throw new RuntimeException("Error encoding EC public key");
    }
    return bOut.toByteArray();
}
Also used : ECNamedCurveParameterSpec(org.gudy.bouncycastle.jce.spec.ECNamedCurveParameterSpec) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SubjectPublicKeyInfo(org.gudy.bouncycastle.asn1.x509.SubjectPublicKeyInfo) AlgorithmIdentifier(org.gudy.bouncycastle.asn1.x509.AlgorithmIdentifier)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 IOException (java.io.IOException)20 DEROutputStream (org.bouncycastle.asn1.DEROutputStream)13 DEROutputStream (org.gudy.bouncycastle.asn1.DEROutputStream)9 X509Certificate (java.security.cert.X509Certificate)7 OutputStream (java.io.OutputStream)4 AlgorithmIdentifier (org.gudy.bouncycastle.asn1.x509.AlgorithmIdentifier)4 CertificateException (java.security.cert.CertificateException)3 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 DEROctetString (org.bouncycastle.asn1.DEROctetString)3 DERSequence (org.bouncycastle.asn1.DERSequence)3 JcaCertStore (org.bouncycastle.cert.jcajce.JcaCertStore)3 CMSSignedData (org.bouncycastle.cms.CMSSignedData)3 CMSSignedDataGenerator (org.bouncycastle.cms.CMSSignedDataGenerator)3 ContentSigner (org.bouncycastle.operator.ContentSigner)3 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)3 JcaDigestCalculatorProviderBuilder (org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder)3 X509Extension (org.gudy.bouncycastle.asn1.x509.X509Extension)3 KeyStoreException (java.security.KeyStoreException)2