Decrypt decrypts data with given password by AES-128-CBC PKCS#7. iv will be read from the head of raw.
(in, password []byte)
| 61 | // Decrypt decrypts data with given password by AES-128-CBC PKCS#7. iv will be read from |
| 62 | // the head of raw. |
| 63 | func Decrypt(in, password []byte) (out []byte, err error) { |
| 64 | keyE := symmetric.KeyDerivation(password, salt[:])[:16] |
| 65 | // IV + padded cipher data == (n + 1 + 1) * aes.BlockSize |
| 66 | if len(in)%aes.BlockSize != 0 || len(in)/aes.BlockSize < 2 { |
| 67 | return nil, errors.New("cipher data size not match") |
| 68 | } |
| 69 | |
| 70 | // read IV |
| 71 | iv := in[:aes.BlockSize] |
| 72 | |
| 73 | // start decryption, as keyE and iv are generated properly, there should |
| 74 | // not be any error |
| 75 | block, _ := aes.NewCipher(keyE) |
| 76 | |
| 77 | mode := cipher.NewCBCDecrypter(block, iv) |
| 78 | // same length as cipher data |
| 79 | plainData := make([]byte, len(in)-aes.BlockSize) |
| 80 | mode.CryptBlocks(plainData, in[aes.BlockSize:]) |
| 81 | |
| 82 | return crypto.RemovePKCSPadding(plainData) |
| 83 | } |