Encrypt and Decrypt AES with Golang and Ruby

huangapple go评论97阅读模式
英文:

Encrypt and Decrypt AES with Golang and Ruby

问题

我正在努力让两个安全系统通过一种共同的加密方案进行通信。我选择了AES作为加密标准,因为它似乎是一种安全的标准,但我并不固执于它,只要我能够进行双向加密即可。

这里有一个简化的示例,使用Go语言和Ruby语言编写的源代码,可以在命令行中运行并查看它们之间的区别。我输出字节码以便进行更容易的直接比较。

我在两个系统中都使用了128位的CFB模式,并且它们似乎都没有使用填充。非常感谢任何帮助!

英文:

I'm working on making two secure systems talk via a common encryption scheme. I picked AES as it seems a secure standard, but I'm not married to it, so long as I have two way encryption.

Here is the Go source and Ruby source simplified down to a really clear example to run from command line and see the differences. I'm outputting bytecode for easier literal comparison.

I'm using 128 bit CFB in both, and neither of them appear to have padding, any help is greatly appreciated!

答案1

得分: 2

你在 Ruby 代码中传递了错误的密钥大小。应该是192位(因为key.size为24字节,等于192位)。

cipher = OpenSSL::Cipher::AES.new(192, :CFB)
cipher.encrypt
cipher.key = key
cipher.iv = iv
encrypted = cipher.update(input) + cipher.final()
puts "Output:     [" + encrypted.bytes.join(" ") + "]"

输出结果:

Output:     [155 79 127 80 31 163 142 111 13 211 221 163 219 248]
英文:

You passed wrong key size in Ruby code. It should be 192. (because key.size is 24 bytes == 192 bits)

cipher = OpenSSL::Cipher::AES.new(192, :CFB)
cipher.encrypt
cipher.key = key
cipher.iv = iv
encrypted = cipher.update(input) + cipher.final()
puts "Output:     [" + encrypted.bytes.join(" ") + "]"

output:

Output:     [155 79 127 80 31 163 142 111 13 211 221 163 219 248]

huangapple
  • 本文由 发表于 2014年3月23日 21:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/22591778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定