Golang DES with Initialization Vector and Secret Key?

agolangf · · 359 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Here is my following code: <a href="https://play.golang.org/p/gIee9raIQV" rel="nofollow">https://play.golang.org/p/gIee9raIQV</a> Currently, I&#39;m trying to encrypt and decrypt using DES, a specified key, and a specified IV. The encryption is not what I am expecting and I think it is because I am not properly prepending the IV to the ciphertext. However, I don&#39;t know how I would prepend the IV to the ciphertext.</p> <hr/>**评论:**<br/><br/>echophant: <pre><p><a href="https://golang.org/pkg/crypto/aes/" rel="nofollow">AES</a> and <a href="https://golang.org/pkg/crypto/des/" rel="nofollow">DES</a> both implement the <a href="https://golang.org/pkg/crypto/cipher/#Block" rel="nofollow">cipher.Block</a> interface. You should be able to take your secret key and feed it into <a href="https://golang.org/pkg/crypto/des/#NewCipher" rel="nofollow">des.NewCipher</a>, then take the resulting <code>cipher.Block</code> and plug it into any of the <a href="https://golang.org/pkg/crypto/cipher" rel="nofollow">cipher</a> methods that take a <code>Block</code> and an <code>iv</code>.</p></pre>ramirezp6856: <pre><p>The following <a href="https://play.golang.org/p/0etXPnCaye" rel="nofollow">code</a> executes, but this is not how I want to implement DES.</p> <ul> <li><p>The <strong>key</strong> variable throws an error when it is larger than 8 bytes. I would like to be able to specify a <strong>key</strong> in the following format: &#34;0xbc, 0xbc, 0xbc, 0xbc, 0xbc, 0xbc, 0xbc, 0xbc&#34;</p></li> <li><p>The <strong>iv</strong> variable throws an error because <strong>iv</strong> length must equal block size. I would like to be able to specify <strong>iv</strong> in the same format as the key: &#34;0xbc, 0xbc, 0xbc, 0xbc, 0xbc, 0xbc, 0xbc, 0xbc&#34;</p></li> </ul></pre>gohacker: <pre><p><a href="https://en.wikipedia.org/wiki/Data_Encryption_Standard" rel="nofollow">https://en.wikipedia.org/wiki/Data_Encryption_Standard</a></p></pre>ramirezp6856: <pre><p>At first, I like -_- </p> <p>But, then I read it... </p> <p>and now my issue relies with utf8.</p></pre>ramirezp6856: <pre><p>I am getting closer. I figured out that I was suppose to specify the key and iv in the following format/syntax</p> <pre><code>[]byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC}. </code></pre> <p>Even though the <a href="https://play.golang.org/p/g86TgoUf6P" rel="nofollow">code</a> runs, it is not encrypting or decrypting correctly. Any tips/suggestions/clues?</p></pre>ericzhill: <pre><p>I would start with a <a href="https://play.golang.org/p/2epUZ0oMiB" rel="nofollow">simple example</a> and <a href="https://play.golang.org/p/z-CLICXkdZ" rel="nofollow">expand</a> from there.</p></pre>ramirezp6856: <pre><p>hmmmm... I can&#39;t input a plain text string of only 5 characters long. Also, I need the encrypted string to be 12 characters long. From what I have been reading it looks like I may need to use CBC encryption. I found this <a href="http://extranet.cryptomathic.com/descalc/index" rel="nofollow">DES tool</a> and it says &#34;IV (only used for CBC mode)&#34; which makes me believe I should be using CBC. That and the fact the encrypted string is 12 characters long for CBC. Here is my <a href="https://play.golang.org/p/2JfiwG8gOF" rel="nofollow">code</a> for DES CBC. Unfortunately, the plain text has to be exactly 8 characters long.</p></pre>ericzhill: <pre><p>The plain text string must be <strong>at least</strong> the length of the key. Shorter texts are traditionally padded with trailing null values to make them long enough.</p> <p>I&#39;m not sure if you care, but DES is recommended against for good security at this point. It can be brute forced in a reasonable amount of time (days) with <a href="https://aws.amazon.com/blogs/aws/new-p2-instance-type-for-amazon-ec2-up-to-16-gpus/" rel="nofollow">sufficient hardware resources</a> for not too much money.</p></pre>

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

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