ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 加密库 <details> <summary>aes.go</summary> ``` package ency import ( "encoding/base64" "github.com/thinkoner/openssl" ) var Aes aesCrypt type aesCrypt struct { } func (a *aesCrypt) Encrypt_128(b []byte,key []byte) ([]byte, error) { return a.encryptByte_XXX(b,key,16) } func (a *aesCrypt) Decrypt_128(b []byte,key []byte) ([]byte, error) { return a.decryptByte_XXX(b,key,16) } func (a *aesCrypt) Decrypt_256(b []byte,key []byte) ([]byte, error) { return a.decryptByte_XXX(b,key,32) } func (a *aesCrypt) Encrypt_256(b []byte,key []byte) ([]byte, error) { return a.encryptByte_XXX(b,key,32) } func (a *aesCrypt) EncryptString_128(str,key string) (string, error) { res, err := a.encryptByte_XXX([]byte(str), []byte(key), 16) if err != nil { return "",err } return base64.StdEncoding.EncodeToString(res),nil } func (a *aesCrypt) DecryptString_128(str,key string) (string, error) { b, err := base64.StdEncoding.DecodeString(str) if err != nil { return "", err } res, err := a.decryptByte_XXX(b, []byte(key), 16) if err != nil { return "", err } return string(res) ,nil } func (a *aesCrypt) EncryptString_256(str,key string) (string, error) { res, err := a.encryptByte_XXX([]byte(str), []byte(key), 32) if err != nil { return "",err } return base64.StdEncoding.EncodeToString(res),nil } func (a *aesCrypt) DecryptString_256(str,key string) (string, error) { b, err := base64.StdEncoding.DecodeString(str) if err != nil { return "", err } res, err := a.decryptByte_XXX(b, []byte(key), 32) if err != nil { return "", err } return string(res) ,nil } // decryptByte_XXX // AES_128->blockSize=16 // AES_192->blockSize=24 // AES_256->blockSize=32 func (a *aesCrypt) decryptByte_XXX(b []byte,key []byte,blockSize int) ([]byte, error) { if len(key) < blockSize { key = openssl.ZerosPadding(key, blockSize) }else if len(key) > blockSize { key=key[:blockSize] } dst, err := openssl.AesECBDecrypt(b, key, openssl.ZEROS_PADDING) return dst, err } func (a *aesCrypt) encryptByte_XXX(b []byte,key []byte,blockSize int) ([]byte, error) { if len(key) < blockSize { key = openssl.ZerosPadding(key, blockSize) }else if len(key) > blockSize { key=key[:blockSize] } res, err := openssl.AesECBEncrypt(b, key, openssl.ZEROS_PADDING) if err != nil { return nil,nil } return res, nil } ``` </details> <br/> <details> <summary>base64.go</summary> ``` package ency import ( "encoding/base64" ) var Base64 base64Crypt type base64Crypt struct { } func (b base64Crypt) Encrypt(src []byte ) string { return base64.StdEncoding.EncodeToString(src) } func (b base64Crypt) DecryptByte(src string ) ([]byte,error) { return base64.StdEncoding.DecodeString(src) } ``` </details> <br/> <details> <summary>md5.go</summary> ``` package ency import ( "github.com/thinkoner/openssl" ) var Md5 md5Crypt type md5Crypt struct { } func (m md5Crypt) EncryptString(str string) string { return openssl.Md5(str) } func (m md5Crypt) EncryptByte(str []byte) string { return openssl.Md5(string(str)) } ``` </details> <br/> <details> <summary>sha256.go</summary> ``` package ency import ( "crypto/sha256" "encoding/hex" ) var Sha256 sha256Crypt type sha256Crypt struct { } func (s sha256Crypt) EncryptString(str string) string { return s.EncryptByte([]byte(str)) } func (s sha256Crypt) EncryptByte(str []byte) string { ha := sha256.New() ha.Write(str) return hex.EncodeToString(ha.Sum(nil)) } ``` </details> <br/> <details> <summary>sm4.go</summary> ``` // 原始sm4 加密没有包含 对src 数据大于16位的处理,并且没有对加密结果进行 base64,再次做封装 package ency import ( "crypto/cipher" "encoding/base64" "errors" "github.com/thinkoner/openssl" "github.com/tjfoc/gmsm/sm4" ) var ( EncryptError = errors.New("sm4 encrypt error") DecryptError = errors.New("sm4 DecryptString_128 error") ) var Sm4 sm4Crypt type sm4Crypt struct { } func (s sm4Crypt) EncryptString(src []byte, key []byte) (string, error) { if len(key) >= sm4.BlockSize { key=key[:sm4.BlockSize] }else{ key=openssl.ZerosPadding(key,sm4.BlockSize) } src = openssl.ZerosPadding(src, sm4.BlockSize) l := len(src) / sm4.BlockSize dstBytes := make([]byte, 0, len(src)) iv := make([]byte, sm4.BlockSize) for i := 0; i < l; i++ { decrypt, err := sm4Encrypt(key, iv, src[i*sm4.BlockSize:(i+1)*sm4.BlockSize]) if err != nil { return "", EncryptError } dstBytes = append(dstBytes, decrypt...) } return base64.StdEncoding.EncodeToString(dstBytes), nil } func (s sm4Crypt) DecryptString(src string, key []byte) ([]byte, error) { if len(key) >= sm4.BlockSize { key=key[:sm4.BlockSize] }else{ key=openssl.ZerosPadding(key,sm4.BlockSize) } b, err := base64.StdEncoding.DecodeString(src) if err != nil { return nil, err } l := len(b) / sm4.BlockSize var dst = make([]byte, 0, l) iv := make([]byte, sm4.BlockSize) for i := 0; i < l; i++ { decrypt, err := sm4Decrypt(key, iv, b[i*sm4.BlockSize:(i+1)*sm4.BlockSize]) if err != nil { return nil, DecryptError } dst = append(dst, decrypt...) } dst = openssl.ZerosUnPadding(dst) return dst, nil } func (s sm4Crypt) Encrypt(src []byte, key []byte) ([]byte, error) { if len(key) >= sm4.BlockSize { key=key[:sm4.BlockSize] }else{ key=openssl.ZerosPadding(key,sm4.BlockSize) } // 对数据进行填充 src = openssl.ZerosPadding(src, sm4.BlockSize) l := len(src) / sm4.BlockSize dstBytes := make([]byte, 0, len(src)) iv := make([]byte, sm4.BlockSize) for i := 0; i < l; i++ { decrypt, err := sm4Encrypt(key, iv, src[i*sm4.BlockSize:(i+1)*sm4.BlockSize]) if err != nil { return nil, EncryptError } dstBytes = append(dstBytes, decrypt...) } return dstBytes, nil } func (s sm4Crypt) Decrypt(b []byte, key []byte) ([]byte, error) { if len(key) >= sm4.BlockSize { key=key[:sm4.BlockSize] }else{ key=openssl.ZerosPadding(key,sm4.BlockSize) } l := len(b) / sm4.BlockSize var dst = make([]byte, 0, l) iv := make([]byte, sm4.BlockSize) for i := 0; i < l; i++ { decrypt, err := sm4Decrypt(key, iv, b[i*sm4.BlockSize:(i+1)*sm4.BlockSize]) if err != nil { return nil, DecryptError } dst = append(dst, decrypt...) } dst = openssl.ZerosUnPadding(dst) return dst, nil } func sm4Encrypt(key, iv, plainText []byte) ([]byte, error) { block, err := sm4.NewCipher(key) if err != nil { return nil, err } blockMode := cipher.NewCBCEncrypter(block, iv) cryted := make([]byte, len(plainText)) blockMode.CryptBlocks(cryted, plainText) return cryted, nil } func sm4Decrypt(key, iv, cipherText []byte) ([]byte, error) { block, err := sm4.NewCipher(key) if err != nil { return nil, err } blockMode := cipher.NewCBCDecrypter(block, iv) origData := make([]byte, len(cipherText)) blockMode.CryptBlocks(origData, cipherText) return origData, nil } ``` </details> <br/> <details> <summary>urlencode.go</summary> ``` package ency import ( "net/url" ) var UrlEncode urlCrypt type urlCrypt struct { } func (m urlCrypt) EncryptString(str string) string { return url.QueryEscape(str) } ``` </details> <br/> <details> <summary>zlib.go</summary> ``` package ency import ( "bytes" "compress/zlib" "io/ioutil" "strings" ) var Zlib zipData type zipData struct { } func (z zipData) EncryptString(str string)([]byte,error){ var b bytes.Buffer w := zlib.NewWriter(&b) _, err := w.Write([]byte(str)) if err != nil { return nil, err } err = w.Close() return b.Bytes(),err } func (z zipData) DecryptString(by string)([]byte,error){ b := strings.NewReader(by) r, err := zlib.NewReader(b) if err != nil { return nil,err } return ioutil.ReadAll(r) } func (z zipData) DecryptByte(by []byte)([]byte,error){ b := bytes.NewReader(by) r, err := zlib.NewReader(b) if err != nil { return nil,err } return ioutil.ReadAll(r) } func (z zipData) EncryptByte(input []byte ) ([]byte,error) { var b bytes.Buffer w := zlib.NewWriter(&b) _, err := w.Write(input) if err != nil { return nil, err } err = w.Close() return b.Bytes(),err } ``` </details> <br/> <details> <summary>al_test.go</summary> ``` package ency import ( "bytes" "testing" ) // TestAes_msg 默认 服务端与客户端的加密 func TestAes_msg(t *testing.T) { src:="UgztkmKKSn0b92SfYbFcaw==" key := make([]byte,0) key = append(key, 1) key = append(key, 1) key=append(key,[]byte("abcdefghijklmnopqrstuvwxyz")...) decryptString, err := Aes.DecryptString_256(src, string(key)) assertErr(t,err) t.Logf(decryptString) } func TestEncryptString_256(t *testing.T) { src1 :="aaaaaaaaaaaaaaaaaaaaaaa" key:="aasdadadsadadasd" out1, err := Aes.EncryptString_256(src1, key) assertErr(t,err) res1, err := Aes.DecryptString_256(out1, key) assertErr(t,err) t.Logf("out1=%v",out1) if src1 != res1 { t.Log(src1) t.Log(res1) t.Errorf(`out1 != res1`) } src2 :="aa" out2, err := Aes.EncryptString_256(src2, key) assertErr(t,err) res2, err := Aes.DecryptString_256(out2, key) assertErr(t,err) t.Logf("out2=%v",out2) if src2 != res2 { t.Logf("src2=%v",src2) t.Logf("out2=%v",out2) t.Logf("res1=%v",res1) t.Errorf(`out1 != res1`) } } func TestAes(t *testing.T) { code:=`KA9NCGDX3JH6CM9CG+iWTSoqsSMj9Shd3eeymAP893QhuQjzBiusxM31/+N6c6643gm5to/ke2AWTlJ7JL/aVrBi4K2/pRkWQjdbTzBWQo+4SQoxxZro11oqqeuHKg6FZnNhDrRK4wJhOsZZkHtaTzPuzAHtkpANteMdg6tYk163i9mGnXTPn/1WbnORtG+G7YEhUthu8NK4WhbNqygHdbT2NPO1MEdavR7XrOYZrHpJhAbypxUxzN8zHkKdNwpUQuuSdFsU18w3ZwpdYVner6a7dBUDg2tTOcPeLw==` strKey := code[0:16] strIn := code[16:] s, err := Aes.DecryptString_128(strIn,strKey) assertErr(t,err) t.Logf(s) } func TestAesByte(t *testing.T) { key := []byte("E1649ed89671e8115e414A0f946fd2fc") orgin := []byte("hello world") encryptByte, err := Aes.Encrypt_256(orgin, key) assertErr(t,err) decryptByte, err := Aes.Decrypt_256(encryptByte, key) assertErr(t,err) if !bytes.Equal(orgin ,decryptByte) { t.Errorf(`orgin != decryptByte orgin=%s decryptByte=%s `,orgin,decryptByte) } } func TestAesString(t *testing.T) { key := "1234567890abcdef" orgin := "hello world" encryptByte, err := Aes.EncryptString_128(orgin, key) t.Logf("encryptByte=%v",encryptByte) assertErr(t,err) decryptByte, err := Aes.DecryptString_128(encryptByte, key) assertErr(t,err) if orgin !=decryptByte { t.Errorf(`orgin != decryptByte orgin=%s decryptByte=%s `,orgin,decryptByte) } } func TestMd5Crypt_Encrypt(t *testing.T) { target := "e10adc3949ba59abbe56e057f20f883e" s := Md5.EncryptString("123456") if s != target { t.Errorf("结果与预期不符 %v 不等于 %v", s, target) } s1 := Md5.EncryptByte([]byte("123456")) if s1 != target { t.Errorf("结果与预期不符 %v 不等于 %v", s1, target) } } func TestZlib_Encode(t *testing.T) { a:=[]byte("hello world") res1,err := Zlib.EncryptByte(a) assertErr(t,err) res2,err := Zlib.DecryptByte(res1) assertErr(t,err) if !bytes.Equal(a,res2) { t.Errorf(`res1 != res2`) } res3, err :=Zlib.EncryptString(string(a)) assertErr(t,err) res4, err :=Zlib.DecryptString(string(res3)) assertErr(t,err) if !bytes.Equal(a,res4) { t.Errorf(`res3 != res4`) } if !bytes.Equal(res1,res3) { t.Errorf(`res1 != res2`) } } func TestBase64(t *testing.T) { src:=[]byte("helo world") s := Base64.Encrypt(src) res1, err := Base64.DecryptByte(s) assertErr(t,err) if !bytes.Equal(src,res1) { t.Errorf(`!bytes.Equal(src,res1)`) } } func TestSm4(t *testing.T) { src := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" key := "41414141414141415050505050505050" out := "V5brYI4Xp8WcxLpM1pqBSFeW62COF6fFnMS6TNaagUiib11tuONUMY354RnUPm9I" encryptByte, err := Sm4.EncryptString([]byte(src), []byte(key)) assertErr(t,err) if out!=encryptByte { t.Error(`!bytes.Equal([]byte(out),encryptByte)`) } decryptString, err := Sm4.DecryptString(out, []byte(key)) assertErr(t,err) if src!=string(decryptString) { t.Errorf(`src!=string(decryptString)`) } } func TestSm4Byte(t *testing.T) { src := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" key := "41414141414141415050505050505050" out, err := Sm4.Encrypt([]byte(src), []byte(key)) assertErr(t,err) res, err := Sm4.Decrypt(out, []byte(key)) assertErr(t,err) t.Log("=========") t.Log(src) t.Log(string(res)) t.Log("=========") if !bytes.Equal([]byte(src),res) { t.Errorf(`bytes.Equal([]byte(src),res1)`) } src1:=[]byte{50,10,10} out1, err := Sm4.Encrypt(src1, []byte(key)) assertErr(t,err) res1, err := Sm4.Decrypt(out1, []byte(key)) if !bytes.Equal(src1,res1) { t.Logf("out1 %v",out1) t.Logf("res1 %v",res1) t.Errorf(`bytes.Equal([]byte(src),res1)`) } } func TestSha256(t *testing.T) { src:="abc" out:="ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" res := Sha256.EncryptString(src) if res != out { t.Errorf(`res != out`) } } func TestUrlEncode(t *testing.T) { src:="你好" out:="%E4%BD%A0%E5%A5%BD" res1 := UrlEncode.EncryptString(src) if res1!=out { t.Errorf(`UrlEncode.EncryptString(src)`) } } func assertErr(t *testing.T,err error) { if err != nil { t.Error(err) } } ``` </details> <br/>