dpd2349 2017-08-16 11:33
浏览 309

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

In PHP, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); returns a value of 32, thus apparently saying that AES-256 wants an Initialization Vector of 32 bytes. But this is deceiving, as said in the comments for mcrypt_encrypt:

Also, MCRYPT_RIJNDAEL_256 is not AES-256, it's a different variant of the Rijndael block cipher. If you want AES-256 in mcrypt, you have to use MCRYPT_RIJNDAEL_128 with a 32-byte key. OpenSSL makes it more obvious which mode you are using (i.e. 'aes-128-cbc' vs 'aes-256-ctr').

So of course, with an IV of 32 bytes, the following example does not work in Go (it causes a panic).

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)

Quoting from the docs of crypto/cipher:

The length of iv must be the same as the Block's block size and must match the iv used to encrypt the data.

(And of course, the AES block size in Go is 16 bytes).

So, finally, how can I decrypt such string in Go?

  • 写回答

1条回答 默认 最新

  • doremifasodo0008008 2017-12-19 14:01
    关注

    Since in Golang you have only built-in standard AES crypto, you need to implement the Rijndael crypto yourself.

    I found one here: https://github.com/celso-wo/rijndael256/blob/master/rijndael256.go

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘