I have difficulties to decrypt payload with AES-256-CTR in Golang
Format:
A zero IV.
Not padded.
Input:
func main() {
encryptedKey := []byte{196, 231, 38, 149, 234, 51, 142, 186, 230, 214, 96, 243, 229, 153, 103, 74, 117, 241, 237, 135, 91, 170, 216, 167, 235, 154, 180, 28, 125, 48, 82, 44}
encryptedData := "bj4hUUJtwIZJnnGYO+04JpIxf1Omnsrq+ilLv+eQXSEbvsEKgD9BZ0cnxGBrvd4KHeyHkoiZZbObDkZemFtQkgo0jw1AtscTv4HSIz9OeOFZmNjjA724dA8oyuHY8WWlFddXcArqv4R5a9DO2Qs4e9sk3KIRYn4OJH//lNi8t111J//wcsSTvuer+EO9XeQIZHsZ4sr+fhdU1Jxz7Z1KOM6c2MGuXAqfKj/ebn01XA/LgnaDO8xZ+k7vs19WY0pH33HM0K5K1C+XUVaRGhUtFt2BDgnG5T/MPIyVlfUpJpHJjEdsvTxTN7muK9UsbSonr3XAp9oV/w7xx65N5iEEjvC1CaehAnKOSM6QiyKs8KLiv2vHaabsNNusFZgHUZYUApErbpK9g3UR0744Sy8hVe/l75SmOofZvu59vU509MMc8TdqxssIv2g7JTw="
plaintext, err := Decode(encryptedKey, encryptedData)
if err != nil {
fmt.Errorf("an error occured while decrypting: %s", err.Error())
}
fmt.Printf("in hex format: %x", plaintext)
}
func Decode(encryptionKey []byte, encryptedData string) ([]byte, error) {
ciphertext, err := base64.StdEncoding.DecodeString(encryptedData)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return nil, err
}
iv := ciphertext[:aes.BlockSize]
stream := cipher.NewCTR(block, iv)
plaintext := make([]byte, len(ciphertext)-aes.BlockSize)
stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:])
return plaintext, nil
}
Output:
15affcc3abb646bbf776136d0dc4a37c1e70c3433f9df8a9f7434b74992c25316d78405deffdbf964bb7e9229f9185b85bcf253bb3dcddb782872e018a1aed84414aaf5bc76af8d8accaf6137758d72322d09a7f56a9b57afc22a1b36cf84f24dc558b6654c69875cfb3b753c765794d22765b6f47f7a5c2b8a1822cbae4e2b66e8d413cdb5631a5a39fc435681bc139d01b06034faca8d7cc1677a4209f7e4fe9b746d7b530e54604a0cd2af2475837083823381626d9e3bfcaa6e531d9125ad851deb34c2d42363e3de5e3ac93bf66b3b2e506c57f1068c41c70a9dfe0d4d131f45ed6d2514a1ec273850c7a51982d31d0e68a785b5eceff141be88512b7eed66bb20d510bdc9ec12095c8e445699325c2cee9e52dec8e8e05359330bc946e6e5e5279acd84369394867d37b2002205f5883f4a281c9123ef6d1ab
Expected output:
7b226d65737361676545787069726174696f6e223a2231363434383331333036303037222c226d6573736167654964223a22414832456a74635157674c4f31364b415a702d316f6e5f353954386a58736f356135305064426f555f39664e737a396e763533376c6c52674e70345a4e4a5536754177714d7845646e50625a6c69753656666568665150366d514534326448544b4b5f6f6f765a74316a30796c6a7a424778424743326f7333655f33654b6f503463745a4b65694139767637222c227061796d656e744d6574686f64223a2243415244222c227061796d656e744d6574686f6444657461696c73223a7b2265787069726174696f6e59656172223a323032372c2265787069726174696f6e4d6f6e7468223a31322c2270616e223a2234313131313131313131313131313131222c22617574684d6574686f64223a2250414e5f4f4e4c59227d7d
Sample sandbox
Since your IV is 0, you need to set iv
and plaintext
as:
iv := make([]byte, aes.BlockSize)
and
plaintext := make([]byte, len(ciphertext))
which yields the expected output.