We have a encryption mechanism in Go. Input will be like key= "dHRzbGNvbnNlbnR0ZWNobQ==" and text = "1234565434".
func encrypt(key []byte, text string) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
msg := AddPadding([]byte(text))
ciphertext := make([]byte, aes.BlockSize+len(msg))
iv := ciphertext[:aes.BlockSize]
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(msg))
finalMsg := removeBase64Padding(base64.URLEncoding.EncodeToString(ciphertext))
return finalMsg, nil
}
func AddPadding(src []byte) []byte {
padding := aes.BlockSize - len(src)%aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
Output is : "AAAAAAAAAAAAAAAAAAAAAEl8eI9S6j7mZTWG0vdwV1A="
I want to replicate the same in NodeJS.
let iv = 'AAAAAAAAAAAAAAAA';
let key = "dHRzbGNvbnNlbnR0ZWNobQ==";
var cipher = crypto.createCipheriv('aes-256-cbc', keyBytes, iv);
cipher.update(src, 'binary', 'base64');
let y = cipher.final('base64');
console.log("y --->> ",y);
But I'm getting error like
crypto.js:194
this._handle.initiv(cipher, toBuf(key), toBuf(iv));
^
Error: Invalid key length
Can anyone please suggest me how to do this?