package console; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import org.springframework.util.Base64Utils; import org.springframework.util.StreamUtils; import iaik.asn1.ASN1; import iaik.asn1.CodingException; public class PdfSigDecoder { /** * Decode a CAdES signature that is located in a PDF document. * * @param args not supported yes * @throws CodingException In case of an ASN.1 decoding error * @throws IOException In case of a general error * @throws UnsupportedEncodingException In case of a general error */ public static void main(String[] args) throws CodingException, UnsupportedEncodingException, IOException { InputStream is = PdfSigDecoder.class.getResourceAsStream("/pdf_cades_4.hex"); String test = new String(StreamUtils.copyToByteArray(is), "UTF-8"); test = test.replaceAll("\\n", ""); final byte[] bytes = new byte[test.length() / 2]; for (int i = 0; i < test.length() / 2; i++) { bytes[i] = (byte) Integer.parseInt(test.substring(i * 2, i * 2 + 2), 16); } Base64Utils.encodeToString(bytes); final ASN1 asn1 = new ASN1(bytes); System.out.println(asn1.toString()); System.out.println(Base64Utils.encodeToString(bytes)); } }