/** * Decodes a PEM-encoded cryptographic object into the raw bytes of its ASN.1 encoding. Header/footer data and * metadata info, e.g. Proc-Type, are ignored. * * @param pem Bytes of PEM-encoded data to decode. * * @return ASN.1 encoded bytes. */ public static byte[] decode(final byte[] pem) { return decode(new String(pem, ByteUtil.ASCII_CHARSET)); }
@Override public PublicKey newInstance() throws EncodingException { return KeyPairUtil.decodePublicKey(PemUtil.decode(encodedKey)); } }
@Override public PrivateKey newInstance() throws EncodingException { return KeyPairUtil.decodePrivateKey(PemUtil.decode(encodedKey)); } }
/** * Tests the given encoded input and converts it to PEM if it is detected, stripping out any header/footer data in the * process. * * @param input Encoded data that may be PEM encoded. * * @return Decoded data if PEM encoding detected, otherwise original data. */ protected byte[] tryConvertPem(final byte[] input) { if (PemUtil.isPem(input)) { return PemUtil.decode(input); } return input; }
@Override protected byte[] decryptKey(final byte[] encrypted, final char[] password) { final String pem = new String(encrypted, ByteUtil.ASCII_CHARSET); final int start = pem.indexOf(PemUtil.DEK_INFO); final int eol = pem.indexOf('\n', start); final String[] dekInfo = pem.substring(start + 10, eol).split(","); final String alg = dekInfo[0]; final byte[] iv = CodecUtil.hex(dekInfo[1]); final byte[] bytes = PemUtil.decode(encrypted); return new OpenSSLEncryptionScheme(OpenSSLAlgorithm.fromAlgorithmId(alg), iv, password).decrypt(bytes); }
@Override public AsymmetricKeyParameter decode(final byte[] encoded, final Object... args) { try { if (PemUtil.isPem(encoded)) { return PublicKeyFactory.createKey(PemUtil.decode(encoded)); } return PublicKeyFactory.createKey(new ASN1InputStream(encoded).readObject().getEncoded()); } catch (IOException e) { throw new EncodingException("ASN.1 decoding error", e); } } }