Last active
October 30, 2023 06:52
-
-
Save cuixin/10612934 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
您好,我最近在做des加解密,搜到您的代码。
想问两个问题:
1.用0补位那个方法有什么好处吗?
2.我要加密一个整形,用这种方法有没有什么问题?
能否帮忙解答一下