Example usage for org.bouncycastle.asn1.util ASN1Dump dumpAsString

List of usage examples for org.bouncycastle.asn1.util ASN1Dump dumpAsString

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.util ASN1Dump dumpAsString.

Prototype

public static String dumpAsString(Object obj) 

Source Link

Document

dump out a DER object as a formatted string, in non-verbose mode.

Usage

From source file:com.android.verity.BootKey.java

License:Apache License

public void dump() throws Exception {
    System.out.println(ASN1Dump.dumpAsString(toASN1Primitive()));
}

From source file:com.aqnote.shared.cryptology.cert.main.AQCRLMain.java

License:Open Source License

public static void createCRL() throws CertException {

    try {/*  w w  w.  ja  v a 2s . c  o m*/
        X509v2CRLBuilder crlBuilder = new X509v2CRLBuilder(X500NameUtil.createRootCaPrincipal(), new Date());
        crlBuilder.setNextUpdate(new Date(System.currentTimeMillis() + DateConstant.ONE_YEAR));
        X509CRLHolder crlHolder = crlBuilder.build(new JcaContentSignerBuilder(SHA256_RSA)
                .setProvider(JCE_PROVIDER).build(CaCertLoader.getRootCaKeyPair(USER_CERT_PASSWD).getPrivate()));
        X509CRL crl = new JcaX509CRLConverter().setProvider(JCE_PROVIDER).getCRL(crlHolder);
        FileOutputStream fostream = new FileOutputStream(CRL_FILE);
        PKCSWriter.storeCRLFile(crl, fostream);

        ASN1Dump.dumpAsString(crlHolder.toASN1Structure());
    } catch (OperatorCreationException e) {
        throw new CertException(e);
    } catch (IOException e) {
        throw new CertException(e);
    } catch (InvalidKeyException e) {
        throw new CertException(e);
    } catch (CRLException e) {
        throw new CertException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertException(e);
    } catch (NoSuchProviderException e) {
        throw new CertException(e);
    } catch (SignatureException e) {
        throw new CertException(e);
    } catch (Exception e) {
        throw new CertException(e);
    }

    return;
}

From source file:com.aqnote.shared.encrypt.cert.main.bc.AQCRLCreator.java

License:Open Source License

public static void createNewCRL() throws CertException {

    try {//  w  ww  .j av  a 2s  .  c om
        X509v2CRLBuilder crlBuilder = new X509v2CRLBuilder(X500NameUtil.createRootPrincipal(), new Date());
        crlBuilder.setNextUpdate(new Date(System.currentTimeMillis() + DateConstant.ONE_YEAR));
        X509CRLHolder crlHolder = crlBuilder.build(new JcaContentSignerBuilder(SHA256_RSA)
                .setProvider(JCE_PROVIDER).build(CaCertLoader.getCaKeyPair().getPrivate()));
        X509CRL crl = new JcaX509CRLConverter().setProvider(JCE_PROVIDER).getCRL(crlHolder);
        FileOutputStream fostream = new FileOutputStream(MAD_CRL_FILE);
        PKCSWriter.storeCRLFile(crl, fostream);

        ASN1Dump.dumpAsString(crlHolder.toASN1Structure());
    } catch (OperatorCreationException e) {
        throw new CertException(e);
    } catch (IOException e) {
        throw new CertException(e);
    } catch (InvalidKeyException e) {
        throw new CertException(e);
    } catch (CRLException e) {
        throw new CertException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertException(e);
    } catch (NoSuchProviderException e) {
        throw new CertException(e);
    } catch (SignatureException e) {
        throw new CertException(e);
    } catch (Exception e) {
        throw new CertException(e);
    }

    return;
}

From source file:io.aos.crypto.spl05.ASN1DumpExample.java

License:Apache License

public static void main(String... args) throws Exception {
    byte[] baseData = new byte[5];
    Date created = new Date(0); // 1/1/1970

    MyStructure structure = new MyStructure(0, created, baseData, "hello", "world");

    System.out.println(ASN1Dump.dumpAsString(structure));

    structure = new MyStructure(1, created, baseData, "hello", "world");

    System.out.println(ASN1Dump.dumpAsString(structure));

    ASN1InputStream aIn = new ASN1InputStream(structure.getEncoded());

    System.out.println(ASN1Dump.dumpAsString(aIn.readObject()));
}

From source file:io.aos.crypto.spl05.EncryptedPrivateKeyInfoExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    // generate a key pair
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
    kpg.initialize(128, Utils.createFixedRandom());

    KeyPair pair = kpg.generateKeyPair();

    // wrapping step
    char[] password = "hello".toCharArray();
    byte[] salt = new byte[20];
    int iCount = 100;
    String pbeAlgorithm = "PBEWithSHAAnd3-KeyTripleDES-CBC";
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password, salt, iCount);
    SecretKeyFactory secretKeyFact = SecretKeyFactory.getInstance(pbeAlgorithm, "BC");
    Cipher cipher = Cipher.getInstance(pbeAlgorithm, "BC");

    cipher.init(Cipher.WRAP_MODE, secretKeyFact.generateSecret(pbeKeySpec));

    byte[] wrappedKey = cipher.wrap(pair.getPrivate());

    System.out.println(/*from  w w w  .  ja  va  2  s  .  com*/
            ASN1Dump.dumpAsString(new ASN1InputStream(cipher.getParameters().getEncoded()).readObject()));

    // create carrier   
    EncryptedPrivateKeyInfo pInfo = new EncryptedPrivateKeyInfo(cipher.getParameters(), wrappedKey);

    // unwrapping step - note we only use the password
    pbeKeySpec = new PBEKeySpec(password);

    cipher = Cipher.getInstance(pInfo.getAlgName(), "BC");

    cipher.init(Cipher.DECRYPT_MODE, secretKeyFact.generateSecret(pbeKeySpec), pInfo.getAlgParameters());

    PKCS8EncodedKeySpec pkcs8Spec = pInfo.getKeySpec(cipher);
    KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privKey = keyFact.generatePrivate(pkcs8Spec);

    ASN1InputStream aIn = new ASN1InputStream(pkcs8Spec.getEncoded());
    PrivateKeyInfo info = PrivateKeyInfo.getInstance(aIn.readObject());

    System.out.println(ASN1Dump.dumpAsString(info));
    System.out.println(ASN1Dump.dumpAsString(info.getPrivateKey()));

    if (privKey.equals(pair.getPrivate())) {
        System.out.println("key recovery successful");
    } else {
        System.out.println("key recovery failed");
    }
}

From source file:io.aos.crypto.spl05.IVExample.java

License:Apache License

public static void main(String... args) throws Exception {
    // set up the parameters object
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES", "BC");
    IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]);

    params.init(ivSpec);//from w  w w  .j av  a  2s. co m

    // look at the ASN.1 encodng.
    ASN1InputStream aIn = new ASN1InputStream(params.getEncoded("ASN.1"));

    System.out.println(ASN1Dump.dumpAsString(aIn.readObject()));
}

From source file:io.aos.crypto.spl05.PKCS1SigEncodingExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("SHA256withRSA", "BC");

    // generate a signature
    signature.initSign(keyPair.getPrivate());

    byte[] message = new byte[] { (byte) 'a', (byte) 'b', (byte) 'c' };

    signature.update(message);/*from   www .j  ava  2 s. c o  m*/

    byte[] sigBytes = signature.sign();

    // open the signature
    Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");

    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());

    byte[] decSig = cipher.doFinal(sigBytes);

    // parse the signature
    ASN1InputStream aIn = new ASN1InputStream(decSig);
    ASN1Sequence seq = (ASN1Sequence) aIn.readObject();

    System.out.println(ASN1Dump.dumpAsString(seq));

    // grab a digest of the correct type
    MessageDigest hash = MessageDigest.getInstance("SHA-256", "BC");

    hash.update(message);

    ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
    if (MessageDigest.isEqual(hash.digest(), sigHash.getOctets())) {
        System.out.println("hash verification succeeded");
    } else {
        System.out.println("hash verification failed");
    }
}

From source file:io.aos.crypto.spl05.PKCS8EncodedKeySpecExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    // create the keys
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");

    generator.initialize(128, Utils.createFixedRandom());

    KeyPair pair = generator.generateKeyPair();

    // dump private key
    ASN1InputStream aIn = new ASN1InputStream(pair.getPrivate().getEncoded());
    PrivateKeyInfo info = PrivateKeyInfo.getInstance(aIn.readObject());

    System.out.println(ASN1Dump.dumpAsString(info));
    System.out.println(ASN1Dump.dumpAsString(info.getPrivateKey()));

    // create from specification
    PKCS8EncodedKeySpec pkcs8Spec = new PKCS8EncodedKeySpec(pair.getPrivate().getEncoded());
    KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privKey = keyFact.generatePrivate(pkcs8Spec);

    if (privKey.equals(pair.getPrivate())) {
        System.out.println("key recovery successful");
    } else {//from www . j av  a2 s.  c o  m
        System.out.println("key recovery failed");
    }
}

From source file:io.aos.crypto.spl05.PSSParamExample.java

License:Apache License

public static void main(String... args) throws Exception {
    Signature signature = Signature.getInstance("SHA1withRSAandMGF1", "BC");

    // set the default parameters
    signature.setParameter(PSSParameterSpec.DEFAULT);

    // get the default parameters
    AlgorithmParameters params = signature.getParameters();

    // look at the ASN.1 encodng.
    ASN1InputStream aIn = new ASN1InputStream(params.getEncoded("ASN.1"));

    System.out.println(ASN1Dump.dumpAsString(aIn.readObject()));
}

From source file:io.aos.crypto.spl05.X509EncodedKeySpecExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    // create the keys
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");

    generator.initialize(128, Utils.createFixedRandom());

    KeyPair pair = generator.generateKeyPair();

    // dump public key
    ASN1InputStream aIn = new ASN1InputStream(pair.getPublic().getEncoded());
    SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(aIn.readObject());

    System.out.println(ASN1Dump.dumpAsString(info));
    System.out.println(ASN1Dump.dumpAsString(info.getPublicKey()));

    // create from specification
    X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(pair.getPublic().getEncoded());
    KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
    PublicKey pubKey = keyFact.generatePublic(x509Spec);

    if (pubKey.equals(pair.getPublic())) {
        System.out.println("key recovery successful");
    } else {//from  ww w .  j  av a2  s.  c  o m
        System.out.println("key recovery failed");
    }
}