golang中的aes加密

作者: aries 分类: Go 发布时间: 2021-12-03 09:01 ė 320次浏览 6 0评论

今天要和第三方的接口做对接,对方给了一组密钥的算法:

str = 406BF0AD11310101220213481000320000
key := ER2Fb6ts3ECX
通过 AES 加密(加解密算法 AES/工作模式 ECB /填充方式 NoPadding)并根据 base64
转码后字符串位:
rebZn7aj61hD3lfsUrhwFgVzPg4yYo9aseP/a4sNTRIh/Vtb0mziFfoHdOZBZ5uj

通过询问得知对方是java语言,所以嘛,貌似ecb nopadding这种方式在golang的标准库中是没有的,只有自力更生了,google一番,再加上大神的指点:

首先要说给你提供这个算法的人压根就不懂密码学。
因为 AES 的要求密钥的字节长度是 64 的倍数,可以是 128、192、256 等等(64 位因为不够安全已经被废弃),当成 ASCII 字符串的话长度就是 16 的倍数。随便给个字符串那不叫密钥,天知道对方用的编程语言或加密库是怎么处理这种不合法的密钥的 —— 是前补 0 了、是前补空了、是后补 0 了、是后补空了、还是怎么着?
而因为密钥的字节长度不确定,所以加密时的一个重要参数 BlockSize 也就不确定了。
得,靠猜吧。当成后补空吧。

得出下面的答案:

package main

import (
"crypto/aes"
"encoding/base64"
"log"
)

func main() {
origin := []byte("406BF0AD11310101220213481000320000")
key := []byte("ER2Fb6ts3ECX")

<span class="highlight-nx">encrypted</span> <span class="highlight-o">:=</span> <span class="highlight-nf">AesEncryptECB</span><span class="highlight-p">(</span><span class="highlight-nx">origin</span><span class="highlight-p">,</span> <span class="highlight-nx">key</span><span class="highlight-p">)</span>
<span class="highlight-nx">log</span><span class="highlight-p">.</span><span class="highlight-nf">Println</span><span class="highlight-p">(</span><span class="highlight-s">&#34;encrypted: &#34;</span><span class="highlight-p">,</span> <span class="highlight-nx">encrypted</span><span class="highlight-p">)</span>
<span class="highlight-nx">decrypted</span> <span class="highlight-o">:=</span> <span class="highlight-nf">AesDecryptECB</span><span class="highlight-p">(</span><span class="highlight-nx">encrypted</span><span class="highlight-p">,</span> <span class="highlight-nx">key</span><span class="highlight-p">)</span>
<span class="highlight-nx">log</span><span class="highlight-p">.</span><span class="highlight-nf">Println</span><span class="highlight-p">(</span><span class="highlight-s">&#34;decrypted: &#34;</span><span class="highlight-p">,</span> <span class="highlight-nb">string</span><span class="highlight-p">(</span><span class="highlight-nx">decrypted</span><span class="highlight-p">))</span>

}

func AesEncryptECB(origData []byte, key []byte) string {
cipher, _ := aes.NewCipher(generateKey(key))
length := (len(origData) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, origData)

<span class="highlight-nx">encrypted</span> <span class="highlight-o">:=</span> <span class="highlight-nb">make</span><span class="highlight-p">([]</span><span class="highlight-kt">byte</span><span class="highlight-p">,</span> <span class="highlight-nb">len</span><span class="highlight-p">(</span><span class="highlight-nx">plain</span><span class="highlight-p">))</span>
<span class="highlight-c1">// Block encryption

for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}

<span class="highlight-k">return</span> <span class="highlight-nx">base64</span><span class="highlight-p">.</span><span class="highlight-nx">StdEncoding</span><span class="highlight-p">.</span><span class="highlight-nf">EncodeToString</span><span class="highlight-p">(</span><span class="highlight-nx">encrypted</span><span class="highlight-p">)</span>

}

func AesDecryptECB(str string, key []byte) (decrypted []byte) {
encrypted, _ := base64.StdEncoding.DecodeString(str)

<span class="highlight-nx">cipher</span><span class="highlight-p">,</span> <span class="highlight-nx">_</span> <span class="highlight-o">:=</span> <span class="highlight-nx">aes</span><span class="highlight-p">.</span><span class="highlight-nf">NewCipher</span><span class="highlight-p">(</span><span class="highlight-nf">generateKey</span><span class="highlight-p">(</span><span class="highlight-nx">key</span><span class="highlight-p">))</span>
<span class="highlight-nx">decrypted</span> <span class="highlight-p">=</span> <span class="highlight-nb">make</span><span class="highlight-p">([]</span><span class="highlight-kt">byte</span><span class="highlight-p">,</span> <span class="highlight-nb">len</span><span class="highlight-p">(</span><span class="highlight-nx">encrypted</span><span class="highlight-p">))</span>
<span class="highlight-k">for</span> <span class="highlight-nx">bs</span><span class="highlight-p">,</span> <span class="highlight-nx">be</span> <span class="highlight-o">:=</span> <span class="highlight-mi">0</span><span class="highlight-p">,</span> <span class="highlight-nx">cipher</span><span class="highlight-p">.</span><span class="highlight-nf">BlockSize</span><span class="highlight-p">();</span> <span class="highlight-nx">bs</span> <span class="highlight-p">&lt;</span> <span class="highlight-nb">len</span><span class="highlight-p">(</span><span class="highlight-nx">encrypted</span><span class="highlight-p">);</span> <span class="highlight-nx">bs</span><span class="highlight-p">,</span> <span class="highlight-nx">be</span> <span class="highlight-p">=</span> <span class="highlight-nx">bs</span><span class="highlight-o">+</span><span class="highlight-nx">cipher</span><span class="highlight-p">.</span><span class="highlight-nf">BlockSize</span><span class="highlight-p">(),</span> <span class="highlight-nx">be</span><span class="highlight-o">+</span><span class="highlight-nx">cipher</span><span class="highlight-p">.</span><span class="highlight-nf">BlockSize</span><span class="highlight-p">()</span> <span class="highlight-p">{</span>
	<span class="highlight-nx">cipher</span><span class="highlight-p">.</span><span class="highlight-nf">Decrypt</span><span class="highlight-p">(</span><span class="highlight-nx">decrypted</span><span class="highlight-p">[</span><span class="highlight-nx">bs</span><span class="highlight-p">:</span><span class="highlight-nx">be</span><span class="highlight-p">],</span> <span class="highlight-nx">encrypted</span><span class="highlight-p">[</span><span class="highlight-nx">bs</span><span class="highlight-p">:</span><span class="highlight-nx">be</span><span class="highlight-p">])</span>
<span class="highlight-p">}</span>

<span class="highlight-nx">trim</span> <span class="highlight-o">:=</span> <span class="highlight-mi">0</span>
<span class="highlight-k">if</span> <span class="highlight-nb">len</span><span class="highlight-p">(</span><span class="highlight-nx">decrypted</span><span class="highlight-p">)</span> <span class="highlight-p">&gt;</span> <span class="highlight-mi">0</span> <span class="highlight-p">{</span>
	<span class="highlight-nx">trim</span> <span class="highlight-p">=</span> <span class="highlight-nb">len</span><span class="highlight-p">(</span><span class="highlight-nx">decrypted</span><span class="highlight-p">)</span> <span class="highlight-o">-</span> <span class="highlight-nb">int</span><span class="highlight-p">(</span><span class="highlight-nx">decrypted</span><span class="highlight-p">[</span><span class="highlight-nb">len</span><span class="highlight-p">(</span><span class="highlight-nx">decrypted</span><span class="highlight-p">)</span><span class="highlight-o">-</span><span class="highlight-mi">1</span><span class="highlight-p">])</span>
<span class="highlight-p">}</span>
<span class="highlight-k">return</span> <span class="highlight-nx">decrypted</span><span class="highlight-p">[:</span><span class="highlight-nx">trim</span><span class="highlight-p">]</span>

}

func generateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 16)
copy(genKey, key)
for i := 16; i < len(key); {
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i]
}
}
return genKey
}

0 goaes
换一个
暂无评论
Ɣ回顶部