DecryptAES decrypts the given data with the given key
(key, data []byte)
| 66 | |
| 67 | // DecryptAES decrypts the given data with the given key |
| 68 | func DecryptAES(key, data []byte) ([]byte, error) { |
| 69 | // Ensure key is 32 bytes long |
| 70 | key = PadKey(key) |
| 71 | |
| 72 | c, err := aes.NewCipher(key) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
| 77 | gcm, err := cipher.NewGCM(c) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | nonceSize := gcm.NonceSize() |
| 83 | if len(data) < nonceSize { |
| 84 | return nil, errors.Errorf("Data size is smaller than nonce size: %d < %d", len(data), nonceSize) |
| 85 | } |
| 86 | |
| 87 | nonce, ciphertext := data[:nonceSize], data[nonceSize:] |
| 88 | plaintext, err := gcm.Open(nil, nonce, ciphertext, nil) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | |
| 93 | return plaintext, nil |
| 94 | } |