<p>Hi,</p>
<p>I'm trying to re-implement a piece of code from Python to Go, but I'm hitting a wall.
This python code decrypts a block of 1024 bytes with AES in "Counter" mode, but with a custom counter callback function.
Here's the code:</p>
<pre><code> def decrypt(key, source):
data = source.read(1024)
iv = data[:16]
encrypted = data[16:]
counter = Crypto.Util.Counter.new(64, prefix=iv[:8], initial_value=struct.unpack(">Q", iv[8:])[0])
cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CTR, counter=counter)
return cipher.decrypt(encrypted)
</code></pre>
<p>Here's the Go version, which lacks the custom counter:</p>
<pre><code>func decrypt(key []byte, data []byte) []byte {
// data is already 1024 bytes long
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
if len(data) < aes.BlockSize {
panic("ciphertext too short")
}
iv := data[:aes.BlockSize]
data = data[aes.BlockSize:]
ctr := cipher.NewCTR(block, iv)
ctr.XORKeyStream(data, data)
return data
}
</code></pre>
<p>The problem is that I couldn't find a way to pass a custom counter function to <code>cipher.NewCTR(...)</code></p>
<p>Is there an easy way to do so?</p>
<p>Thank you very much.</p>
<hr/>**评论:**<br/><br/>Grundlebuttskin: <pre><p>The implementation looks pretty rigid but quite simple. It would probably take you all of 5 minutes to roll your own: <a href="https://golang.org/src/crypto/cipher/ctr.go" rel="nofollow">https://golang.org/src/crypto/cipher/ctr.go</a></p></pre>sroeuouay: <pre><p>I just saw that!
Yeah it looks quite simple to change how the counter is incremented.
What's the "go way" to create a custom "ctr-prefix" version of this one? Should I get the whole "cipher" package and then compile it with my custom code?</p></pre>Grundlebuttskin: <pre><p>Just create your own type and define a XORKeyStream func that takes your type as a pointer receiver. In other words, you just have to implement the Stream interface. That's all the cipher.ctr type does. Notice that NewCTR even returns a Stream. Does that make sense?</p></pre>sroeuouay: <pre><p>I'm currently trying <a href="/u/alexwhoizzle" rel="nofollow">/u/alexwhoizzle</a> 's method, is there a cleaner way to do it?
Thank you for your help.</p></pre>alexwhoizzle: <pre><p>What I've had to do in my own project was copy over <a href="https://github.com/golang/go/blob/master/src/crypto/cipher/ctr.go" rel="nofollow">ctr.go</a> and <a href="https://github.com/golang/go/blob/master/src/crypto/cipher/xor.go" rel="nofollow">xor.go</a> to my project and change the counter implementation to your liking. </p></pre>sroeuouay: <pre><p>That's what I'm doing right now, thanks for your input :)</p></pre>Jalaska13: <pre><p>Obligatory "don't roll your own crypto if it's security-sensitive" warning. Has to be said :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传