-
-
Save elvuel/247db505a32baf58deeb to your computer and use it in GitHub Desktop.
des ecb mode in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/des" | |
"errors" | |
) | |
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 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.TrimFunc(origData, | |
func(r rune) bool { | |
return r == rune(0) | |
}) | |
} | |
func DesEncrypt(src, key []byte) ([]byte, error) { | |
block, err := des.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
bs := block.BlockSize() | |
src = ZeroPadding(src, bs) | |
// src = PKCS5Padding(src, bs) | |
if len(src)%bs != 0 { | |
return nil, errors.New("Need a multiple of the blocksize") | |
} | |
out := make([]byte, len(src)) | |
dst := out | |
for len(src) > 0 { | |
block.Encrypt(dst, src[:bs]) | |
src = src[bs:] | |
dst = dst[bs:] | |
} | |
return out, nil | |
} | |
func DesDecrypt(src, key []byte) ([]byte, error) { | |
block, err := des.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
out := make([]byte, len(src)) | |
dst := out | |
bs := block.BlockSize() | |
if len(src)%bs != 0 { | |
return nil, errors.New("crypto/cipher: input not full blocks") | |
} | |
for len(src) > 0 { | |
block.Decrypt(dst, src[:bs]) | |
src = src[bs:] | |
dst = dst[bs:] | |
} | |
out = ZeroUnPadding(out) | |
// out = PKCS5UnPadding(out) | |
return out, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go test . -v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment