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:de.tsenger.animamea.asn1.AmECPublicKey.java

License:Open Source License

public AmECPublicKey(String oidString, BigInteger p, BigInteger a, BigInteger b, ECPoint G, BigInteger r,
        ECPoint Y, BigInteger f) {
    super(oidString);
    this.p = new DERTaggedObject(false, 1, new ASN1Integer(p));
    this.a = new DERTaggedObject(false, 2, new ASN1Integer(a));
    this.b = new DERTaggedObject(false, 3, new ASN1Integer(b));
    this.G = new DERTaggedObject(false, 4, new DEROctetString(G.getEncoded()));
    this.r = new DERTaggedObject(false, 5, new ASN1Integer(r));
    this.Y = new DERTaggedObject(false, 6, new DEROctetString(Y.getEncoded()));
    this.f = new DERTaggedObject(false, 7, new ASN1Integer(f));
    vec.add(this.p);
    vec.add(this.a);
    vec.add(this.b);
    vec.add(this.G);
    vec.add(this.r);
    vec.add(this.Y);
    vec.add(this.f);
}

From source file:de.tsenger.animamea.asn1.AmRSAPublicKey.java

License:Open Source License

public AmRSAPublicKey(String oidString, BigInteger n, BigInteger e) {
    super(oidString);
    this.n = new DERTaggedObject(false, 1, new ASN1Integer(n));
    this.e = new DERTaggedObject(false, 2, new ASN1Integer(e));
    vec.add(this.n);
    vec.add(this.e);
}

From source file:de.tsenger.animamea.asn1.PaceInfo.java

License:Open Source License

public PaceInfo(String oid, int version, int parameterId) {
    this.protocol = new ASN1ObjectIdentifier(oid);
    this.version = new ASN1Integer(version);
    this.parameterId = new ASN1Integer(parameterId);
}

From source file:de.tsenger.animamea.iso7816.MSESetAT.java

License:Open Source License

/**
 * Setzt das Tag 0x83 (Reference of public / secret key) fr PACE
 * //from  w  w w .j a va 2s . co  m
 * @param kr
 *            Referenziert das verwendete Passwort: 1: MRZ 2: CAN 3: PIN 4:
 *            PUK
 */
public void setKeyReference(int kr) {
    DERTaggedObject to = new DERTaggedObject(false, 0x03, new ASN1Integer(kr));
    try {
        do83KeyReference = to.getEncoded(ASN1Encoding.DER);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:de.tsenger.animamea.iso7816.MSESetAT.java

License:Open Source License

/**
 * Setzt das Tag 0x84 (Reference of a private key / Reference for computing
 * a session key)/* ww  w. j av  a 2 s.c o m*/
 * 
 * @param pkr
 *            Bei PACE wird der Index der zu verwendenden Domain Parameter
 *            angegeben Bei CA wird der Index des zu verwendenden Private
 *            Keys angegeben Bei RI wird der Index des zu verwendenden
 *            Private Keys angegeben
 */
public void setPrivateKeyReference(int pkr) {
    DERTaggedObject to = new DERTaggedObject(false, 0x04, new ASN1Integer(pkr));
    try {
        do84PrivateKeyReference = to.getEncoded(ASN1Encoding.DER);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:dorkbox.build.util.jar.JarSigner.java

License:Apache License

private static void writeDsaKeysToFile(DSAPrivateKeyParameters wimpyPrivateKey,
        DSAPublicKeyParameters wimpyPublicKey, File wimpyKeyRawFile) throws IOException, FileNotFoundException {

    DSAParameters parameters = wimpyPublicKey.getParameters(); // has to convert to DSAParameter so encoding works.
    byte[] publicKeyBytes = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(wimpyPublicKey.getY())).getEncoded();
    // SAME AS:/*from ww w  .ja v a2  s  .co m*/
    //        Certificate[] certificates = Launcher.class.getProtectionDomain().getCodeSource().getCertificates();
    //        if (certificates.length != 1) {
    //            // WHOOPS!
    //            Exit.FailedSecurity("Incorrect certificate length!");
    //        }
    //
    //        Certificate certificate = certificates[0];
    //        PublicKey publicKey = certificate.getPublicKey();
    //        byte[] publicKeyBytes = publicKey.getEncoded();
    //
    //        digest.reset();
    //        digest.update(publicKeyBytes, 0, publicKeyBytes.length);
    //        hashPublicKeyBytes = digest.digest();

    parameters = wimpyPrivateKey.getParameters();
    byte[] privateKeyBytes = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(wimpyPrivateKey.getX())).getEncoded();

    // write public length to bytes.
    byte[] publicKeySize = new byte[] { (byte) (publicKeyBytes.length >>> 24),
            (byte) (publicKeyBytes.length >>> 16), (byte) (publicKeyBytes.length >>> 8),
            (byte) (publicKeyBytes.length >>> 0) };

    ByteArrayOutputStream keyOutputStream = new ByteArrayOutputStream(
            4 + publicKeyBytes.length + privateKeyBytes.length);

    keyOutputStream.write(publicKeyBytes, 0, publicKeyBytes.length);
    keyOutputStream.write(privateKeyBytes, 0, privateKeyBytes.length);
    keyOutputStream.write(publicKeySize, 0, publicKeySize.length); // mess with people staring at the keys (store length at the end).

    displayByteHash(publicKeyBytes);

    // write out the file
    OutputStream outputStream = new FileOutputStream(wimpyKeyRawFile);
    keyOutputStream.writeTo(outputStream);
    Sys.close(outputStream);
}

From source file:dorkbox.util.crypto.DsaTest.java

License:Apache License

@Test
public void DsaJceSerializaion() throws IOException {

    AsymmetricCipherKeyPair generateKeyPair = CryptoDSA
            .generateKeyPair(new SecureRandom(entropySeed.getBytes()), 1024);
    DSAPrivateKeyParameters privateKey = (DSAPrivateKeyParameters) generateKeyPair.getPrivate();
    DSAPublicKeyParameters publicKey = (DSAPublicKeyParameters) generateKeyPair.getPublic();

    // public key as bytes.
    DSAParameters parameters = publicKey.getParameters();
    byte[] bs = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(publicKey.getY())).getEncoded();

    parameters = privateKey.getParameters();
    byte[] bs2 = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(privateKey.getX())).getEncoded();

    DSAPrivateKeyParameters privateKey2 = (DSAPrivateKeyParameters) PrivateKeyFactory.createKey(bs2);
    DSAPublicKeyParameters publicKey2 = (DSAPublicKeyParameters) PublicKeyFactory.createKey(bs);

    // test via signing
    byte[] bytes = "hello, my name is inigo montoya".getBytes();

    BigInteger[] signature = CryptoDSA.generateSignature(privateKey, new SecureRandom(entropySeed.getBytes()),
            bytes);/*w w  w. j  a v a  2 s  .  co m*/

    boolean verify1 = CryptoDSA.verifySignature(publicKey, bytes, signature);

    if (!verify1) {
        fail("failed signature verification");
    }

    boolean verify2 = CryptoDSA.verifySignature(publicKey2, bytes, signature);

    if (!verify2) {
        fail("failed signature verification");
    }

    // now reverse who signs what.
    BigInteger[] signatureB = CryptoDSA.generateSignature(privateKey2, new SecureRandom(entropySeed.getBytes()),
            bytes);

    boolean verifyB1 = CryptoDSA.verifySignature(publicKey, bytes, signatureB);

    if (!verifyB1) {
        fail("failed signature verification");
    }

    boolean verifyB2 = CryptoDSA.verifySignature(publicKey2, bytes, signatureB);

    if (!verifyB2) {
        fail("failed signature verification");
    }
}

From source file:edu.tamu.tcat.crypto.bouncycastle.ASN1SeqKeyImpl.java

License:Apache License

private static ASN1Sequence getParameters(ECParameterSpec ecParameterSpec) throws EncodingException {
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(1));
    EllipticCurve curve = ecParameterSpec.getCurve();

    ASN1Sequence fieldId = getField(curve.getField());
    v.add(fieldId);/*  ww w.j  a  v  a 2  s  . c om*/
    v.add(getCurve(curve));

    org.bouncycastle.math.ec.ECPoint g = EC5Util.convertPoint(ecParameterSpec, ecParameterSpec.getGenerator(),
            false);
    byte[] encoded = g.getEncoded();
    v.add(new DEROctetString(encoded));

    v.add(new ASN1Integer(ecParameterSpec.getOrder()));
    v.add(new ASN1Integer(ecParameterSpec.getCofactor()));

    return new DERSequence(v);
}

From source file:edu.tamu.tcat.crypto.bouncycastle.ASN1SeqKeyImpl.java

License:Apache License

private static ASN1Sequence getField(ECField field) throws EncodingException {
    ASN1EncodableVector v = new ASN1EncodableVector();
    if (field instanceof ECFieldFp) {
        ECFieldFp fpField = (ECFieldFp) field;
        v.add(new ASN1ObjectIdentifier("1.2.840.10045.1.1"));
        v.add(new ASN1Integer(fpField.getP()));
    } else//from w w  w  . j  av a 2 s .  c  o  m
        throw new EncodingException("Only know how to encode prime fields");

    return new DERSequence(v);
}

From source file:es.gob.afirma.signers.pkcs7.DigestedData.java

License:Open Source License

/** Crea un objeto CMS DigestedData.
 * @param digestAlgo ALgoritmo de huella digital
 * @param contentInfo ContentInfo//w w w  .  ja va  2s .  c  o  m
 * @param digest Valor de la huella digital
 */
public DigestedData(final AlgorithmIdentifier digestAlgo, final ContentInfo contentInfo,
        final ASN1OctetString digest) {
    this.version = new ASN1Integer(0);
    this.digestAlgorithm = digestAlgo;
    this.contentInfo = contentInfo;
    this.digest = digest;
}