-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.go
185 lines (171 loc) · 5.03 KB
/
cipher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2019 go-crypto Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package gocrypto is a simple encryption and decryption package
package gocrypto
import (
"crypto/cipher"
"fmt"
)
// CipherCrypt is a simple encryption and decryption package
type CipherCrypt struct {
Block cipher.Block
}
// Encrypt encrypts src to dst with cipher & iv, if failed return error
// src the original source bytes
// c the defined cipher type,now support CBC, CFB, OFB, ECB
// ivs the iv for CBC, CFB, OFB mode
// dst the encrypted bytes
func (cc *CipherCrypt) Encrypt(src []byte, c Cipher, ivs ...[]byte) (dst []byte, err error) {
block := cc.Block
data := PKCS7Padding(src, block.BlockSize())
if len(data)%block.BlockSize() != 0 {
return nil, fmt.Errorf("Need a multiple of the blocksize ")
}
switch c {
case CBC:
return cbcEncrypt(block, data, ivs...)
case CFB:
return cfbEncrypt(block, data, ivs...)
case OFB:
return ofbCrypt(block, data, ivs...)
default:
return ecbEncrypt(block, data)
}
}
// EncryptToString encrypts src then encodes data returned to string
// encodeType now support String, HEX, Base64
func (cc *CipherCrypt) EncryptToString(encodeType Encode, src []byte, c Cipher, ivs ...[]byte) (dst string, err error) {
data, err := cc.Encrypt(src, c, ivs...)
if err != nil {
return
}
return EncodeToString(data, encodeType)
}
// Decrypt decrypts src to dst with cipher & iv, if failed return error
// src the original encrypted bytes
// c the defined cipher type,now support CBC, CFB, OFB, ECB
// ivs the iv for CBC, CFB, OFB mode
// dst the decrypted bytes
func (cc *CipherCrypt) Decrypt(src []byte, c Cipher, ivs ...[]byte) (dst []byte, err error) {
block := cc.Block
if len(src)%block.BlockSize() != 0 {
err = fmt.Errorf("input not full blocks")
return
}
switch c {
case CBC:
dst, err = cbcDecrypt(block, src, ivs...)
case CFB:
dst, err = cfbDecrypt(block, src, ivs...)
case OFB:
dst, err = ofbCrypt(block, src, ivs...)
default:
dst, err = ecbDecrypt(block, src)
}
if err != nil {
return nil, err
}
return UnPaddingPKCS7(dst, block.BlockSize()), err
}
// DecryptToString decrypts src then encodes return data to string
// encodeType now support String, HEX, Base64
func (cc *CipherCrypt) DecryptToString(encodeType Encode, src []byte, c Cipher, ivs ...[]byte) (dst string, err error) {
data, err := cc.Decrypt(src, c, ivs...)
if err != nil {
return
}
return EncodeToString(data, encodeType)
}
// ecbEncrypt encrypts data with ecb mode
func ecbEncrypt(block cipher.Block, src []byte) (dst []byte, err error) {
out := make([]byte, len(src))
dst = out
for len(src) > 0 {
block.Encrypt(dst, src[:block.BlockSize()])
src = src[block.BlockSize():]
dst = dst[block.BlockSize():]
}
return out, nil
}
// ecbDecrypt decrypts data with ecb mode
func ecbDecrypt(block cipher.Block, src []byte) (dst []byte, err error) {
dst = make([]byte, len(src))
out := dst
bs := block.BlockSize()
if len(src)%bs != 0 {
err = fmt.Errorf("crypto/cipher: input not full blocks")
return
}
for len(src) > 0 {
block.Decrypt(out, src[:bs])
src = src[bs:]
out = out[bs:]
}
return
}
// cbcEncrypt encrypts data with cbc mode
func cbcEncrypt(block cipher.Block, src []byte, ivs ...[]byte) (dst []byte, err error) {
var iv []byte
if len(ivs) > 0 {
iv = ivs[0]
}
bm := cipher.NewCBCEncrypter(block, iv)
dst = make([]byte, len(src))
bm.CryptBlocks(dst, src)
return
}
// cbcDecrypt decrypts data with cbc mode
func cbcDecrypt(block cipher.Block, src []byte, ivs ...[]byte) (dst []byte, err error) {
var iv []byte
if len(ivs) > 0 {
iv = ivs[0]
}
bm := cipher.NewCBCDecrypter(block, iv)
dst = make([]byte, len(src))
bm.CryptBlocks(dst, src)
return
}
// cfbEncrypt encrypts data with cfb mode
func cfbEncrypt(block cipher.Block, src []byte, ivs ...[]byte) (dst []byte, err error) {
var iv []byte
if len(ivs) > 0 {
iv = ivs[0]
}
bm := cipher.NewCFBEncrypter(block, iv)
dst = make([]byte, len(src))
bm.XORKeyStream(dst, src)
return
}
// cfbDecrypt decrypts data with cfb mode
func cfbDecrypt(block cipher.Block, src []byte, ivs ...[]byte) (dst []byte, err error) {
var iv []byte
if len(ivs) > 0 {
iv = ivs[0]
}
bm := cipher.NewCFBDecrypter(block, iv)
dst = make([]byte, len(src))
bm.XORKeyStream(dst, src)
return
}
// ofbCrypt encrypts or decrypts data with ofb mode
func ofbCrypt(block cipher.Block, src []byte, ivs ...[]byte) (dst []byte, err error) {
var iv []byte
if len(ivs) > 0 {
iv = ivs[0]
}
bm := cipher.NewOFB(block, iv)
dst = make([]byte, len(src))
bm.XORKeyStream(dst, src)
return
}