go语言AES CFB加解密服务端与客户端互通

xiaoxiao2021-02-28  123

package utils import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" "log" ) var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} var key = "32位key" func AesEncrypt(content string) string { // 创建加密算法aes c, err := aes.NewCipher([]byte(key)) if err != nil { log.Fatalln("Error: NewCipher(%d bytes) = %s", len(key), err) } //加密字符串 cfb := cipher.NewCFBEncrypter(c, commonIV) ciphertext := make([]byte, len(content)) cfb.XORKeyStream(ciphertext, []byte(content)) fmt.Printf("%s=>%x\n", content, ciphertext) ciphertext = Base64Encode(ciphertext) return string(ciphertext) } func AesDecrypt(content string) string { text := make([]byte, len(content)) var err error text, err = Base64Decode([]byte(content)) if err != nil { log.Fatalln(err) } // 创建加密算法aes c, err := aes.NewCipher([]byte(key)) if err != nil { log.Fatalln("Error: NewCipher(%d bytes) = %s", len(key), err) } // 解密字符串 decryptText := make([]byte, len(text)) cfbdec := cipher.NewCFBDecrypter(c, commonIV) cfbdec.XORKeyStream(decryptText, []byte(text)) fmt.Printf("%x=>%s\n", text, decryptText) return string(decryptText) }

客户端解密时不同语言容易出现问题,一种简单的处理办法就是对go代码交叉编译出android端和ios端的lib库,我们可以使用go mobile,https://github.com/golang/go/wiki/Mobile 获取: $ go get golang.org/x/mobile/cmd/gomobile $ gomobile init # it might take a few minutes 编译: $ gomobile bind -target=android golang.org/x/mobile/example/bind/hello 会生成一个aar

转载请注明原文地址: https://www.6miu.com/read-51449.html

最新回复(0)