-
-
Save ZanderZhang/dda8e79c404e9f386a40b73d773fdba5 to your computer and use it in GitHub Desktop.
Revisions
-
cuixin revised this gist
Apr 14, 2014 . 3 changed files with 25 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,6 @@ import ( "bytes" "crypto/des" "errors" ) func PKCS5Padding(ciphertext []byte, blockSize int) []byte { @@ -73,23 +72,3 @@ func DesDecrypt(src, key []byte) ([]byte, error) { // out = PKCS5UnPadding(out) return out, nil } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ go test . -v -
cuixin created this gist
Apr 14, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,95 @@ package main import ( "bytes" "crypto/des" "errors" "testing" ) 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 } 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)) }