使用MCRYPT_RIJNDAEL_256使用Go加密在php中加密的字符串进行解密

How*_*owl 9 php encryption aes go

在PHP中,mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);返回值32,因此显然说AES-256需要32字节的初始化向量.但是,这是欺骗,在上述征求意见mcrypt_encrypt:

此外,MCRYPT_RIJNDAEL_256它不是AES-256,它是Rijndael分组密码的不同变体.如果您想在mcrypt中使用AES-256,则必须使用MCRYPT_RIJNDAEL_12832字节密钥.OpenSSL使您更明显地使用哪种模式(即'aes-128-cbc'vs'aes-256-ctr').

当然,对于32字节的IV,以下示例在Go中不起作用(它引起恐慌).

score := decodePost(c.PostForm("score"))
iv := decodePost(c.PostForm("iv"))

aesKey := getAESKey()
baseAES, err := aes.NewCipher([]byte(aesKey))
if err != nil {
    c.AbortWithError(500, err)
    return
}
block := cipher.NewCBCDecrypter(baseAES, []byte(iv))
block.CryptBlocks(score, score)
Run Code Online (Sandbox Code Playgroud)

引用文档crypto/cipher:

iv的长度必须与Block的块大小相同,并且必须与用于加密数据的iv匹配.

(当然,Go中AES块大小为16字节).

那么,最后,我如何在Go中解密这样的字符串?

小智 2

由于在 Golang 中只有内置标准 AES 加密,因此您需要自己实现 Rijndael 加密。

我在这里找到了一个: https: //github.com/celso-wo/rijndael256/blob/master/rijndael256.go