title: AES-CTR Encrypt in Go and decrypt in CryptoJS
body: <p>I have a problem decrypting text, which is encrypted in Go lang, with CryptoJS.</p>
  
  <p>Here is Go code:
  <a href="https://play.golang.org/p/xCbl48T_iN" rel="nofollow">https://play.golang.org/p/xCbl48T_iN</a></p>
  
  <pre><code>package main
  
  import (
      "crypto/aes"
      "crypto/cipher"
      "encoding/base64"
      "fmt"
  )
  
  func main() {
      key := []byte("1234567890123456")
      plaintext := []byte("text can be a random lenght")
  
      block, err := aes.NewCipher(key)
      if err != nil {
          panic(err)
      }
  
      // The IV needs to be unique, but not secure. Therefore it's common to
      // include it at the beginning of the ciphertext.
      // BTW (only for test purpose) I don't include it
  
      ciphertext := make([]byte, len(plaintext))
  
      iv := []byte{'\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f'}
  
      stream := cipher.NewCTR(block, iv)
      stream.XORKeyStream(ciphertext, plaintext) 
  
      // CTR mode is the same for both encryption and decryption, so we can
      // also decrypt that ciphertext with NewCTR.
          base := base64.StdEncoding.EncodeToString(ciphertext)
      fmt.Printf("encodedHEX: %x\n", ciphertext)
      fmt.Printf("encodedBASE: %s\n", base)
  
      plaintext2 := make([]byte, len(plaintext))
      stream = cipher.NewCTR(block, iv)
      stream.XORKeyStream(plaintext2, ciphertext)
  
      fmt.Printf("decoded: %s\n", plaintext2)
  }
  </code></pre>
  
  <p>Here is JS code:
  <a href="http://jsfiddle.net/Ltkxm64n/" rel="nofollow">http://jsfiddle.net/Ltkxm64n/</a></p>
  
  <pre><code>var key = CryptoJS.enc.Hex.parse('31323334353637383930313233343536');
  var iv = CryptoJS.enc.Hex.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
  var encrypted = CryptoJS.AES.encrypt("text can be a random lenght", key, {
    mode: CryptoJS.mode.CTR,
    iv: iv
  });
  
  console.log(encrypted.ciphertext.toString());
  console.log(encrypted.toString());
  
  var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
    mode: CryptoJS.mode.CTR,
    iv: iv
  });
  console.log(decrypted.toString(CryptoJS.enc.Utf8)); 
  // text can be a random lenght
  </code></pre>
  
  <p>Both works well encrypting and decrypting, but when I copy the base64 ciphertext from GO to JS (or viceversa), it doesn't work.
  I also noticed that first part of js output is the same of Go output, but in js output there are more bytes than in Go one.</p>
  
  <p>My purpose is to encrypt some text in GO, then ship Base64 ciphertext to JS that can decrypt it.</p>
  
  <p>Thank you</p>