-
Notifications
You must be signed in to change notification settings - Fork 3
/
caesar.go
48 lines (42 loc) · 1.32 KB
/
caesar.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
package caesarKit
import (
"encoding/base64"
"github.com/richelieu-yang/chimera/v2/src/crypto/base64Kit"
)
// Encrypt 凯撒密码加密.
/*
PS: 仅加密大小写的英文字母.
@param shift 推荐值: (1) [0, 25]
(2) 不推荐使用 26*n(n >= 0),因为这样加密了个寂寞
*/
func Encrypt(plainText string, shift int) (cipherText string) {
cipher := NewCaesarCipher(shift)
cipherText = cipher.Encrypt(plainText)
return
}
func Decrypt(cipherText string, shift int) (plainText string) {
cipher := NewCaesarCipher(shift)
plainText = cipher.Decrypt(cipherText)
return
}
// EncryptWithBase64 凯撒密码加密 + base64 编码.
/*
PS:
(1) base64采用了 base64.RawURLEncoding;
(2) 传参 plainText 可以有中文;
(3) 返回值可以放到 url 中(但不推荐).
*/
func EncryptWithBase64(plainText string, shift int) (cipherText string) {
// base64 编码
base64Str := base64Kit.EncodeStringToString(plainText, base64Kit.WithEncoding(base64.RawURLEncoding))
// 凯撒密码 加密
cipherText = Encrypt(base64Str, shift)
return
}
func DecryptWithBase64(cipherText string, shift int) (plainText string, err error) {
// 凯撒密码 解密
base64Str := Decrypt(cipherText, shift)
// base64 解码
plainText, err = base64Kit.DecodeStringToString(base64Str, base64Kit.WithEncoding(base64.RawURLEncoding))
return
}