Go Language - AES Encryption And Decryption Example

Advanced Encryption Standard is a symmetric encryption algorithm. AES encryption is used by the U.S. for securing sensitive but unclassified material, so we can say it is enough secure. It allows 128 bit, 192 bit and 256-bit encryption. Symmetric encryption is very fast as compared to asymmetric encryption and are used in systems such as database system.

The following illustration highlights how symmetric cryptography works:

Example:

package main

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
)

func main() {

//generate a random 32 byte key
key := GenerateRandomKey()

//your secret text
secret := "This is my password"

//encryption
encrypted := encrypt(secret, key)
fmt.Printf("encrypted data: %s\n", encrypted)

//decryption
decrypted := decrypt(encrypted, key)
fmt.Printf("decrypted data: %s\n", decrypted)
}

//encryption
func encrypt(stringToEncrypt string,
keyString string) (encryptedString string) {

//convert decode it to bytes
key := Decode(keyString)
plaintext := []byte(stringToEncrypt)

//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
CheckError(err)

//Create a new GCM
aesGCM, err := cipher.NewGCM(block)
CheckError(err)

//Create a nonce.
nonce := make([]byte, aesGCM.NonceSize())
_, err = io.ReadFull(rand.Reader, nonce)
CheckError(err)

//Encrypt the data
ciphertext := aesGCM.
Seal(nonce, nonce, plaintext, nil)
return fmt.Sprintf("%x", ciphertext)
}

//decryption
func decrypt(encryptedString string,
keyString string) (decryptedString string) {

key := Decode(keyString)
enc := Decode(encryptedString)

//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
CheckError(err)

//Create a new GCM
aesGCM, err := cipher.NewGCM(block)
CheckError(err)

//Get the nonce size
nonceSize := aesGCM.NonceSize()

//Extract the nonce from the encrypted data
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]

//Decrypt the data
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
CheckError(err)

return fmt.Sprintf("%s", plaintext)
}

//generate a random 32 byte key
func GenerateRandomKey() string {

bytes := make([]byte, 32)
_, err := rand.Read(bytes)
CheckError(err)
key := hex.EncodeToString(bytes)
return key
}

//Convert to bytes
func Decode(text string) []byte {

bytes, err := hex.DecodeString(text)
CheckError(err)
return bytes
}

func CheckError(err error) {
if err != nil {
panic(err)
}
}

Output:

  Author name,
                                                                                Seril

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete