Example usage for org.bouncycastle.asn1 ASN1InputStream ASN1InputStream

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

Introduction

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

Prototype

public ASN1InputStream(byte[] input) 

Source Link

Document

Create an ASN1InputStream based on the input byte array.

Usage

From source file:fi.laverca.Pkcs7.java

License:Apache License

/**
 * Convert a byte array to a PKCS7 SignedData object
 * @param bytes byte array/*from   w  w  w .  jav a  2 s  .c om*/
 * @return PKCS7 SignedData object
 */
public static SignedData bytesToPkcs7SignedData(byte[] bytes) {

    if (bytes == null) {
        throw new IllegalArgumentException("null bytes");
    }

    ASN1InputStream ais = new ASN1InputStream(bytes);
    ASN1Object asn1 = null;
    try {
        asn1 = ais.readObject();
    } catch (IOException ioe) {
        throw new IllegalArgumentException("not a pkcs7 signature");
    } finally {
        try {
            ais.close();
        } catch (IOException e) {
            // Ignore
        }
    }

    ContentInfo ci = ContentInfo.getInstance(asn1);

    DERObjectIdentifier typeId = ci.getContentType();
    if (!typeId.equals(PKCSObjectIdentifiers.signedData)) {
        throw new IllegalArgumentException("not a pkcs7 signature");
    }

    return SignedData.getInstance(ci.getContent());
}

From source file:fr.insalyon.creatis.vip.core.server.business.proxy.ProxyClient.java

License:Open Source License

private void printKey(PrivateKey key, PrintStream out) throws IOException {
    out.println("-----BEGIN RSA PRIVATE KEY-----");
    ByteArrayInputStream inStream = new ByteArrayInputStream(key.getEncoded());
    ASN1InputStream derInputStream = new ASN1InputStream(inStream);
    ASN1Primitive keyInfo = derInputStream.readObject();
    PrivateKeyInfo pki;/*from   ww  w .  j  a  v a2s  .  c  om*/
    pki = PrivateKeyInfo.getInstance(keyInfo);
    ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive();
    // build and return the actual key
    ASN1Sequence privKey = (ASN1Sequence) innerType;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DEROutputStream der = new DEROutputStream(bout);
    der.writeObject(privKey);
    printB64(bout.toByteArray(), out);
    out.println("-----END RSA PRIVATE KEY-----");
}

From source file:id.govca.detachedsignature.FileHelper.java

public static byte[] CMStoDER(CMSSignedData sigData) throws IOException {
    ByteArrayInputStream inStream = new ByteArrayInputStream(sigData.getEncoded());
    ASN1InputStream asnInputStream = new ASN1InputStream(inStream);

    ASN1Primitive asp = asnInputStream.readObject();
    byte[] result = asp.getEncoded("DER");

    return result;
}

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 ww .j a va  2  s . c o  m*/
            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 a v  a 2  s. c om

    // 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 ww  w  . j  a va  2 s. co  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  ww  w. j a va  2 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   w w w. j  av  a2s. c o m
        System.out.println("key recovery failed");
    }
}