AES in CTR mode with a custom counter func?

agolangf · · 735 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi,</p> <p>I&#39;m trying to re-implement a piece of code from Python to Go, but I&#39;m hitting a wall. This python code decrypts a block of 1024 bytes with AES in &#34;Counter&#34; mode, but with a custom counter callback function. Here&#39;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(&#34;&gt;Q&#34;, iv[8:])[0]) cipher = Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_CTR, counter=counter) return cipher.decrypt(encrypted) </code></pre> <p>Here&#39;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) &lt; aes.BlockSize { panic(&#34;ciphertext too short&#34;) } 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&#39;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&#39;s the &#34;go way&#34; to create a custom &#34;ctr-prefix&#34; version of this one? Should I get the whole &#34;cipher&#34; 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&#39;s all the cipher.ctr type does. Notice that NewCTR even returns a Stream. Does that make sense?</p></pre>sroeuouay: <pre><p>I&#39;m currently trying <a href="/u/alexwhoizzle" rel="nofollow">/u/alexwhoizzle</a> &#39;s method, is there a cleaner way to do it? Thank you for your help.</p></pre>alexwhoizzle: <pre><p>What I&#39;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&#39;s what I&#39;m doing right now, thanks for your input :)</p></pre>Jalaska13: <pre><p>Obligatory &#34;don&#39;t roll your own crypto if it&#39;s security-sensitive&#34; warning. Has to be said :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

735 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传