Skip to content

Instantly share code, notes, and snippets.

@marlonfan
Forked from cuixin/des.go
Last active August 21, 2017 04:48
Show Gist options
  • Save marlonfan/b7b63dad211fff5c581c71416bf1ccd4 to your computer and use it in GitHub Desktop.
Save marlonfan/b7b63dad211fff5c581c71416bf1ccd4 to your computer and use it in GitHub Desktop.
des ecb mode in golang
package main
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/base64"
"fmt"
)
func main() {
// DES 加解密
testDes()
// 3DES加解密
test3Des()
}
func testDes() {
key := []byte("256&#@$M")
result, err := DesEncrypt([]byte("123456"), key)
if err != nil {
panic(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(result))
hexstr := fmt.Sprintf("%X", result)
fmt.Println(hexstr)
origData, err := DesDecrypt(result, key)
if err != nil {
panic(err)
}
fmt.Println("=======")
fmt.Println(string(origData))
}
func test3Des() {
key := []byte("256&#@$Msefiel#fi32lf3e!")
result, err := TripleDesEncrypt([]byte("123456"), key)
if err != nil {
panic(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(result))
origData, err := TripleDesDecrypt(result, key)
if err != nil {
panic(err)
}
fmt.Println(string(origData))
}
func DesEncrypt(origData, key []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
origData = PKCS5Padding(origData, block.BlockSize())
// origData = ZeroPadding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key)
crypted := make([]byte, len(origData))
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
// crypted := 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
}
// 3DES加密
func TripleDesEncrypt(origData, key []byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
origData = PKCS5Padding(origData, block.BlockSize())
// origData = ZeroPadding(origData, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key[:8])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
// 3DES解密
func TripleDesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, key[:8])
origData := make([]byte, len(crypted))
// origData := crypted
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
// origData = ZeroUnPadding(origData)
return origData, nil
}
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
func ZeroUnPadding(origData []byte) []byte {
return bytes.TrimRightFunc(origData, func(r rune) bool {
return r == rune(0)
})
}
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 次
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
package main
import (
"fmt"
"testing"
)
func TestDesEncrypt(t *testing.T) {
key := []byte("5e8487e6")
origtext := []byte("hello world123563332")
erytext, err := DesEncrypt(origtext, key)
if err != nil {
t.Fatal(err)
}
fmt.Printf("%v\n", erytext)
destext, err2 := DesDecrypt(erytext, key)
if err2 != nil {
t.Fatal(err2)
}
fmt.Println(string(destext))
fmt.Println(len(origtext), len(string(destext)))
fmt.Println(string(origtext) == string(destext))
}
go test . -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment