-
-
Save fengjijiao/4dfe1f68e3dd9d9b7652039fd79751e1 to your computer and use it in GitHub Desktop.
AES-256 GCM Encryption/Decryption Example in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package secure | |
import ( | |
"crypto/aes" | |
"crypto/cipher" | |
"encoding/hex" | |
"fmt" | |
"crypto/sha512" | |
) | |
func CipherMydata(text string) { | |
//cipher key | |
dStr, _ := hex.DecodeString("310bc65ed2bfd049a3877465ebd547210a492db4edf03e9506a3f942135d55e4") | |
plaintext := []byte(text) | |
//******************************* | |
/*This step can be removed or modified. I'm using sha512 to increase length of | |
key and use part of hashed key as nonce and newKey. You can think of some other way or | |
just remove sha512 and use your custom nonce and key as it is. The reason I'm doing this | |
is to make nonce dynamic since key is known to both the parties. | |
*/ | |
hasher := sha512.New() | |
hasher.Write(dStr) | |
out := hex.EncodeToString(hasher.Sum(nil)) | |
newKey, _ := hex.DecodeString(out[:64]) | |
nonce, _ := hex.DecodeString(out[64:(64+24)]) | |
//******************************* | |
aData, _ := hex.DecodeString("03c128d8a500d78e0ee0f8ab4662584b") | |
fmt.Println("Adata:\t 03c128d8a500d78e0ee0f8ab4662584b") | |
block, err := aes.NewCipher(newKey) | |
if err != nil { | |
panic(err.Error()) | |
} | |
aesgcm, err := cipher.NewGCM(block) | |
if err != nil { | |
panic(err.Error()) | |
} | |
cipherText := aesgcm.Seal(nil, nonce, plaintext, aData) | |
fmt.Printf("Ciphered text:\t %x\n", cipherText) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package secure | |
import ( | |
"crypto/aes" | |
"crypto/cipher" | |
"encoding/hex" | |
"fmt" | |
"crypto/sha512" | |
) | |
func DecipherMydata(adata, data string) { | |
dStr, _ := hex.DecodeString("310bc65ed2bfd049a3877465ebd547210a492db4edf03e9506a3f942135d55e4") | |
//******************************* | |
hasher := sha512.New() | |
hasher.Write(dStr) | |
out := hex.EncodeToString(hasher.Sum(nil)) | |
newKey, _ := hex.DecodeString(out[:64]) | |
nonce, _ := hex.DecodeString(out[64:(64+24)]) | |
//******************************* | |
aData, _ := hex.DecodeString(adata) | |
block, err := aes.NewCipher(newKey) | |
if err != nil { | |
panic(err.Error()) | |
} | |
aesgcm, err := cipher.NewGCM(block) | |
if err != nil { | |
panic(err.Error()) | |
} | |
cipherText, _ := hex.DecodeString(data) | |
output, _ := aesgcm.Open(nil, nonce, cipherText, aData) | |
fmt.Println("Data:\t ", string(output)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Applying AES-256 GCM | |
package main | |
import "secure" //check import according to GOPATH | |
func main() { | |
//cipher data | |
text := "password" | |
secure.CipherMydata(text) | |
/* | |
Output: | |
Adata: 03c128d8a500d78e0ee0f8ab4662584b | |
Ciphered text: 50b18db2616d85b9165ea08d6bfa1344f6bfa4c31c05730d | |
*/ | |
//decrypting data | |
secure.DecipherMydata("03c128d8a500d78e0ee0f8ab4662584b", "50b18db2616d85b9165ea08d6bfa1344f6bfa4c31c05730d") | |
/* | |
Output : | |
Data: password | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment