mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-24 06:34:42 +08:00
新增三方工具库
This commit is contained in:
89
util/aes_encrypt.go
Normal file
89
util/aes_encrypt.go
Normal file
@@ -0,0 +1,89 @@
|
||||
// Copyright 2014 mqantserver Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func NewAesEncrypt(key string) (aes *AesEncrypt, err error) {
|
||||
keyLen := len(key)
|
||||
if keyLen < 16 {
|
||||
err = fmt.Errorf("The length of res key shall not be less than 16")
|
||||
return
|
||||
}
|
||||
aes = &AesEncrypt{
|
||||
StrKey: key,
|
||||
}
|
||||
return aes, nil
|
||||
}
|
||||
|
||||
type AesEncrypt struct {
|
||||
StrKey string
|
||||
}
|
||||
|
||||
func (this *AesEncrypt) getKey() []byte {
|
||||
keyLen := len(this.StrKey)
|
||||
if keyLen < 16 {
|
||||
panic("The length of res key shall not be less than 16")
|
||||
}
|
||||
arrKey := []byte(this.StrKey)
|
||||
if keyLen >= 32 {
|
||||
//取前32个字节
|
||||
return arrKey[:32]
|
||||
}
|
||||
if keyLen >= 24 {
|
||||
//取前24个字节
|
||||
return arrKey[:24]
|
||||
}
|
||||
//取前16个字节
|
||||
return arrKey[:16]
|
||||
}
|
||||
|
||||
//加密字符串
|
||||
func (this *AesEncrypt) Encrypt(strMesg string) ([]byte, error) {
|
||||
key := this.getKey()
|
||||
var iv = []byte(key)[:aes.BlockSize]
|
||||
encrypted := make([]byte, len(strMesg))
|
||||
aesBlockEncrypter, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
|
||||
aesEncrypter.XORKeyStream(encrypted, []byte(strMesg))
|
||||
return encrypted, nil
|
||||
}
|
||||
|
||||
//解密字符串
|
||||
func (this *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) {
|
||||
defer func() {
|
||||
//错误处理
|
||||
if e := recover(); e != nil {
|
||||
err = e.(error)
|
||||
}
|
||||
}()
|
||||
key := this.getKey()
|
||||
var iv = []byte(key)[:aes.BlockSize]
|
||||
decrypted := make([]byte, len(src))
|
||||
var aesBlockDecrypter cipher.Block
|
||||
aesBlockDecrypter, err = aes.NewCipher([]byte(key))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv)
|
||||
aesDecrypter.XORKeyStream(decrypted, src)
|
||||
return string(decrypted), nil
|
||||
}
|
||||
Reference in New Issue
Block a user