xyz/models/common.go
를 수정합니다.
package models
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"io"
)
var key []byte
func init() {
key = []byte(beego.AppConfig.String("enckey"))
orm.RegisterModelWithPrefix("tb_", new(Admin))
}
func encodeBase64(b []byte) []byte {
return []byte(base64.StdEncoding.EncodeToString(b))
}
func decodeBase64(b []byte) []byte {
data, err := base64.StdEncoding.DecodeString(string(b))
if err != nil {
fmt.Printf("Error: Bad Key!\n")
return nil
}
return data
}
func encrypt(text []byte) []byte {
// The key argument should be the AES key,
// either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
b := encodeBase64(text)
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], b)
return ciphertext
}
func decrypt(text []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
if len(text) < aes.BlockSize {
fmt.Printf("Error!\n")
return nil
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
return decodeBase64(text)
}
댓글 없음:
댓글 쓰기