gaes developing

This commit is contained in:
john 2018-08-08 18:41:09 +08:00
parent 5ad9b272a6
commit e3699a2427
3 changed files with 184 additions and 6 deletions

67
g/crypto/gaes/gaes.go Normal file
View File

@ -0,0 +1,67 @@
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// AES
package gaes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
)
const (
ivDefValue = "I Love Go Frame!"
)
func Encrypt(plaintext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
plaintext = PKCS5Padding(plaintext, blockSize)
iv := []byte(ivDefValue)
blockMode := cipher.NewCBCEncrypter(block, iv)
ciphertext := make([]byte, len(plaintext))
blockMode.CryptBlocks(ciphertext, plaintext)
return ciphertext, nil
}
func Decrypt(cipherText []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(cipherText) < blockSize {
return nil, errors.New("cipherText too short")
}
iv := []byte(ivDefValue)
if len(cipherText)%blockSize != 0 {
return nil, errors.New("cipherText is not a multiple of the block size")
}
blockModel := cipher.NewCBCDecrypter(block, iv)
plaintext := make([]byte, len(cipherText))
blockModel.CryptBlocks(plaintext, cipherText)
plaintext = PKCS5UnPadding(plaintext)
return plaintext, nil
}
func PKCS5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func PKCS5UnPadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}

View File

@ -0,0 +1,51 @@
package main
import (
"gitee.com/johng/gf/g/database/gdb"
"fmt"
"gitee.com/johng/gf/g/crypto/gaes"
"gitee.com/johng/gf/g"
)
func main() {
gdb.AddDefaultConfigNode(gdb.ConfigNode {
Host : "127.0.0.1",
Port : "3306",
User : "root",
Pass : "123456",
Name : "test",
Type : "mysql",
Role : "master",
Charset : "utf8",
})
db, err := gdb.New()
if err != nil {
panic(err)
}
key := "0123456789123456"
name := "john"
encryptedName, err := gaes.Encrypt([]byte(name), []byte(key))
if err != nil {
fmt.Println(err)
}
// 写入
r, err := db.Table("user").Data(g.Map{
"uid" : 1,
"name" : encryptedName,
}).Save()
if err != nil {
fmt.Println(err)
}
fmt.Println(r.RowsAffected())
// 查询
one, err := db.Table("user").Where("name=?", encryptedName).One()
if err != nil {
fmt.Println(err)
}
fmt.Println(one.ToMap())
}

View File

@ -1,15 +1,75 @@
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"gitee.com/johng/gf/g/util/gregex"
"gitee.com/johng/gf/g/encoding/gbase64"
)
const (
ivDefValue = "I Love Go Frame!"
)
func main() {
name := "page"
path := "/page/template/{page}.html"
rule := fmt.Sprintf(`{%s}`, name, name)
tpl, err := gregex.ReplaceString(rule, `{.page}`, path)
v := "1234567890123456789012345678901234567890123456789012345678901234567890"
k := "123456789012345 "
e, err := AesEncrypt([]byte(v), []byte(k))
fmt.Println(err)
fmt.Println(tpl)
fmt.Println(len(e))
fmt.Println(string(gbase64.Encode(string(e))))
d, err := AesDecrypt([]byte(e), []byte(k))
fmt.Println(err)
fmt.Println(string(d))
}
func AesEncrypt(plaintext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
plaintext = PKCS5Padding(plaintext, blockSize)
iv := []byte(ivDefValue)
blockMode := cipher.NewCBCEncrypter(block, iv)
ciphertext := make([]byte, len(plaintext))
blockMode.CryptBlocks(ciphertext, plaintext)
return ciphertext, nil
}
func AesDecrypt(cipherText []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(cipherText) < blockSize {
return nil, errors.New("cipherText too short")
}
iv := []byte(ivDefValue)
if len(cipherText)%blockSize != 0 {
return nil, errors.New("cipherText is not a multiple of the block size")
}
blockModel := cipher.NewCBCDecrypter(block, iv)
plaintext := make([]byte, len(cipherText))
blockModel.CryptBlocks(plaintext, cipherText)
plaintext = PKCS5UnPadding(plaintext)
return plaintext, nil
}
func PKCS5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func PKCS5UnPadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}