Ai
1 Star 1 Fork 0

Sailinst/utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
id5helper.go 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
Yang Jay 提交于 2017-11-30 19:09 +08:00 . add id5helper
package utils
import (
"bytes"
"crypto/cipher"
"crypto/des"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
)
const (
base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func DesEncrypt(origData, key []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
origData = PKCS5Padding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key)
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func DesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, key)
origData := make([]byte, len(crypted))
// origData := crypted
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
// origData = ZeroUnPadding(origData)
return origData, nil
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
// 去掉最后一个字节 unpadding 次
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func Id5Encode(source string) (string, error) {
if data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(source)), simplifiedchinese.GBK.NewEncoder())); err == nil {
if dst, err := DesEncrypt(data, []byte("12345678")); err == nil {
coder := base64.NewEncoding(base64Table)
return coder.EncodeToString(dst), nil
} else {
return "", err
}
} else {
return "", err
}
}
func Id5Decode(source string) (string, error) {
coder := base64.NewEncoding(base64Table)
if data, err := coder.DecodeString(source); err == nil {
if dst, err := DesDecrypt(data, []byte("12345678")); err == nil {
if da, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(dst), simplifiedchinese.GBK.NewDecoder())); err == nil {
return string(da), nil
} else {
return "", err
}
} else {
return "", err
}
} else {
return "", err
}
}
func RsaEncrypt(origData, publicKey []byte) ([]byte, error) {
block, key := pem.Decode(publicKey)
fmt.Println("key:=", string(key))
if block == nil {
return nil, errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pub := pubInterface.(*rsa.PublicKey)
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
func RsaDecrypt(ciphertext, privateKey []byte) ([]byte, error) {
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error!")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/sailinst/utils.git
git@gitee.com:sailinst/utils.git
sailinst
utils
utils
bd18503514a5

搜索帮助