【发布时间】:2019-12-23 16:58:15
【问题描述】:
我在 Kotlin 中编写了一个小测试,使用算法“AES/CFB8/NoPadding”的 Cipher 实例加密一些文本“Hello”。 (我的世界的东西)
我现在正尝试在 Go 中做同样的事情,但是我无法产生相同的结果。我尝试过的所有不同方法总是产生不同的结果。
为了达到这一点,我已经查看了以下线程/示例。
- How to use rsa key pair for AES encryption and decryprion in golang
- https://play.golang.org/p/77fRvrDa4A
- Decrypt in Golang what was encrypted in Python AES CFB
- https://gist.github.com/temoto/5052503
- AES Encryption in Golang and Decryption in Java
- Different Results in Go and Pycrypto when using AES-CFB
Kotlin 代码:
enum class Mode(val mode: Int)
{
ENCRYPT(Cipher.ENCRYPT_MODE),
DECRYPT(Cipher.DECRYPT_MODE),
}
fun createSecret(data: String): SecretKey
{
return SecretKeySpec(data.toByteArray(), "AES")
}
fun newCipher(mode: Mode): Cipher
{
val secret = createSecret("qwdhyte62kjneThg")
val cipher = Cipher.getInstance("AES/CFB8/NoPadding")
cipher.init(mode.mode, secret, IvParameterSpec(secret.encoded))
return cipher
}
fun runCipher(data: ByteArray, cipher: Cipher): ByteArray
{
val output = ByteArray(data.size)
cipher.update(data, 0, data.size, output)
return output
}
fun main()
{
val encrypter = newCipher(Mode.ENCRYPT)
val decrypter = newCipher(Mode.DECRYPT)
val iText = "Hello"
val eText = runCipher(iText.toByteArray(), encrypter)
val dText = runCipher(eText, decrypter)
val oText = String(dText)
println(iText)
println(Arrays.toString(eText))
println(Arrays.toString(dText))
println(oText)
}
转码:
func TestCipher(t *testing.T) {
secret := newSecret("qwdhyte62kjneThg")
encrypter := newCipher(secret, ENCRYPT)
decrypter := newCipher(secret, DECRYPT)
iText := "Hello"
eText := encrypter.run([]byte(iText))
dText := decrypter.run(eText)
oText := string(dText)
fmt.Printf("%s\n%v\n%v\n%s\n", iText, eText, dText, oText)
}
type Mode int
const (
ENCRYPT Mode = iota
DECRYPT
)
type secret struct {
Data []byte
}
type cipherInst struct {
Data cipher2.Block
Make cipher2.Stream
}
func newSecret(text string) *secret {
return &secret{Data: []byte(text)}
}
func newCipher(data *secret, mode Mode) *cipherInst {
cip, err := aes.NewCipher(data.Data)
if err != nil {
panic(err)
}
var stream cipher2.Stream
if mode == ENCRYPT {
stream = cipher2.NewCFBEncrypter(cip, data.Data)
} else {
stream = cipher2.NewCFBDecrypter(cip, data.Data)
}
return &cipherInst{Data: cip, Make: stream}
}
func (cipher *cipherInst) run(dataI []byte) []byte {
out := make([]byte, len(dataI))
cipher.Make.XORKeyStream(out, dataI)
return out
}
Kotlin 代码产生输出:
Hello
[68, -97, 26, -50, 126]
[72, 101, 108, 108, 111]
Hello
但是,Go 代码会产生输出:
Hello
[68 97 242 158 187]
[72 101 108 108 111]
Hello
在这一点上,这个问题几乎停止了我正在从事的项目的进展。任何有关我遗漏或做错的信息都会有所帮助。
【问题讨论】:
-
虽然我不确定 100%,但我愿意打赌
NewCFBEncrypter/NewCFBDecrypter实现全块 CFB 模式(即用于 AES 的 CFB-128),而不是 CFB- 8.我不知道 is 是否有 CFB-8 for Go 的内置实现;如果没有,您可能必须直接使用原始 AES 分组密码自己实现(即“在 ECB 模式下”)。 -
事实上,kostya's answer 到您上面链接到的最后一个问题似乎在 Go 中提供了 CFB-8 实现。
-
我实际上也尝试过,但它不起作用......@IlmariKaronen 产生这个:你好 [68 160 19 37 229] [72 90 73 186 47] HZI�/
-
看起来
XORKeyStream函数中有错字导致解密失败。有趣的是以前没有人发现它......无论如何,我只是编辑了答案来修复它,it seems to work fine now。 -
是的,我做到了,而且成功了。谢谢你。 @IlmariKaronen
标签: go encryption kotlin aes