title: AES-CTR double encryption reverses the ciphertext to plaintext
body: <p>When I try to encrypt the ciphertext again with the same key, it produces the original plaintext.</p>
  
  <p>Algoritm used is <strong>AES</strong> with <strong>COUNTER MODE</strong>. <strong>Key</strong> and <strong>IV</strong> remains the same.</p>
  
  <p>Is this the way the algorithm is supposed to behave? And if, what is the use of <strong>Cipher.ENCRYTMODE</strong> which is to be given as the first parameter of Cipher.init()?</p>
  
  <p>Here is the sample program with which I tested,</p>
  
  <pre><code>import javax.crypto.*;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  public class EncryptionTest {
  
      public static void main(String[] args) throws Exception {
          SecretKeySpec key = null;
          IvParameterSpec ivSpec = null;
          byte[] keyBytes = "usethiskeyusethiusethiskeyusethi".getBytes();
          byte[] ivBytes = "usethisIusethisI".getBytes();
          key = new SecretKeySpec(keyBytes, "AES"); //No I18N
          ivSpec = new IvParameterSpec(ivBytes);
  
          Cipher AesCipher = Cipher.getInstance("AES/CTR/NoPadding");
  
  
          byte[] byteText = "Your Plain Text Here".getBytes();
  
          AesCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
          byte[] byteCipherText = AesCipher.doFinal(byteText);
          System.out.println("Encrypted : " + new String(byteCipherText));
  
          AesCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
          byte[] bytePlainText = AesCipher.doFinal(byteCipherText);
          System.out.println("Double Encrypted : " + new String(bytePlainText));
      }
  }
  </code></pre>