private ByteString pkcs1Bytes() { try { PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.getPrivate().getEncoded()); return ByteString.of(privateKeyInfo.parsePrivateKey().toASN1Primitive().getEncoded()); } catch (IOException e) { throw new AssertionError(e); } }
private static byte[] toByteArray(ASN1Primitive obj) { try { return obj.getEncoded(); } catch (IOException e) { throw new IllegalArgumentException("Unable to encode object"); } }
private static byte[] toByteArray(ASN1Primitive obj) { try { return obj.getEncoded(); } catch (IOException e) { throw new IllegalArgumentException("Unable to encode object"); } }
@SuppressWarnings("rawtypes") private Object createGenericTypeObject(ASN1Primitive current, Class<T> clazz2) throws Throwable { Class<?>[] paramIS = new Class[1]; paramIS[0] = InputStream.class; Method m = clazz.getDeclaredMethod("decode", paramIS); InputStream is = new ByteArrayInputStream(current.getEncoded()); Object clazzInstance = clazz.newInstance(); m.invoke(clazzInstance, is); return clazzInstance; }
PublicKey pub = pair.getPublic(); byte[] pubBytes = pub.getEncoded(); SubjectPublicKeyInfo spkInfo = SubjectPublicKeyInfo.getInstance(pubBytes); ASN1Primitive primitive = spkInfo.parsePublicKey(); byte[] publicKeyPKCS1 = primitive.getEncoded();
/** * Constructor from the encoding of an ASN.1 object. * * @param obj the object to be encoded. */ public DEROctetString( ASN1Encodable obj) throws IOException { super(obj.toASN1Primitive().getEncoded(ASN1Encoding.DER)); }
public DERBitString( ASN1Encodable obj) throws IOException { this.data = obj.toASN1Primitive().getEncoded(ASN1Encoding.DER); this.padBits = 0; }
private byte[] getDEREncoded( ASN1Encodable obj) { try { return obj.toASN1Primitive().getEncoded(ASN1Encoding.DER); } catch (IOException e) { throw new IllegalArgumentException("cannot encode object added to SET"); } }
X509Certificate certificate = ... byte[] encExtensionSubjectKeyIdentifier = certificate.getExtensionValue(Extension.subjectKeyIdentifier.getId()); // Unwrap first 'layer' ASN1Primitive skiPrimitive = JcaX509ExtensionUtils.parseExtensionValue(encExtensionSubjectKeyIdentifier); // Unwrap second 'layer' byte[] keyIdentifier = ASN1OctetString.getInstance(skiPrimitive.getEncoded()).getOctets(); // Use keyIdentifier in e.g. CMS SignerInfo SignerInfoGenerator signerInfoGenerator = jcaSignerInfoGeneratorBuilder.build(sha1Signer, keyIdentifier);
public DEROctetString( ASN1Encodable obj) throws IOException { super(obj.toASN1Primitive().getEncoded(ASN1Encoding.DER)); }
PrivateKey priv = pair.getPrivate(); byte[] privBytes = priv.getEncoded(); PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(privBytes); ASN1Encodable encodable = pkInfo.parsePrivateKey(); ASN1Primitive primitive = encodable.toASN1Primitive(); byte[] privateKeyPKCS1 = primitive.getEncoded();
public PrivateKeyInfo( AlgorithmIdentifier algId, ASN1Encodable privateKey, ASN1Set attributes) throws IOException { this.privKey = new DEROctetString(privateKey.toASN1Primitive().getEncoded(ASN1Encoding.DER)); this.algId = algId; this.attributes = attributes; }
private ByteString pkcs1Bytes() { try { PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.getPrivate().getEncoded()); return ByteString.of(privateKeyInfo.parsePrivateKey().toASN1Primitive().getEncoded()); } catch (IOException e) { throw new AssertionError(e); } }
@SneakyThrows public static String convertPublicKeyFromX509ToPkcs1Pem(final byte[] publicKeyX509) { // Convert public key from X.509 SubjectPublicKeyInfo to PKCS1: final ASN1Primitive publicKeyPrimitive = SubjectPublicKeyInfo.getInstance(publicKeyX509).parsePublicKey(); return pem(publicKeyPrimitive.getEncoded(), RsaKey.KEY_FORMAT_PKCS1, RsaKey.KEY_TYPE_PUBLIC); }
@SneakyThrows public static String convertPrivateKeyFromPkcs8ToPkcs1Pem(final byte[] privateKeyPkcs8) { // Convert private key from PKCS8 to PKCS1: final ASN1Encodable encodable = PrivateKeyInfo.getInstance(privateKeyPkcs8).parsePrivateKey(); return pem(encodable.toASN1Primitive().getEncoded(), RsaKey.KEY_FORMAT_PKCS1, RsaKey.KEY_TYPE_PRIVATE); }
public DLBitString( ASN1Encodable obj) throws IOException { super(obj.toASN1Primitive().getEncoded(ASN1Encoding.DER), 0); }
private void verifySig(ASN1Encodable store, SignatureCheck integrityCheck, PublicKey key) throws GeneralSecurityException, IOException { Signature sig = helper.createSignature(integrityCheck.getSignatureAlgorithm().getAlgorithm().getId()); sig.initVerify(key); sig.update(store.toASN1Primitive().getEncoded(ASN1Encoding.DER)); if (!sig.verify(integrityCheck.getSignature().getOctets())) { throw new IOException("BCFKS KeyStore corrupted: signature calculation failed"); } }
public DERBitString( ASN1Encodable obj) throws IOException { super(obj.toASN1Primitive().getEncoded(ASN1Encoding.DER), 0); }
@Override protected AsymmetricKeyParameter decodeASN1(final byte[] encoded) { try { return PrivateKeyFactory.createKey(new ASN1InputStream(encoded).readObject().getEncoded()); } catch (IOException e) { throw new EncodingException("ASN.1 decoding error", e); } } }
private PdfName getSignatureHashKey(String signatureName) throws NoSuchAlgorithmException, IOException { PdfSignature sig = sgnUtil.getSignature(signatureName); PdfString contents = sig.getContents(); byte[] bc = PdfEncodings.convertToBytes(contents.getValue(), null); byte[] bt = null; if (PdfName.ETSI_RFC3161.equals(sig.getSubFilter())) { ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(bc)); ASN1Primitive pkcs = din.readObject(); bc = pkcs.getEncoded(); } bt = hashBytesSha1(bc); return new PdfName(convertToHex(bt)); }