title: PKCS7 validation error: ASN1 corrupted data
body: <p>I am developing a java class that signs text files in PKCS7 format. I discovered that it is possible to do this using Sun libraries instead of BouncyCastle, and I developed the following method:</p>
  
  <pre><code>    public void AssinaPKCS7(String ArquivoAssinar) {
  
          String SrtResultPKCS7 = "";
          byte[] Conteudo;
          byte[] Hash;
          String DadosArq = "";
          String Linha = "";
          boolean AssinValid = false;
  
          try {
              FileInputStream Entrada = new FileInputStream(ArquivoAssinar);
              InputStreamReader Leitor = new InputStreamReader(Entrada, Charset);
              BufferedReader Buffer = new BufferedReader(Leitor);
              Linha = Buffer.readLine();
  
              while (Linha != null) {
                  DadosArq = DadosArq + Linha;
                  Linha = Buffer.readLine();
              }
  
              Buffer.close();
  
              Conteudo = DadosArq.getBytes(Charset);
  
              X500Name xName = X500Name.asX500Name(Certif.getSubjectX500Principal());
              BigInteger serial = Certif.getSerialNumber();
              AlgorithmId digestAlgorithmId = new AlgorithmId(AlgorithmId.SHA_oid);
              AlgorithmId signAlgorithmId = new AlgorithmId(AlgorithmId.RSAEncryption_oid);
  
              MessageDigest MessDig = MessageDigest.getInstance("SHA1");
              Hash = MessDig.digest(Conteudo);
  
              PKCS9Attribute Atributo1 = new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID);
              PKCS9Attribute Atributo2 = new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, Hash); 
              PKCS9Attributes ConjuntoAtrib = new PKCS9Attributes(new PKCS9Attribute[] {Atributo1, Atributo2}); 
  
              Signature Sign = Signature.getInstance(AlgoritmoAssinatura);
              Sign.initSign(PrivPass);
              Sign.update(Conteudo);
              ResultadoAssinatura = Sign.sign();
  
              Signature Verif = Signature.getInstance(AlgoritmoAssinatura);
              Verif.initVerify(Certif);
              Verif.update(Conteudo);
              AssinValid = Verif.verify(ResultadoAssinatura);
  
              if (AssinValid) {
                  try {
                      SignerInfo sInfo = new SignerInfo(xName, serial, digestAlgorithmId, ConjuntoAtrib, signAlgorithmId, ResultadoAssinatura, null);
  
                      ContentInfo cInfo = new ContentInfo(ContentInfo.DIGESTED_DATA_OID, new DerValue(DerValue.tag_OctetString, Conteudo));
  
                      PKCS7 p7 = new PKCS7(new AlgorithmId[] { digestAlgorithmId }, cInfo, new java.security.cert.X509Certificate[] { Certif }, new SignerInfo[] { sInfo });
  
                      ByteArrayOutputStream bOut = new DerOutputStream();
                      p7.encodeSignedData(bOut);
                      byte[] encoded = bOut.toByteArray();
                      SrtResultPKCS7 = Encoder.encode(encoded);
  
                      FileOutputStream Saida = new FileOutputStream(ArquivoAssinar);
                      OutputStreamWriter Escritor = new OutputStreamWriter(Saida, Charset);
                      BufferedWriter BuffWriter = new BufferedWriter(Escritor); 
                      //BuffWriter.write(SrtResultPKCS7);
                      BuffWriter.write(bOut.toString());
                      BuffWriter.close();
                  }
                  catch (Exception E) {
                      E.printStackTrace();        
                  }
              }
              else {
                  System.out.println("Assinatura inválida");
              }
          }
          catch (Exception E) {
              E.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p>When I tryed to validate the resulting file in online validator <a href="https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATBHE/assinadoc/ValidadorAssinaturas.app/valida.aspx" rel="nofollow noreferrer">https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATBHE/assinadoc/ValidadorAssinaturas.app/valida.aspx</a> I got this error message:</p>
  
  <p><strong>ASN1 corrupted data</strong></p>
  
  <p>I checked the signature, she is OK. I believe the error is in som data convertion while generating PKCS7 result.</p>
  
  <p>Has anyone had this problem? And how can I validate the PCKS7 result in java code? </p>