title: Why does JNCryptor/RNCryptor library throws a InvalidHMACException?
body: <p>I want to use AES256 symmetric encryption my iPhone app and my Java server socket. I am currently using Rob Napier's RNCryptor/JNCryptor library. Encryption on the iPhone seems to work well, as I am able to decrypt the encrypted strings back again. But as soon as I try to decrypt a string on my Java server socket, the following exception is thrown:</p>
  
  <pre><code>com.acme.crypto.InvalidHMACException: Incorrect HMAC value.
      at com.acme.crypto.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:248)
      at com.acme.crypto.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:323)
      at com.acme.crypto.AES256JNCryptor.decryptData(AES256JNCryptor.java:280)
  
  com.acme.crypto.CryptorException: Unrecognised version number: 61.
      at com.acme.crypto.AES256JNCryptor.decryptData(AES256JNCryptor.java:283)
  </code></pre>
  
  <p>Here is the relevant client code snippet for sending the encrypted data (iOS/Objective-C):</p>
  
  <pre><code> // add line break and send message to server
   NSString* message = [NSString stringWithFormat:@"%@\n", output];
   NSData* data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSUTF8StringEncoding 
                                                     allowLossyConversion:NO]];
   // encrypt outgoing data with AES-256
   NSError *error1;
   NSData *cypher = [RNEncryptor encryptData:data
                                withSettings:kRNCryptorAES256Settings
                                    password:@"mypassword"
                                       error:&amp;error1];
   // write encrypted data to output stream
   if (error1==nil) {
       NSInteger result = [outputStream write:[cypher bytes] maxLength:[cypher length]];
   } else {
       NSLog(@"Encryption of outgoing data failed: %@", error1);
   }
  </code></pre>
  
  <p>And here's is the corresponding server code that receives the encrypted data on its socket (Linux/Java):</p>
  
  <pre><code>// initializing cryptor object during object construction
  JNCryptor cryptor = new AES256JNCryptor();
  
  // setting up the input stream on the server socket
  BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
  
  // this is within the client thread ...
  
  String line;
  while((line=input.readLine())!=null) {
      try {
          // ... the exception is thrown at the following line  ...
          byte[] decrypted = cryptor.decryptData(line.getBytes(), password.toCharArray());
          line = new String(decrypted, StandardCharsets.UTF_8);
          // message handling ...
      } catch (Exception ex) {
          // print exception ...
      }
   }
  </code></pre>
  
  <p>Somebody any idea what I am doing wrong? Do I have to use Base64 or similiar for encoding before sending the data? Any help highly appreciated.</p>
  
  <p><strong>EDIT: Here's the solution. Instead of using a character based input-stream, I now use the InputStream from the socket directly in order to read the raw bytes to feed the decryption algorithm:</strong></p>
  
  <pre><code>@Override
  public void run() {
      try {
          int bytes;
          byte[] buffer = new byte[4096];
          while((bytes=input.read(buffer))!=-1) {
              try {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  baos.write(buffer, 0, bytes);
                  byte[] decrypted = cryptor.decryptData(baos.toByteArray(), password.toCharArray());
                  String line = new String(decrypted, StandardCharsets.UTF_8);
                  // handle input ...
              } catch (Exception ex) {
                  // handle exception ...
              }
          }
      } catch (Exception ex) {
          // handle exception ...
      }
  }
  </code></pre>