title: How do I decrypt an AES256 bit cipher in golang that was encrypted in nodejs?
body: <p>I encrypted a string in Node.js like this. </p>
  
  <pre><code>var cipher = crypto.createCipheriv(
  "aes256",
  "&lt;A Buffer of length 32&gt;",
  "79b67e539e7fcaefa7abf167de5c06ed"  
  );
  </code></pre>
  
  <p>I noticed that a buffer in nodejs is like hex but every 2 consecutive characters are paired. So, It's length is half of whatever will come out if I convert it to a hex. </p>
  
  <p>Example: </p>
  
  <p>Buffer:</p>
  
  <pre><code>&lt;Buffer c3 80 36 f6 51 57 cb 6d b0 e8 fd 85 5a a2 8a da 07 4b e7 19 17 d1 c8 ee dc 2a e4 d8 5e 3c 9d a6&gt;
  </code></pre>
  
  <p>Hex:</p>
  
  <pre><code>c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6
  </code></pre>
  
  <p>Now, The key I use in aes256 can not be of length 64. Here, Buffer's length is 32 and hex's length is 64. </p>
  
  <p>I want to decrypt this cipher in golang and I'll have to use this key and iv to decrypt it. </p>
  
  <p>aes in golang takes a length depending upon the size of key and when it sees a key of length 64 it throws an error that says, <code>Invalid key length</code>.</p>
  
  <p>How do I decrypt it in golang? There is my current program in go: <a href="https://play.golang.org/p/SoXOz3XIPK" rel="nofollow noreferrer">https://play.golang.org/p/SoXOz3XIPK</a></p>
  
  <pre><code>package main
  
  import (
      "crypto/aes"
      "crypto/cipher"
      "fmt"
      "log"
  )
  
  func main() {
  
      encKey := "c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6"
      iv := "79b67e539e7fcaefa7abf167de5c06ed"
      cipherText := "c02eccfc514a0b7fae830586dd56e0fcebb81fc49f41fa6dedf099c3645793bef7ec7075eca30063f9c0ef395d5ee2d44e4f3490114280abb7cf86d6eb525e2ec9bd2b781388986480f8b3df95f7b10e"
  
      block, err := aes.NewCipher([]byte(encKey))
      if err != nil {
          log.Fatalf("%s", err)
      }
  
      decrypter := cipher.NewCFBDecrypter(block, []byte(iv))
  
      decrypted := make([]byte, 1000)
      decrypter.XORKeyStream(decrypted, []byte(cipherText))
  
      fmt.Printf("%s\n", string(decrypted))
  
  }
  </code></pre>