first commit
This commit is contained in:
commit
53e59ddf89
12 changed files with 558 additions and 0 deletions
54
utils/utils.go
Normal file
54
utils/utils.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func AdaptHandler(h func(http.ResponseWriter, *http.Request)) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
h(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func AesDecrypt(encryptedData, sessionKey, iv string) ([]byte, error) {
|
||||
//Base64解码
|
||||
keyBytes, err := base64.StdEncoding.DecodeString(sessionKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ivBytes, err := base64.StdEncoding.DecodeString(iv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cryptData, err := base64.StdEncoding.DecodeString(encryptedData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
origData := make([]byte, len(cryptData))
|
||||
//AES
|
||||
block, err := aes.NewCipher(keyBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//CBC
|
||||
mode := cipher.NewCBCDecrypter(block, ivBytes)
|
||||
//解密
|
||||
mode.CryptBlocks(origData, cryptData)
|
||||
//去除填充位
|
||||
origData = PKCS7UnPadding(origData)
|
||||
return origData, nil
|
||||
}
|
||||
|
||||
func PKCS7UnPadding(plantText []byte) []byte {
|
||||
length := len(plantText)
|
||||
if length > 0 {
|
||||
unPadding := int(plantText[length-1])
|
||||
return plantText[:(length - unPadding)]
|
||||
}
|
||||
return plantText
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue