How to Encrypt and Decrypt a String in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to Encrypt and Decrypt a String using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)
func main() {
// Define the plaintext message to encrypt
message := "Hello, World! Welcome to JavaFRM."
// Generate a random 32-byte encryption key
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
panic(err)
}
// Encrypt the message using AES-256 encryption
ciphertext, err := encrypt(key, message)
if err != nil {
panic(err)
}
// Print the encrypted message (base64 encoded)
fmt.Println("Encrypted message:", base64.StdEncoding.EncodeToString(ciphertext))
// Decrypt the message using the same key
decrypted, err := decrypt(key, ciphertext)
if err != nil {
panic(err)
}
// Print the decrypted message
fmt.Println("Decrypted message:", decrypted)
}
func encrypt(key []byte, message string) ([]byte, error) {
// Convert the plaintext message to a byte array
plaintext := []byte(message)
// Generate a new AES cipher using the 256-bit key
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Generate a random 16-byte initialization vector
iv := make([]byte, aes.BlockSize)
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
return nil, err
}
// Encrypt the plaintext message using AES-256 encryption
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
copy(ciphertext[:aes.BlockSize], iv)
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, nil
}
func decrypt(key []byte, ciphertext []byte) (string, error) {
// Extract the initialization vector from the ciphertext
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
// Generate a new AES cipher using the 256-bit key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// Decrypt the ciphertext message using AES-256 encryption
plaintext := make([]byte, len(ciphertext))
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
return string(plaintext), nil
}
Below is the explanation for the program:
The program first defines a plaintext message to encrypt.
Next, the program generates a random 32-byte encryption key using the rand.Read() function.
The program then encrypts the plaintext message using the AES-256 encryption algorithm, and prints the encrypted message as a base64-encoded string.
The program then decrypts the encrypted message using the same key, and prints the decrypted message.
The encrypt() function takes a key and a plaintext message as input, and returns the encrypted message as a byte array.
The function first converts the plaintext message to a byte array. It then generates a new AES cipher using the 256-bit key, and generates a random 16-byte initialization vector.
The function encrypts the plaintext message using AES-256 encryption, and returns the ciphertext message as a byte array.
The decrypt() function takes a key and a ciphertext message as input, and returns the decrypted message as a string.
The function first extracts the initialization vector from the ciphertext message. It then generates a new AES cipher
#go #goprogramming #golang #golangtutorial #golanguage