如何在Go中正确编写JVM AES/CFB8加密 [英] How to properly write JVM AES/CFB8 Encryption in Go

查看:125
本文介绍了如何在Go中正确编写JVM AES/CFB8加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Kotlin中编写了一个小测试,以使用算法为"AES/CFB8/NoPadding"的Cipher实例对一些文本"Hello"进行加密. (minecraft的东西)

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

我现在正尝试在Go中执行相同的操作,但是我无法产生相同的结果.我尝试过的所有不同方法总是会产生不同的结果.

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. 如何使用用于golang中AES加密和解密的rsa密钥对
  2. https://play.golang.org/p/77fRvrDa4A
  3. 在Golang中解密在Python AES中加密的内容CFB
  4. https://gist.github.com/temoto/5052503
  5. Golang中的AES加密和Java中的解密
  6. 使用AES时Go和Pycrypto的结果不同-CFB
  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

科特琳代码:

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
}

科特琳代码产生输出:

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

但是,Go代码会产生输出:

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.

推荐答案

此解决方案是手动实现CFB8,因为内置实现默认为CFB128.

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

由kostya创建并由Ilmari Karonen(此处)修复的实现.

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

这篇关于如何在Go中正确编写JVM AES/CFB8加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆