title: android cipher dofinal array too large
body: <p>I am writing an Android version of a Java program that I use to keep track of passwords.</p>
  
  <p>I have the following function that I use to decode one line of the encrypted data:</p>
  
  <pre><code>public String decrypt(String text) {
      byte[] decryptedText = null;
      String rtn = "";
      String[] byteValues = text.substring(1, text.length() - 1).split(",");
      byte[] bytes = new byte[byteValues.length];
      for (int i=0, len=bytes.length; i&lt;len; i++) {
          bytes[i] = Byte.valueOf(byteValues[i].trim());     
      }
      try {
          Cipher cipher = Cipher.getInstance(ALGORITHM);
          cipher.init(Cipher.DECRYPT_MODE, privateKey);
          decryptedText = cipher.doFinal(bytes);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      if (decryptedText != null) {
          try {
              rtn = new String(decryptedText, "UTF-8");
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
      }
  
      return rtn;
  }
  </code></pre>
  
  <p>ALGORITHM is set to:</p>
  
  <pre><code>public static final String ALGORITHM = "RSA";
  </code></pre>
  
  <p>The input text looks something like this (there are 128 values):</p>
  
  <pre><code>[12, -27, -31, 2, -65, -119, -104, -9, -42, 6, -32, -116, 35, 14, -30, -51, 117, 21, -33, -98, 21, 41, -11, 118, 85, -46, -67, -19, 47, -10, -42, -18, -116, 87, 49, 76, 25, 114, 88, -100, 41, 74, -70, 73, -103, 78, 117, 123, 127, -4, 98, -6, -124, -69, -112, -51, 27, 46, -83, 5, -84, 22, -64, -92, 15, -85, 34, -44, -50, 28, -69, 28, 106, -75, -7, 63, 112, 22, 112, -85, -28, 29, 97, -51, 112, -52, -2, 60, -55, -57, 41, 78, -90, -55, -1, -128, 12, 10, 73, -95, -105, 38, -125, 123, 89, 35, 63, 100, 124, 64, -4, 11, -50, -100, -81, -66, -64, 94, -93, -72, 67, 3, 22, -126, -125, 24, 127, -74]
  </code></pre>
  
  <p>When I run this function as part of a Java program (both Jave 1.6 &amp; 1.7), I get back the original string as I expect. But, when I run this as part of my Android application the decryptedText array, instead of having a length of about 30, always has a length of 127. The resulting return string has all sorts of garbage characters in the front, but the last part of the string is the correctly decoded text.</p>
  
  <p>Should I be calling the <code>doFinal</code> differently on Android than in Java?</p>