简体   繁体   中英

Nodejs / Golang aes 256 decryption

I have an encrypted string that I was able to decrypt in nodejs using the following code

let decrypt = crypto.createDecipheriv('aes-256-cbc-hmac-sha1', derived, iv);
decrypt.setAutoPadding(false);
let decrypted = decrypt.update(new_buf, 'binary', 'utf8') + decrypt.final('utf8');

I have to be able to decrypt the same buffer in a go program and I wrote this

fmt.Printf("buf size=%d\n", len(derivedKey))
block, err := aes.NewCipher(derivedKey)
if err != nil {
    fmt.Printf("%v\n", err)
    return ("0")
}
fmt.Printf("block size=%d\n", block.BlockSize())
cbc := cipher.NewCBCDecrypter(block, ivKey)
fmt.Printf("cbc block size=%d\n", cbc.BlockSize())
decr := make([]byte, len(encr))
cbc.CryptBlocks(decr, []byte(encr))

This isn't working (I suppose I won't be posting here otherwise :p) but I also noticed that when I print the length of the derived key I get 32 but when I read the block.BlockSize() or the cbc.BlockSize() I get 16 as an answer so I'm wondering if I don't have to setup something because it is detecting my algorithm as aes128 instead of aes256.

Ok this is working well actually, the blocksize show isn't a problem, internally it creates the correct block for the aes256, my problem was because of the parameters sent to me that added padding on the "encr" variable. But the code on top works perfectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM