List of usage examples for org.bouncycastle.asn1 ASN1InputStream readObject
public ASN1Primitive readObject() throws IOException
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 www. j av a 2 s . co 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 ww . j a v a 2s .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);/* www .j a v a2 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 {/* w w w . ja v a2 s . c om*/ 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 ww .ja v a2s . c om*/ System.out.println("key recovery failed"); } }
From source file:io.apigee.trireme.crypto.algorithms.DsaKeyPairProvider.java
License:Open Source License
/** * DSA Key Pair format -- the PEM file contains an ASN.1 sequence containing six integers: * p, q, g, y, and x. We construct the appropriate Java data structures after parsing those. *///from w w w. ja va 2 s. com @Override public KeyPair readKeyPair(String algorithm, Reader rdr, char[] passphrase) throws CryptoException, IOException { PemReader reader = new PemReader(rdr); PemObject pemObj = reader.readPemObject(); if (pemObj == null) { throw new CryptoException("Not a valid PEM file"); } if (!DSA_TYPE.equals(pemObj.getType())) { throw new CryptoException("PEM file does not contain a DSA private key"); } ASN1InputStream asnIn = new ASN1InputStream(pemObj.getContent()); ASN1Primitive ao = asnIn.readObject(); if (ao == null) { throw new CryptoException("PEM file does not contain an ASN.1 object"); } if (!(ao instanceof ASN1Sequence)) { throw new CryptoException("PEM file does not contain a sequence"); } ASN1Sequence seq = (ASN1Sequence) ao; if (seq.size() != 6) { throw new CryptoException("ASN.1 sequence is the wrong length for a DSA key"); } DERInteger p = (DERInteger) seq.getObjectAt(1); DERInteger q = (DERInteger) seq.getObjectAt(2); DERInteger g = (DERInteger) seq.getObjectAt(3); DERInteger y = (DERInteger) seq.getObjectAt(4); DERInteger x = (DERInteger) seq.getObjectAt(5); try { KeyFactory factory = KeyFactory.getInstance("DSA"); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y.getValue(), p.getValue(), q.getValue(), g.getValue()); PublicKey pub = factory.generatePublic(pubSpec); DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(x.getValue(), p.getValue(), q.getValue(), g.getValue()); PrivateKey key = factory.generatePrivate(keySpec); return new KeyPair(pub, key); } catch (GeneralSecurityException gse) { throw new CryptoException(gse); } }
From source file:it.scoppelletti.spaceship.security.FakeCertificateFactory.java
License:Apache License
@SuppressWarnings({ "deprecation", "TryFinallyCanBeTryWithResources" }) public static X509Certificate create(PublicKey publicKey, FakeKeyPairGeneratorSpec spec) throws IOException, CertificateParsingException { ASN1ObjectIdentifier sigAlgOid;//from w w w . jav a 2 s . c o m AlgorithmIdentifier sigAlgId; org.bouncycastle.jce.X509Principal subject; ASN1EncodableVector result; Certificate cert; org.bouncycastle.jce.provider.X509CertificateObject x509Cert; TBSCertificate tbsCertificate; ASN1InputStream publicKeyInfoIn = null; V3TBSCertificateGenerator tbsGenerator; byte[] signature; sigAlgOid = PKCSObjectIdentifiers.sha256WithRSAEncryption; sigAlgId = new AlgorithmIdentifier(sigAlgOid, DERNull.INSTANCE); signature = new byte[1]; tbsGenerator = new V3TBSCertificateGenerator(); try { publicKeyInfoIn = new ASN1InputStream(publicKey.getEncoded()); tbsGenerator.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(publicKeyInfoIn.readObject())); } finally { if (publicKeyInfoIn != null) { publicKeyInfoIn.close(); } } subject = new org.bouncycastle.jce.X509Principal(spec.getSubject().getEncoded()); tbsGenerator.setSerialNumber(new ASN1Integer(spec.getSerialNumber())); tbsGenerator.setSubject(subject); tbsGenerator.setIssuer(subject); tbsGenerator.setStartDate(new Time(spec.getStartDate())); tbsGenerator.setEndDate(new Time(spec.getEndDate())); tbsGenerator.setSignature(sigAlgId); tbsCertificate = tbsGenerator.generateTBSCertificate(); result = new ASN1EncodableVector(); result.add(tbsCertificate); result.add(sigAlgId); result.add(new DERBitString(signature)); cert = Certificate.getInstance(new DERSequence(result)); x509Cert = new org.bouncycastle.jce.provider.X509CertificateObject(cert); return x509Cert; }