/
cipher.go
175 lines (161 loc) · 4.71 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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package encryption
import (
"crypto/cipher"
"fmt"
)
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
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)
}
return UnPaddingPKCS7(dst), 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
}