golang是如何实现3DES加密

import (
	"buslog"
	"bytes"
	"crypto/des"
	"encoding/base64"
	"errors"
)

//ECB PKCS5Padding
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padtext...)
}

//ECB PKCS5Unpadding
func PKCS5Unpadding(origData []byte) []byte {
	length := len(origData)
	unpadding := int(origData[length-1])
	return origData[:(length - unpadding)]
}

//Des加密
func encrypt(logPrefix string, origData, key []byte) ([]byte, error) {
	if len(origData) < 1 || len(key) < 1 {
		buslog.GSLog.Error(logPrefix + "wrong data or key")
		return nil, errors.New("wrong data or key")
	}
	block, err := des.NewCipher(key)
	if err != nil {
		buslog.GSLog.Error(logPrefix+"%v", err.Error())
		return nil, err
	}
	bs := block.BlockSize()
	if len(origData)%bs != 0 {
		buslog.GSLog.Error(logPrefix + "wrong padding")
		return nil, errors.New("wrong padding")
	}
	out := make([]byte, len(origData))
	dst := out
	for len(origData) > 0 {
		block.Encrypt(dst, origData[:bs])
		origData = origData[bs:]
		dst = dst[bs:]
	}
	return out, nil
}

//Des解密
func decrypt(logPrefix string, crypted, key []byte) ([]byte, error) {
	if len(crypted) < 1 || len(key) < 1 {
		buslog.GSLog.Error(logPrefix + "wrong data or key")
		return nil, errors.New("wrong data or key")
	}
	block, err := des.NewCipher(key)
	if err != nil {
		return nil, err
	}
	out := make([]byte, len(crypted))
	dst := out
	bs := block.BlockSize()
	if len(crypted)%bs != 0 {
		buslog.GSLog.Error(logPrefix + "wrong crypted size")
		return nil, errors.New("wrong crypted size")
	}

	for len(crypted) > 0 {
		block.Decrypt(dst, crypted[:bs])
		crypted = crypted[bs:]
		dst = dst[bs:]
	}

	return out, nil
}

//[golang ECB 3DES Encrypt]
func TripleEcbDesEncrypt(logPrefix string, origData, key []byte) (string, error) {
	tkey := make([]byte, 24, 24)
	copy(tkey, key)
	k1 := tkey[:8]
	k2 := tkey[8:16]
	k3 := tkey[16:]
	block, err := des.NewCipher(k1)
	if err != nil {
		buslog.GSLog.Error(logPrefix+"%v", err.Error())
		return "", err
	}
	bs := block.BlockSize()
	origData = PKCS5Padding(origData, bs)

	buf1, err := encrypt(logPrefix, origData, k1)
	if err != nil {
		buslog.GSLog.Error(logPrefix+"%v", err.Error())
		return "", err
	}
	buf2, err := decrypt(logPrefix, buf1, k2)
	if err != nil {
		return "", err
	}
	out, err := encrypt(logPrefix, buf2, k3)
	if err != nil {
		buslog.GSLog.Error(logPrefix+"%v", err.Error())
		return "", err
	}
	return base64.StdEncoding.EncodeToString(out), nil
}

//[golang ECB 3DES Decrypt]
func TripleEcbDesDecrypt(logPrefix string, crypted string, key []byte) ([]byte, error) {
	tkey := make([]byte, 24, 24)
	copy(tkey, key)
	k1 := tkey[:8]
	k2 := tkey[8:16]
	k3 := tkey[16:]
	cryptedByte, _ := base64.StdEncoding.DecodeString(crypted)
	buf1, err := decrypt(logPrefix, cryptedByte, k3)
	if err != nil {
		return nil, err
	}
	buf2, err := encrypt(logPrefix, buf1, k2)
	if err != nil {
		return nil, err
	}
	out, err := decrypt(logPrefix, buf2, k1)
	if err != nil {
		return nil, err
	}
	out = PKCS5Unpadding(out)
	return out, nil
}

 

你可能感兴趣的:(加密,安全)