Golang

 

Learn Programming – Encryption & Decryption

Introduction

Encryption and decryption are the processes used to protect sensitive information. Encryption transforms readable data into an unreadable format using a specific algorithm, while decryption reverses the process to restore the original data.

In this program, we will implement a simple encryption and decryption algorithm in the Go programming language. We will use a basic Caesar cipher-like algorithm, where each character in the text is shifted by a fixed number of positions. This will serve as an introduction to encryption techniques.

Objective

The goal of this program is to:

  • Demonstrate the concept of encryption and decryption.
  • Implement a simple Caesar cipher in Go.
  • Allow the user to encrypt and decrypt messages by specifying a shift value.

Code


package main

import (
	"fmt"
)

// Encrypt function that shifts characters by the given key
func Encrypt(text string, key int) string {
	encryptedText := ""
	for _, char := range text {
		// Check if character is a letter
		if char >= 'a' && char <= 'z' { encryptedText += string(((char-'a'+rune(key))%26)+'a') } else if char >= 'A' && char <= 'Z' { encryptedText += string(((char-'A'+rune(key))%26)+'A') } else { // Non-alphabetic characters are not changed encryptedText += string(char) } } return encryptedText } // Decrypt function that reverses the encryption by shifting in the opposite direction func Decrypt(text string, key int) string { decryptedText := "" for _, char := range text { // Check if character is a letter if char >= 'a' && char <= 'z' { decryptedText += string(((char-'a'-rune(key)+26)%26)+'a') } else if char >= 'A' && char <= 'Z' {
			decryptedText += string(((char-'A'-rune(key)+26)%26)+'A')
		} else {
			// Non-alphabetic characters are not changed
			decryptedText += string(char)
		}
	}
	return decryptedText
}

func main() {
	// Sample text and key for encryption and decryption
	var text string
	var key int

	// User input
	fmt.Println("Enter the text to encrypt:")
	fmt.Scanln(&text)

	fmt.Println("Enter the shift key (number of positions to shift):")
	fmt.Scanln(&key)

	// Encrypt the text
	encryptedText := Encrypt(text, key)
	fmt.Println("\nEncrypted text:", encryptedText)

	// Decrypt the text back to original
	decryptedText := Decrypt(encryptedText, key)
	fmt.Println("\nDecrypted text:", decryptedText)
}
            

Explanation

This Go program performs basic encryption and decryption using a Caesar cipher. The program has two key functions: Encrypt and Decrypt.

  • Encrypt function: This function takes a text and a key (number of positions to shift). It loops through each character of the text. If the character is a letter (either uppercase or lowercase), it shifts it by the key. Non-alphabetic characters (such as spaces, punctuation) are left unchanged.
  • Decrypt function: This function reverses the process of encryption by shifting characters in the opposite direction using the same key. It also keeps non-alphabetic characters intact.

The main function is where the program starts. It prompts the user for the text to encrypt and the key for the shift. It then prints the encrypted text and decrypts it back to show the original message.

How to Run the Program

Follow these steps to run the program:

    1. Install the Go programming language on your computer if you haven’t already. You can download it from the official Go website.
    2. Save the program code in a file named encryption.go.
    3. Open a terminal or command prompt and navigate to the directory where the encryption.go file is saved.
    4. Run the following command to execute the program:
go run encryption.go
  1. The program will prompt you to enter the text to encrypt and the shift key. After entering the details, it will display the encrypted and decrypted text.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)