title: Read/Write Certificate Signing Request on Android (spongycastle)
body: <p>I try to write code on Android to generate RSA key-pair and then generate Certificate Request file (.csr) like this image below:</p>
  
  <p><a href="https://i.stack.imgur.com/xDdYE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xDdYE.jpg" alt="enter image description here"></a></p>
  
  <ol>
  <li><p>First I use spongycastle lib to generate Key Pair (public key and private key)</p>
  
  <pre><code>KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  keyPairGenerator.initialize(1024,new SecureRandom());
  KeyPair keyPair = keyPairGenerator.generateKeyPair();
  publicKey = keyPair.getPublic();
  privateKey = keyPair.getPrivate();
  </code></pre></li>
  <li><p>Then I use <code>CSRHelper</code> class I found on this <a href="https://stackoverflow.com/questions/37850134/certificate-enrollment-process/37898553#comment77283917_37898553">link</a> to generate:  <code>byte CSRder[]</code>:</p>
  
  <pre><code>byte CSRder[] = csr.getEncoded();
  </code></pre></li>
  <li><p>I write code to write byte[] to file:</p>
  
  <pre><code>File file;
  FileOutputStream outputStream;
  try
  {
      file = new File(getCacheDir(),"csr.txt");
      outputStream = new FileOutputStream(file);
      outputStream.write(CSRder);
      outputStream.close();
  }
  catch (IOException e)
  {
      e.printStackTrace();
  }
  </code></pre></li>
  <li><p>Final I write code to read file again:</p>
  
  <pre><code>BufferedReader input = null;
  File file = null;
  try {
     file = new File(getCacheDir(), "csr.txt"); // Pass getFilesDir() and "MyFile" to read file
  
      input = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
      String line;
      StringBuffer buffer = new StringBuffer();
      while ((line = input.readLine()) != null) {
            buffer.append(line);
      }
      Log.d(TAG, buffer+"");
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre></li>
  </ol>
  
  <p>But my logcat show unreadable character. </p>
  
  <pre><code>    07-21 13:48:35.163 16157-16157/com.example.napoleon.test_2 D/MainActivity: 0��0���0;10Unapoleon.com10UAralink10UOrgUnit0��0 *�H��������0�������Qt��G�]�ܪ�0�'�I^�Q��[�r5ڢ_!|������ZC��~&lt;��*o�?�d+-����)��V�&lt;߹��m��(��ѐxDcx��NhƬF��Ҵvq+���0�Iq�-Eoe,���"0     *�H��   100U�0�0    *�H���������YPT3��?��P5MY��hs)��$1Gv�r_��76ߞ;���ҽ�޼ t�kI�I��Z��tg����O�W��Gt�=���V���#G1�$z�$�V����_^7_x�?�0�׵#�;��f?�
  </code></pre>
  
  <p>How to write this csr byte to file and read again?</p>