Example usage for org.bouncycastle.asn1 ASN1Integer ASN1Integer

List of usage examples for org.bouncycastle.asn1 ASN1Integer ASN1Integer

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1Integer ASN1Integer.

Prototype

public ASN1Integer(byte[] bytes) 

Source Link

Document

Construct an INTEGER from the passed in byte array.

Usage

From source file:net.ripe.rpki.commons.crypto.rfc3779.ResourceExtensionEncoder.java

License:BSD License

/**
 * ASId ::= INTEGER
 */
ASN1Integer asIdToDer(Asn asn) {
    return new ASN1Integer(asn.getValue());
}

From source file:net.ripe.rpki.commons.crypto.rfc3779.ResourceExtensionParserTest.java

License:BSD License

@Test(expected = IllegalArgumentException.class)
public void shouldFailOnIllegalIpAddressOrRange() {
    parser.derToIpAddressOrRange(IpResourceType.IPv4, new ASN1Integer(123));
}

From source file:net.ripe.rpki.commons.crypto.util.Asn1UtilTest.java

License:BSD License

@Test(expected = IllegalArgumentException.class)
public void shouldFailOnOutOfRangeAsn() {
    parseAsId(new ASN1Integer(-1));
}

From source file:net.schmizz.sshj.signature.SignatureDSA.java

License:Apache License

/**
 * Encodes the signature as a DER sequence (ASN.1 format).
 *///from w w w  .  j  av a 2  s  . c om
private byte[] asnEncode(byte[] sigBlob) throws IOException {
    byte[] r = new BigInteger(1, Arrays.copyOfRange(sigBlob, 0, 20)).toByteArray();
    byte[] s = new BigInteger(1, Arrays.copyOfRange(sigBlob, 20, 40)).toByteArray();

    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new ASN1Integer(r));
    vector.add(new ASN1Integer(s));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ASN1OutputStream asnOS = new ASN1OutputStream(baos);

    asnOS.writeObject(new DERSequence(vector));
    asnOS.flush();

    return baos.toByteArray();
}

From source file:net.schmizz.sshj.signature.SignatureECDSA.java

License:Apache License

/**
 * Encodes the signature as a DER sequence (ASN.1 format).
 *///  w  w w . ja v a 2  s.com
private byte[] asnEncode(byte[] sigBlob) throws IOException {
    Buffer.PlainBuffer sigbuf = new Buffer.PlainBuffer(sigBlob);
    byte[] r = sigbuf.readBytes();
    byte[] s = sigbuf.readBytes();

    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new ASN1Integer(r));
    vector.add(new ASN1Integer(s));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ASN1OutputStream asnOS = new ASN1OutputStream(baos);

    asnOS.writeObject(new DERSequence(vector));
    asnOS.flush();

    return baos.toByteArray();
}

From source file:net.sf.keystore_explorer.crypto.csr.spkac.Spkac.java

License:Open Source License

private byte[] encodeRsaPublicKeyAsBitString(RSAPublicKey rsaPublicKey) throws SpkacException {
    try {//  w  ww  . ja  v  a  2 s  .  c  o  m
        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);
    }
}

From source file:net.sf.keystore_explorer.crypto.csr.spkac.Spkac.java

License:Open Source License

private byte[] encodeDsaPublicKeyAsBitString(DSAPublicKey dsaPublicKey) throws SpkacException {
    try {//from   ww  w  .  j  a  v a2  s  .c  om
        ASN1Integer publicKey = new ASN1Integer(dsaPublicKey.getY());

        return publicKey.getEncoded(ASN1Encoding.DER);
    } catch (Exception ex) {
        throw new SpkacException(res.getString("NoEncodeDsaPublicKey.exception.message"), ex);
    }
}

From source file:net.sf.keystore_explorer.crypto.csr.spkac.Spkac.java

License:Open Source License

private ASN1Sequence createPublicKeyAndChallenge() throws SpkacException {
    ASN1EncodableVector publicKeyAlgorithm = new ASN1EncodableVector();
    publicKeyAlgorithm.add(new ASN1ObjectIdentifier(getPublicKeyAlg().oid()));

    if (getPublicKey() instanceof RSAPublicKey) {
        publicKeyAlgorithm.add(DERNull.INSTANCE);
    } else {/*from   www. j av  a  2 s. c o  m*/
        DSAParams dsaParams = ((DSAPublicKey) getPublicKey()).getParams();

        ASN1EncodableVector dssParams = new ASN1EncodableVector();
        dssParams.add(new ASN1Integer(dsaParams.getP()));
        dssParams.add(new ASN1Integer(dsaParams.getQ()));
        dssParams.add(new ASN1Integer(dsaParams.getG()));

        publicKeyAlgorithm.add(new DERSequence(dssParams));
    }

    ASN1EncodableVector spki = new ASN1EncodableVector();
    spki.add(new DERSequence(publicKeyAlgorithm));
    spki.add(encodePublicKeyAsBitString(getPublicKey()));

    ASN1EncodableVector publicKeyAndChallenge = new ASN1EncodableVector();
    publicKeyAndChallenge.add(new DERSequence(spki));
    publicKeyAndChallenge.add(new DERIA5String(getChallenge()));
    return new DERSequence(publicKeyAndChallenge);
}

From source file:net.sf.keystore_explorer.crypto.privatekey.OpenSslPvkUtil.java

License:Open Source License

/**
 * OpenSSL encode a private key.//from   ww  w  .j a  v  a  2 s. c  o m
 *
 * @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 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);
    }
}

From source file:net.sf.keystore_explorer.crypto.publickey.KeyIdentifierGenerator.java

License:Open Source License

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();
}