1
kingiis 2020-10-09 17:53:39 +08:00
公用 c 库可能会好点
|
2
songjiaxin2008 2020-10-09 17:59:49 +08:00
可以参考一下这个实现 https://github.com/wumansgy/goEncrypt 引用的包都是标准库的
|
3
songjiaxin2008 2020-10-09 18:00:05 +08:00
NoPadding = ZeroPadding
|
4
songjiaxin2008 2020-10-09 18:01:00 +08:00
网页上跑了下应该没啥问题
|
5
geelaw 2020-10-09 18:11:33 +08:00 via iPhone
为了数据安全最好还是不要对接这个系统,使用非伪随机的密钥以及使用密钥相关的 IV,都会破坏 AES 所有的安全保证。
|
6
teawithlife 2020-10-10 07:42:38 +08:00
其实就是后面补 0x00
play.golang.org/p/MxstiahPDa3 |
7
dafsic 2020-10-10 09:49:22 +08:00
我对接过,跟 java 版本有关,问问 java 是什么版本,最好要到加密部分的 java 代码看看
|
8
TypeErrorNone 2020-10-10 10:15:17 +08:00
之前写的,可以参考下
``` package xaes import ( "bytes" "crypto/aes" "crypto/cipher" "crypto/rand" "crypto/sha256" "encoding/base64" "github.com/pkg/errors" ) type Aes struct { key []byte } func NewAes(key string) (*Aes) { if key == "" { panic("aes key empty") } sum := sha256.Sum256([]byte(key)) return &Aes{ key:sum[:], } } func (a *Aes) Encrypt(encodeBytes []byte) (val string, err error) { block, err := aes.NewCipher(a.key) if err != nil { return } blockSize := block.BlockSize() encodeBytes = a.pkCS5Padding(encodeBytes, blockSize) iv := make([]byte, blockSize) _,err = rand.Read(iv) if err != nil { return } blockMode := cipher.NewCBCEncrypter(block, iv) crypted := make([]byte, len(encodeBytes)) blockMode.CryptBlocks(crypted, encodeBytes) iv = append(iv,crypted...) val = base64.StdEncoding.EncodeToString(iv) return } func (a *Aes) pkCS5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize padtext := bytes.Repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } func (a *Aes) Decrypt(decodeStr string) (origData []byte,err error) { decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr) if err != nil { return } block, err := aes.NewCipher(a.key) if err != nil { return nil, err } if len(decodeBytes) < block.BlockSize() { err = errors.New("decodeBytes 长度不足") return } iv := decodeBytes[:block.BlockSize()] decodeBytes = decodeBytes[block.BlockSize():] blockMode := cipher.NewCBCDecrypter(block, iv) origData = make([]byte, len(decodeBytes)) blockMode.CryptBlocks(origData, decodeBytes) origData = a.pkCS5UnPadding(origData) return } func (a *Aes) pkCS5UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] } ``` |
9
chengxiao OP |
10
sunmoon1983 2021-12-02 14:44:05 +08:00
@TypeErrorNone
哥们,我试了试你给的方法,貌似不太行呀,我把 pkCS5Padding 改成了 zeroPadding,最后出来的数据和人家给的数据就是不太一样 |
11
sunmoon1983 2021-12-02 14:46:39 +08:00
@chengxiao 哥们,你搞出来了吗? Aes Nopadding 加密
|
12
pifuant 2023-08-11 10:38:13 +08:00
@sunmoon1983 @chengxiao 老哥们, 怎么解决的,ZeroPadding 不行啊
|