英文:
Migrate Java decryption code to Golang
问题
我在过去几天里一直在努力将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必须在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
- 对于
CBzip2InputStream
,必须省略bzip2头部("BZ"),但对于bzip2.NewReader
则不需要 - 从Java和Golang中的
instream
读取的前16个字节是相同的,从第17个字节开始,由于某种原因,所有字节都不同
英文:
I'm struggling with the migration of Java code to Golang for the last few days and I am now stuck. This is the working Java code:
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);
My implementation in 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)
What I know so far:
- all error values omitted by
_
arenil
in this piece of code - the bzip2 header ("BZ") must be omitted for
CBzip2InputStream
, but not forbzip2.NewReader
- the first 16 bytes read from
instream
in Java and golang are the same, starting with the 17th byte all bytes differ for whatever reason
答案1
得分: 9
CBizp2InputStream确实使用AES ECB。这是一个可工作的实现。我省略了错误处理以使代码更短:
c, _ := aes.NewCipher([]byte(keyString))
bufIn := make([]byte, 16)
bufOut := make([]byte, 16)
dec := bytes.NewBuffer(make([]byte, 0))
var i int
for {
i, _ = src.Read(bufIn)
if i == 0 {
break
}
c.Decrypt(bufOut, bufIn)
dec.Write(bufOut)
}
zip := bzip2.NewReader(dec)
io.Copy(dst, zip)
附加说明:
- src是一个io.Reader,dst是一个io.Writer,两者都作为参数提供给解密函数
- keyString包含秘密密钥
- 我使用
i == 0
作为终止条件,因为在最后一个成功读取时,err
可以设置为io.EOF(参见golang io.Reader规范)
完美运行。现在实现加密应该很容易。
英文:
CBizp2InputStream indeed uses AES ECB. This is a working implementation. I omitted error handling to make the code shorter:
c, _ := aes.NewCipher([]byte(keyString))
bufIn := make([]byte, 16)
bufOut := make([]byte, 16)
dec := bytes.NewBuffer(make([]byte, 0))
var i int
for {
i, _ = src.Read(bufIn)
if i == 0 {
break
}
c.Decrypt(bufOut, bufIn)
dec.Write(bufOut)
}
zip := bzip2.NewReader(dec)
io.Copy(dst, zip)
Additional explanation:
- src is an io.Reader and dst is an io.Writer, both supplied to the decrypt function as arguments
- keyString contains the secret key
- I use
i == 0
as breaking condition becauseerr
can or cannot be set to io.EOF in the last successful read (see golang io.Reader specification)
Works perfectly. Implementing encryption should now be easy.
答案2
得分: 0
我正在尝试使用三重DES ECB模式。我找到了一个ECB的代码,因为它在Go的加密库中没有实现,因为它不安全。然而,当初始化三重DES密码时,我在密钥的长度上遇到了一个错误,并且我被迫使用特定的密钥。密码库中抛出错误的代码是:
// NewTripleDESCipher creates and returns a new cipher.Block.
func NewTripleDESCipher(key []byte) (cipher.Block, error) {
if len(key) != 24 {
return nil, KeySizeError(len(key))
}
我该如何解决这个问题?请注意,在Java中我没有这个问题。
英文:
I am trying the same but usinh triple DES ECB mode. I found a code for ECB since it is not implemented in go crypto lib as it is not secure. However when initializing triple DES cipher, i am getting an error on the length of the key and i am obliged to use a certain key. The code from the cipher library throwing the error is:
// NewTripleDESCipher creates and returns a new cipher.Block.
func NewTripleDESCipher(key []byte) (cipher.Block, error) {
if len(key) != 24 {
return nil, KeySizeError(len(key))
}
What can i do to overcome this issue? Note that in Java i dont have this problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论