在过去的几天里,我一直在努力将Java代码迁移到Golang,现在我陷入了困境。这是有效的Java代码:


final Key k = new SecretKeySpec(keyString.getBytes(), "AES");

Cipher c = Cipher.getInstance("AES");

c.init(Cipher.DECRYPT_MODE, k);


final InputStream in = new BufferedInputStream(new FileInputStream(fileNameToDecrypt));

final CipherInputStream instream = new CipherInputStream(in, c);


if (instream.read() != 'B') {

    System.out.println("Error");

}


if (instream.read() != 'Z') {

    System.out.println("Error");

}


final CBZip2InputStream zip = new CBZip2InputStream(instream);

我在Golang中的实现:


c, _ := aes.NewCipher([]byte(keyString))

// IV must be defined in golang

iv := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

d := cipher.NewCBCDecrypter(c, iv)


fi, _ := os.Open(fileNameToDecrypt)

stat, _ := fi.Stat()

enc := make([]byte, stat.Size())

dec := make([]byte, stat.Size())

fi.Read(enc)

d.CryptBlocks(dec, enc)

instream := bytes.NewBuffer(dec)

zip := bzip2.NewReader(instream)

到目前为止我所知道的:


这段代码中所有被忽略的错误值_都nil在这段代码中

必须为省略bzip2标头(“ BZ”)CBzip2InputStream,但不能为bzip2.NewReader

从instreamJava和golang中读取的前16个字节是相同的,从第17个字节开始,所有字节由于某种原因而有所不同