Fetch the repository succeeded.
// Copyright 2019 Andyfoo
//
// 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 xcrypto
import (
"crypto/aes"
"crypto/cipher"
"gitee.com/andyfoo/go-xutils/xencode"
"gitee.com/andyfoo/go-xutils/xlog"
)
func AesEncrypt(src []byte, key []byte, iv []byte) []byte {
if len(key) != 16 {
xlog.Error("key size must is 16")
return []byte{}
}
if len(iv) != 16 {
xlog.Error("iv size must is 16")
return []byte{}
}
block, err := aes.NewCipher(key)
if err != nil {
xlog.Error(err)
return []byte{}
}
bs := block.BlockSize()
data := Pkcs5Padding(src, bs)
if len(data)%bs != 0 {
xlog.Error("need a multiple of the blocksize")
return []byte{}
}
blockmode := cipher.NewCBCEncrypter(block, iv)
dst := make([]byte, len(data))
blockmode.CryptBlocks(dst, data)
return dst
}
func AesEncryptHex(src string, key string, iv string) string {
return xencode.HexEncodeStr(string(AesEncrypt([]byte(src), []byte(key), []byte(iv))))
}
func AesEncryptBase64(src string, key string, iv string) string {
return xencode.Base64EncodeStr(string(AesEncrypt([]byte(src), []byte(key), []byte(iv))))
}
func AesDecrypt(src []byte, key []byte, iv []byte) []byte {
if len(key) != 16 {
xlog.Error("key size must is 16")
return []byte{}
}
if len(iv) != 16 {
xlog.Error("iv size must is 16")
return []byte{}
}
block, err := aes.NewCipher(key)
if err != nil {
xlog.Error(err)
return []byte{}
}
blockmode := cipher.NewCBCDecrypter(block, iv)
dst := make([]byte, len(src))
blockmode.CryptBlocks(dst, src)
return UnPkcs5Padding(dst)
}
func AesDecryptHex(src string, key string, iv string) string {
return string(AesDecrypt(xencode.HexDecode([]byte(src)), []byte(key), []byte(iv)))
}
func AesDecryptBase64(src string, key string, iv string) string {
return string(AesDecrypt(xencode.Base64Decode([]byte(src)), []byte(key), []byte(iv)))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。