-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.go
More file actions
44 lines (39 loc) · 806 Bytes
/
decrypt.go
File metadata and controls
44 lines (39 loc) · 806 Bytes
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
package abdecrypt
import (
"crypto/aes"
"crypto/cipher"
"errors"
"io"
)
// Decrypt decrypts payload of an Android backup using master key mk.
func Decrypt(r io.Reader, mk *MasterKey) (io.Reader, error) {
switch mk.Version {
case 5:
return decryptV5(r, mk.IV, mk.Key)
}
return nil, errors.New("unsupported version")
}
func decryptV5(r io.Reader, iv, key []byte) (io.Reader, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, iv)
buf := make([]byte, mode.BlockSize())
dr, dw := io.Pipe()
go func() {
for {
_, err := io.ReadFull(r, buf)
if err != nil {
dw.CloseWithError(err)
return
}
mode.CryptBlocks(buf, buf)
_, err = dw.Write(buf)
if err != nil {
return
}
}
}()
return dr, nil
}