如何在Go中正确编写JVM AES / CFB8加密

I wrote a little test in Kotlin to encrypt some text "Hello" using a Cipher instance with the algorithm "AES/CFB8/NoPadding". (minecraft stuff)

And I am now attempting to do the same in Go, however I am unable to produce the same result. All the different methods I have tried always produce something different.

These are the following threads/examples I've already looked through in order to get to this point.

  1. How to use rsa key pair for AES encryption and decryprion in golang
  2. https://play.golang.org/p/77fRvrDa4A
  3. Decrypt in Golang what was encrypted in Python AES CFB
  4. https://gist.github.com/temoto/5052503
  5. AES Encryption in Golang and Decryption in Java
  6. Different Results in Go and Pycrypto when using AES-CFB

Kotlin Code:

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)
}

Go Code:

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
%v
%v
%s
", 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 code produces the output:

Hello
[68, -97, 26, -50, 126]
[72, 101, 108, 108, 111]
Hello

However, the Go code produces the output:

Hello
[68 97 242 158 187]
[72 101 108 108 111]
Hello

At this point, this issue has pretty much halted the progress of the project I'm working on. Any information on what I'm missing or doing wrong would be helpful.

duancuan7057
duancuan7057 是的,我做到了,而且有效。谢谢。@IlmariKaronen
大约一年之前 回复
doushang8846
doushang8846 在上面链接的答案中修正错字后,您是否再试一次?
大约一年之前 回复
douqiju2520
douqiju2520 我在帖子中链接了该问题,以表明我已经看过了,但没有帮助。
大约一年之前 回复
dongpu2694
dongpu2694 使用AES-CFB时Go和Pycrypto中不同结果的可能重复项
大约一年之前 回复
dtzh131555
dtzh131555 看起来XORKeyStream函数中有一个错字,导致解密失败。有趣的是,以前没有人抓到它。无论如何,我只是编辑了答案以解决它,现在看来工作正常。
大约一年之前 回复
dongwen1935
dongwen1935 我实际上也尝试过,但是不起作用...@IlmariKaronen产生了这个:您好[681601937229][72907318647]HZI�/
大约一年之前 回复
dongruoqiong9017
dongruoqiong9017 实际上,kostya对您上面链接的最后一个问题的答案似乎在Go中提供了CFB-8实现。
大约一年之前 回复
duannei1477
duannei1477 虽然我不确定100%,但我敢打赌NewCFBEncrypter/NewCFBDecrypter实现全块CFB模式(即AES为CFB-128),而不是CFB-8。我不知道Go是否有CFB-8的内置实现。如果不是,则您可能必须直接使用原始AES块密码(即“在ECB模式下”)自行实现。
大约一年之前 回复

1个回答



解决方案是手动实现CFB8,因为内置的实现默认为CFB128。</ p>

由kostya创建并由Ilmari Karonen(此处)修复的实现。</ p>
</ div>

展开原文

原文

The solution to this is to implement CFB8 manually since the built in implementation defaults to CFB128.

Implementation created by kostya and fixed by Ilmari Karonen (here).

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问