Decrypt decrypts encrypted text with key (must be valid 32 chars AES key). This method uses AES-256-GCM block cypher mode.
(cipherText string, key string)
| 40 | // |
| 41 | // This method uses AES-256-GCM block cypher mode. |
| 42 | func Decrypt(cipherText string, key string) ([]byte, error) { |
| 43 | block, err := aes.NewCipher([]byte(key)) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | gcm, err := cipher.NewGCM(block) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | nonceSize := gcm.NonceSize() |
| 54 | |
| 55 | cipherByte, err := base64.StdEncoding.DecodeString(cipherText) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | nonce, cipherByteClean := cipherByte[:nonceSize], cipherByte[nonceSize:] |
| 61 | return gcm.Open(nil, nonce, cipherByteClean, nil) |
| 62 | } |
searching dependent graphs…