123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package utils
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "crypto/des"
- "crypto/rand"
- "encoding/hex"
- "fmt"
- "io"
- )
- var JS_KEY = []byte("mianbaon")
- var GO_KEY = []byte("mirendemianbaona")
- // Go DES ECB加密
- func EncryptDES_ECB(src string, key string) string {
- keyByte := JS_KEY
- if len(key) == 8 {
- keyByte = []byte(key)
- }
- data := []byte(src)
- block, err := des.NewCipher(keyByte)
- if err != nil {
- panic(err)
- }
- bs := block.BlockSize()
- //对明文数据进行补码
- data = PKCS5Padding(data, bs)
- if len(data)%bs != 0 {
- panic("Need a multiple of the blocksize")
- }
- out := make([]byte, len(data))
- dst := out
- for len(data) > 0 {
- //对明文按照blocksize进行分块加密
- //必要时可以使用go关键字进行并行加密
- block.Encrypt(dst, data[:bs])
- data = data[bs:]
- dst = dst[bs:]
- }
- return fmt.Sprintf("%X", out)
- }
- // Go DES ECB解密
- func DecryptDES_ECB(src string, key string) string {
- data, err := hex.DecodeString(src)
- if err != nil {
- panic(err)
- }
- keyByte := JS_KEY
- if len(key) == 8 {
- keyByte = []byte(key)
- }
- block, err := des.NewCipher(keyByte)
- if err != nil {
- panic(err)
- }
- bs := block.BlockSize()
- if len(data)%bs != 0 {
- panic("crypto/cipher: input not full blocks")
- }
- out := make([]byte, len(data))
- dst := out
- for len(data) > 0 {
- block.Decrypt(dst, data[:bs])
- data = data[bs:]
- dst = dst[bs:]
- }
- out = PKCS5UnPadding(out)
- return string(out)
- }
- // 明文补码算法
- func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
- padding := blockSize - len(ciphertext)%blockSize
- padtext := bytes.Repeat([]byte{byte(padding)}, padding)
- return append(ciphertext, padtext...)
- }
- // 明文减码算法
- func PKCS5UnPadding(origData []byte) []byte {
- length := len(origData)
- unpadding := int(origData[length-1])
- return origData[:(length - unpadding)]
- }
- // 加密
- func Encrypt(plaintextStr string) (string, error) {
- // 将字符串形式的 plaintext 转换为字节切片
- plaintext := []byte(plaintextStr)
- // 创建一个新的 AES 密码块
- block, err := aes.NewCipher(GO_KEY)
- if err != nil {
- return "", err
- }
- // 创建一个新的 GCM 模式的 AEAD(Authenticated Encryption with Associated Data)
- aead, err := cipher.NewGCM(block)
- if err != nil {
- return "", err
- }
- // 生成一个随机的 nonce(在 GCM 模式中,nonce 应该是唯一的)
- nonce := make([]byte, aead.NonceSize())
- if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
- return "", err
- }
- // 加密数据并添加认证标签
- ciphered := aead.Seal(nil, nonce, plaintext, nil)
- // 将 nonce 和 ciphertext 连接起来
- encryptedData := append(nonce, ciphered...)
- // 将加密后的数据转换为十六进制字符串并返回
- return hex.EncodeToString(encryptedData), nil
- }
- // 解密
- func Decrypt(cipheredStr string) (string, error) {
- // 将字符串形式的 ciphered 转换为字节切片
- ciphered, err := hex.DecodeString(cipheredStr)
- if err != nil {
- return "", err
- }
- // 创建一个新的 AES 密码块
- block, err := aes.NewCipher(GO_KEY)
- if err != nil {
- return "", err
- }
- // 创建一个新的 GCM 模式的 AEAD
- aead, err := cipher.NewGCM(block)
- if err != nil {
- return "", err
- }
- // 从 ciphered 中分离出 nonce 和实际的加密数据
- nonceSize := aead.NonceSize()
- nonce, ciphered := ciphered[:nonceSize], ciphered[nonceSize:]
- // 解密数据并验证认证标签
- plaintext, err := aead.Open(nil, nonce, ciphered, nil)
- if err != nil {
- return "", err
- }
- // 将解密后的字节切片转换为字符串并返回
- return string(plaintext), nil
- }
|