Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Last active December 23, 2023 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montanaflynn/d77caeb63dca385634ad9de99658c744 to your computer and use it in GitHub Desktop.
Save montanaflynn/d77caeb63dca385634ad9de99658c744 to your computer and use it in GitHub Desktop.
Decrypt aes-256-ctr in Golang encrypted with node.js
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
"strings"
)
func main() {
passphrase := "password"
encKey := strings.ReplaceAll(fmt.Sprintf("%-64x\n", passphrase), " ", "0")[:64]
cipherText := "3158cdb3ef572582be83efcf68bafbb4bacf8b3cc54f70"
encKeyDecoded, err := hex.DecodeString(encKey)
if err != nil {
panic(err)
}
cipherTextDecoded, err := hex.DecodeString(cipherText)
if err != nil {
panic(err)
}
block, err := aes.NewCipher([]byte(encKeyDecoded))
if err != nil {
panic(err)
}
iv := cipherTextDecoded[:aes.BlockSize]
cipherTextBytes := []byte(cipherTextDecoded)
plaintext := make([]byte, len(cipherTextBytes)-aes.BlockSize)
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, cipherTextBytes[aes.BlockSize:])
fmt.Println(string(plaintext))
}
@aprilcoskun
Copy link

Do you know how to encrypt same way node.js does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment