# Golang实现ECB模式3DES算法

agamem · · 1549 次点击 · · 开始浏览

## 基础

3DES
3DES算法就是采用一个长度为24字节的密钥，将密钥分成各8字节的3份子密钥：K1、k2、k3。使用这3个密钥对明文进行加密、解密处理，如下：
E(k,d)、D(k,d)分别表示使用密钥k对数据d进行加密或解密，返回加密或解密后的数据。

3DES加密过程：
`E(k3,D(k2,E(k1,d)))`

3DES解密过程与加密相反：
`D(k1,E(k2,D(k3,d)))`

``````func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
}

length := len(origData)
}``````

## 代码

``````package tripledesecb
import (
"bytes"
"crypto/des"
"errors"
"fmt"
"golang.org/x/crypto/pbkdf2"
)

func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
}

length := len(origData)
}

//Des加密
func encrypt(origData, key []byte) ([]byte, error) {
if len(origData) < 1 || len(key) < 1 {
return nil, errors.New("wrong data or key")
}
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
bs := block.BlockSize()
if len(origData)%bs != 0 {
}
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(crypted, key []byte) ([]byte, error) {
if len(crypted) < 1 || len(key) < 1 {
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 {
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(origData, key []byte) ([]byte, 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 {
return nil, err
}
bs := block.BlockSize()

buf1, err := encrypt(origData, k1)
if err != nil {
return nil, err
}
buf2, err := decrypt(buf1, k2)
if err != nil {
return nil, err
}
out, err := encrypt(buf2, k3)
if err != nil {
return nil, err
}
return out, nil
}

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

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

## 基础

3DES
3DES算法就是采用一个长度为24字节的密钥，将密钥分成各8字节的3份子密钥：K1、k2、k3。使用这3个密钥对明文进行加密、解密处理，如下：
E(k,d)、D(k,d)分别表示使用密钥k对数据d进行加密或解密，返回加密或解密后的数据。

3DES加密过程：
`E(k3,D(k2,E(k1,d)))`

3DES解密过程与加密相反：
`D(k1,E(k2,D(k3,d)))`

``````func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
}

length := len(origData)
}``````

## 代码

``````package tripledesecb
import (
"bytes"
"crypto/des"
"errors"
"fmt"
"golang.org/x/crypto/pbkdf2"
)

func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
}

length := len(origData)
}

//Des加密
func encrypt(origData, key []byte) ([]byte, error) {
if len(origData) < 1 || len(key) < 1 {
return nil, errors.New("wrong data or key")
}
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
bs := block.BlockSize()
if len(origData)%bs != 0 {
}
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(crypted, key []byte) ([]byte, error) {
if len(crypted) < 1 || len(key) < 1 {
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 {
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(origData, key []byte) ([]byte, 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 {
return nil, err
}
bs := block.BlockSize()

buf1, err := encrypt(origData, k1)
if err != nil {
return nil, err
}
buf2, err := decrypt(buf1, k2)
if err != nil {
return nil, err
}
out, err := encrypt(buf2, k3)
if err != nil {
return nil, err
}
return out, nil
}

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