Node.js AES decryption code converted to Golang throws key size error

45 Views Asked by At

I have a nodesJS code which is used for decrypting and works fine. I was trying to convert the same in to Golang it always throws "panic: crypto/aes: invalid key size 7" error, which works perfectly when executed in NodeJs

NodeJs code :

const AesEncryption = require('aes-encryption')
var Buffer = require('buffer/').Buffer;
var CryptoJS = require("crypto-js");
const test = ""
const decodedCiphertext = Buffer.from(test, 'base64').toString('utf-8');
const decryptedData = CryptoJS.AES.decrypt(decodedCiphertext, 'D4jo0Ky')
const decryptedText = decryptedData.toString(CryptoJS.enc.Utf8);
console.log(decryptedText);

The will print the decrypted text correctly, but when convert these to go and run, its failing

GoLang code :

func main() {
encodedTest := "" // Replace with your test string
decodedCiphertext, _ := base64.StdEncoding.DecodeString(encodedTest)

key := []byte("s0k00py") // Replace with your AES key

block, err := aes.NewCipher(key)
if err != nil {
    panic(err.Error())
}

if len(decodedCiphertext) < aes.BlockSize {
    panic("Ciphertext block size is too short")
}

iv := decodedCiphertext[:aes.BlockSize]
decodedCiphertext = decodedCiphertext[aes.BlockSize:]

stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(decodedCiphertext, decodedCiphertext)

decryptedText := string(decodedCiphertext)
fmt.Println(decryptedText)
}

These one errors out with "panic: crypto/aes: invalid key size 7" , i tried to increase the key size to 16 byte using sha256.Sum256([]byte(inputString)), which prints garbage value in response.

Any help is much appreciated, thank you!

0

There are 0 best solutions below