Skip to content

Commit 9455fd4

Browse files
committed
change page of layout
1 parent 793f633 commit 9455fd4

File tree

15 files changed

+1626
-0
lines changed

15 files changed

+1626
-0
lines changed

Encryption/aes/aes.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package aes
2+
3+
import (
4+
"bytes"
5+
"crypto/aes"
6+
"crypto/cipher"
7+
"encoding/base64"
8+
"strings"
9+
"errors"
10+
)
11+
12+
func getKey(key string) []byte {
13+
keyLen := len(key)
14+
if keyLen < 16 {
15+
panic("res key 长度不能小于16")
16+
}
17+
arrKey := []byte(key)
18+
if keyLen >= 32 {
19+
//取前32个字节
20+
return arrKey[:32]
21+
}
22+
if keyLen >= 24 {
23+
//取前24个字节
24+
return arrKey[:24]
25+
}
26+
//取前16个字节
27+
return arrKey[:16]
28+
}
29+
30+
31+
func Base64UrlSafeEncode(source []byte) string {
32+
// Base64 Url Safe is the same as Base64 but does not contain '/' and '+' (replaced by '_' and '-') and trailing '=' are removed.
33+
bytearr := base64.StdEncoding.EncodeToString(source)
34+
safeurl := strings.Replace(string(bytearr), "/", "_", -1)
35+
safeurl = strings.Replace(safeurl, "+", "-", -1)
36+
safeurl = strings.Replace(safeurl, "=", "", -1)
37+
return safeurl
38+
}
39+
40+
func AesDecrypt(msg, k string) (string, error) {
41+
42+
key := getKey(k)
43+
44+
crypted, _ := base64.StdEncoding.DecodeString(msg)
45+
46+
block, err := aes.NewCipher([]byte(key))
47+
if err != nil {
48+
return "", err
49+
}
50+
blockMode := NewECBDecrypter(block)
51+
origData := make([]byte, len(crypted))
52+
blockMode.CryptBlocks(origData, crypted)
53+
origData = PKCS5UnPadding(origData)
54+
//fmt.Println("source is :", origData, string(origData))
55+
return string(origData), nil
56+
}
57+
58+
func AesEncrypt(src, k string) (string, error) {
59+
60+
key := getKey(k)
61+
62+
block, err := aes.NewCipher([]byte(key))
63+
if err != nil {
64+
return "", err
65+
}
66+
if src == "" {
67+
return "", errors.New("Encrypt Msg Is nul")
68+
}
69+
ecb := NewECBEncrypter(block)
70+
content := []byte(src)
71+
content = PKCS5Padding(content, block.BlockSize())
72+
crypted := make([]byte, len(content))
73+
ecb.CryptBlocks(crypted, content)
74+
75+
return base64.StdEncoding.EncodeToString(crypted), nil
76+
}
77+
78+
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
79+
padding := blockSize - len(ciphertext)%blockSize
80+
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
81+
return append(ciphertext, padtext...)
82+
}
83+
84+
func PKCS5UnPadding(origData []byte) []byte {
85+
length := len(origData)
86+
// 去掉最后一个字节 unpadding 次
87+
unpadding := int(origData[length-1])
88+
return origData[:(length - unpadding)]
89+
}
90+
91+
type ecb struct {
92+
b cipher.Block
93+
blockSize int
94+
}
95+
96+
func newECB(b cipher.Block) *ecb {
97+
return &ecb{
98+
b: b,
99+
blockSize: b.BlockSize(),
100+
}
101+
}
102+
103+
type ecbEncrypter ecb
104+
105+
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
106+
// mode, using the given Block.
107+
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
108+
return (*ecbEncrypter)(newECB(b))
109+
}
110+
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
111+
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
112+
if len(src)%x.blockSize != 0 {
113+
panic("crypto/cipher: input not full blocks")
114+
}
115+
if len(dst) < len(src) {
116+
panic("crypto/cipher: output smaller than input")
117+
}
118+
for len(src) > 0 {
119+
x.b.Encrypt(dst, src[:x.blockSize])
120+
src = src[x.blockSize:]
121+
dst = dst[x.blockSize:]
122+
}
123+
}
124+
125+
type ecbDecrypter ecb
126+
127+
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
128+
// mode, using the given Block.
129+
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
130+
return (*ecbDecrypter)(newECB(b))
131+
}
132+
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
133+
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
134+
if len(src)%x.blockSize != 0 {
135+
panic("crypto/cipher: input not full blocks")
136+
}
137+
if len(dst) < len(src) {
138+
panic("crypto/cipher: output smaller than input")
139+
}
140+
for len(src) > 0 {
141+
x.b.Decrypt(dst, src[:x.blockSize])
142+
src = src[x.blockSize:]
143+
dst = dst[x.blockSize:]
144+
}
145+
}

Encryption/des/des.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package des
2+
3+
import (
4+
"bytes"
5+
"crypto/cipher"
6+
"crypto/des"
7+
)
8+
9+
func DesEncrypt(msg string, key string, iv string) (string, error) {
10+
11+
origData := []byte(msg)
12+
block, err := des.NewCipher([]byte(key))
13+
if err != nil {
14+
return "", err
15+
}
16+
origData = PKCS5Padding(origData, block.BlockSize())
17+
// origData = ZeroPadding(origData, block.BlockSize())
18+
blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
19+
crypted := make([]byte, len(origData))
20+
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
21+
// crypted := origData
22+
blockMode.CryptBlocks(crypted, origData)
23+
return string(crypted), nil
24+
}
25+
26+
func DesDecrypt(msg string, key string, iv string) (string, error) {
27+
28+
crypted := []byte(msg)
29+
block, err := des.NewCipher([]byte(key))
30+
if err != nil {
31+
return "", err
32+
}
33+
blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
34+
origData := make([]byte, len(crypted))
35+
// origData := crypted
36+
blockMode.CryptBlocks(origData, crypted)
37+
origData = PKCS5UnPadding(origData)
38+
// origData = ZeroUnPadding(origData)
39+
return string(origData), nil
40+
}
41+
42+
/*
43+
DES/ECB/PKCS5Padding 加密
44+
*/
45+
func DesEncryptECB(msg string, key string) (string, error) {
46+
47+
origData := []byte(msg)
48+
49+
block, err := des.NewCipher([]byte(key))
50+
if err != nil {
51+
return "", err
52+
}
53+
bs := block.BlockSize()
54+
origData = PKCS5Padding(origData, bs)
55+
if len(origData)%bs != 0 {
56+
return "", err
57+
}
58+
crypted := make([]byte, len(origData))
59+
dst := crypted
60+
for len(origData) > 0 {
61+
block.Encrypt(dst, origData[:bs])
62+
origData = origData[bs:]
63+
dst = dst[bs:]
64+
}
65+
66+
return string(crypted), nil
67+
}
68+
69+
/*
70+
DES/ECB/PKCS5Padding 解密
71+
*/
72+
func DesDecryptECB(msg string, key string) (string, error) {
73+
74+
crypted := []byte(msg)
75+
76+
block, err := des.NewCipher([]byte(key))
77+
if err != nil {
78+
return "", err
79+
}
80+
bs := block.BlockSize()
81+
if len(crypted)%bs != 0 {
82+
return "", err
83+
}
84+
origData := make([]byte, len(crypted))
85+
dst := origData
86+
for len(crypted) > 0 {
87+
block.Decrypt(dst, crypted[:bs])
88+
crypted = crypted[bs:]
89+
dst = dst[bs:]
90+
}
91+
origData = PKCS5UnPadding(origData)
92+
return string(origData), nil
93+
}
94+
95+
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
96+
padding := blockSize - len(ciphertext)%blockSize
97+
padtext := bytes.Repeat([]byte{0}, padding)
98+
return append(ciphertext, padtext...)
99+
}
100+
101+
func ZeroUnPadding(origData []byte) []byte {
102+
return bytes.TrimRightFunc(origData, func(r rune) bool {
103+
return r == rune(0)
104+
})
105+
}
106+
107+
// 3DES加密
108+
func TripleDesEncrypt(msg string, key string, iv string) (string, error) {
109+
110+
origData := []byte(msg)
111+
112+
block, err := des.NewTripleDESCipher([]byte(key))
113+
if err != nil {
114+
return "", err
115+
}
116+
origData = PKCS5Padding(origData, block.BlockSize())
117+
// origData = ZeroPadding(origData, block.BlockSize())
118+
//blockMode := cipher.NewCBCEncrypter(block, key[:8])
119+
blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
120+
crypted := make([]byte, len(origData))
121+
blockMode.CryptBlocks(crypted, origData)
122+
return string(crypted), nil
123+
}
124+
125+
// 3DES解密
126+
func TripleDesDecrypt(msg string, key string, iv string) (string, error) {
127+
128+
crypted := []byte(msg)
129+
block, err := des.NewTripleDESCipher([]byte(key))
130+
if err != nil {
131+
return "", err
132+
}
133+
//blockMode := cipher.NewCBCDecrypter(block, key[:8])
134+
blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
135+
origData := make([]byte, len(crypted))
136+
// origData := crypted
137+
blockMode.CryptBlocks(origData, crypted)
138+
origData = PKCS5UnPadding(origData)
139+
// origData = ZeroUnPadding(origData)
140+
return string(origData), nil
141+
}
142+
143+
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
144+
padding := blockSize - len(ciphertext)%blockSize
145+
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
146+
return append(ciphertext, padtext...)
147+
}
148+
149+
func PKCS5UnPadding(origData []byte) []byte {
150+
length := len(origData)
151+
// 去掉最后一个字节 unpadding 次
152+
unpadding := int(origData[length-1])
153+
return origData[:(length - unpadding)]
154+
}

0 commit comments

Comments
 (0)