/
cipher.go
59 lines (54 loc) · 1.38 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
package recovery_passwords
import(
"crypto/aes"
"crypto/cipher"
"encoding/base64"
)
// htttps://github.com/cckuailong/HackChrome/blob/master/utils/cipher.go
func AesGCMDecrypt(crypted,key,nounce []byte)([]byte,error){
block,err := aes.NewCipher(key)
if err != nil{
return nil,err
}
blockMode,_ := cipher.NewGCM(block)
origData,err := blockMode.Open(nil,nounce,crypted,nil)
if err != nil{
return nil,err
}
return origData,nil
}
//https://studygolang.com/articles/14251
//使用PKCS7进行填充,IOS也是7
func PKCS7UnPadding(origData []byte)([]byte){
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length-unpadding)]
}
func AesCBCDncrypt(encryptData,key,iv []byte)([]byte,error){
block,err := aes.NewCipher(key)
if err != nil{
panic(err)
}
blocksize := block.BlockSize()
if len(encryptData) < blocksize{
panic("cipertext too short")
}
if len(encryptData)%blocksize != 0{
panic("ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block,iv)
mode.CryptBlocks(encryptData,encryptData)
encryptData = PKCS7UnPadding(encryptData)
return encryptData,nil
}
func AESDncrypt(rawData string,key []byte,iv []byte)(string,error){
data,err := base64.StdEncoding.DecodeString(rawData)
if err != nil{
return "",err
}
dnData,err := AesCBCDncrypt(data,key,iv)
if err != nil{
return "", err
}
return string(dnData), nil
}