dongxin1999 2017-01-19 00:22
浏览 132
已采纳

Go加密与使用相同密钥和iv的Ruby加密不同

I have the following Ruby code:

require 'base64'
require 'openssl'

data = '503666666'

key = '4768c01c4f598828ef80d9982d95f888fb952c5b12189c002123e87f751e3e82'

nonce = '4eFi6Q3PX1478767
'
nonce = Base64.decode64(nonce)

c = OpenSSL::Cipher.new('aes-256-gcm')
c.encrypt
c.key = key
c.iv = nonce

result = c.update(data) + c.final
tag = c.auth_tag

puts Base64.encode64(result + tag) # => J3AVfNG84bz2UuXcfre7LVjSbMpX9XBq6g==

that I'm trying to replicate in Golang. Here's what I have so far:

package main

import (
    "fmt"
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "encoding/hex"
)

func main() {
    data := []byte("503666666")

    key, err := hex.DecodeString(`4768c01c4f598828ef80d9982d95f888fb952c5b12189c002123e87f751e3e82`)
    if err != nil {
        panic(err)
    }

    nonceB64 := "4eFi6Q3PX1478767
"
    nonce, err := base64.StdEncoding.DecodeString(nonceB64)
    if err != nil {
        panic(err)
    }

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

    aesgcm, err := cipher.NewGCM(block)
    if err != nil {
        panic(err.Error())
    }

    ciphertext := aesgcm.Seal(nil, nonce, data, nil)
    fmt.Printf("%s
", base64.StdEncoding.EncodeToString(ciphertext))
}

However the outcome from the Go version is:

+S52HGbLV1xp+GnF0v8VNOqc5J2GY2+SqA==

vs.

J3AVfNG84bz2UuXcfre7LVjSbMpX9XBq6g==

Why am I getting different results?

Thanks,

  • 写回答

1条回答 默认 最新

  • duan0424 2017-01-19 00:31
    关注

    The AES 256 cipher requires a 32 byte key. The Ruby code is setting the key to a 64 byte string consisting of hexadecimal digits. OpenSSL is truncating the string to 32 bytes before use (change key to '4768c01c4f598828ef80d9982d95f888' in the Ruby code and you'll get the same output).

    The Go code however is hex decoding the key before use, converting the 64 hexadecimal digits to the 32 bytes required for the key.

    If you want to change the Go code so that it matches the Ruby result, then you'll need to truncate the key and remove the hex decoding step:

    key := []byte("4768c01c4f598828ef80d9982d95f888")
    

    However, I'd argue that the key handling in the Go version of the code is better. If you want to change the Ruby version to match the Go version, you can hex decode the key before use:

    key = [key].pack('H*')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)