title: Sending IV with cipher text
body: <p>I am encrypting my message with a constant secret key and random IV using <code>AES/CTR/NoPadding</code> in Java.</p>
  <p>I plan to prepend the IV to the cipher text and then <code>Base64</code> encode it.</p>
  <p>Now, there is one problem that is bothering me. There can be multiple <code>IV</code> + Cipher text combinations that can result in my original message. Isn't that a problem? Also, is it safe to send IV as such (i.e. prepend/append to the cipher text) or there is some procedure that I should follow?</p>
  <p>I am relatively new to cryptography, so pardon me if it's very simple question. I couldn't find any satisfying answer to this.</p>
  <h3>EDIT:</h3>
  <pre><code>public static String encrypt(String message) {
      try {
          Cipher cipher = Cipher.getInstance(&quot;AES/CTR/NoPadding&quot;);
          byte[] iv = generateRandomIV();
          cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, new IvParameterSpec(iv));
  
          byte[] cipherText = cipher.doFinal(message.getBytes(&quot;utf-8&quot;));
          return DatatypeConverter.printBase64Binary(concat(iv, cipherText));
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      return null;
  }
  
  public static String decrypt(String encryptedMessage) {
      try {
          byte[] bytes = DatatypeConverter.parseBase64Binary(encryptedMessage);
          byte[] iv = getIV(bytes);
          byte[] cipherText = getCipherText(bytes);
  
          Cipher cipher = Cipher.getInstance(&quot;AES/CTR/NoPadding&quot;);
          cipher.init(Cipher.DECRYPT_MODE, SECRET_KEY, new IvParameterSpec(iv));
  
          return new String(cipher.doFinal(cipherText));
      } catch (Exception ex) {
          ex.printStackTrace();
          return encryptedMessage;
      }
  }
  
  private static byte[] getIV(byte[] bytes) {
      return Arrays.copyOfRange(bytes, 0, 16);
  }
  
  private static byte[] getCipherText(byte[] bytes) {
      return Arrays.copyOfRange(bytes, 16, bytes.length);
  }
  </code></pre>
  <p>and then</p>
  <pre><code>public static void main(String[] args) {
      System.out.println(decrypt(&quot;wVroKV1UnL2NXiImS83hLKpLLJKk&quot;));
      System.out.println(decrypt(&quot;Q0tWAMZDhqMo0LbtEY7lF9D8Dkor&quot;));
  }
  </code></pre>
  <p>Both these produce same output -- &quot;goody&quot;</p>