Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to use XOR to encrypt and decrypt a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
)

// XORCipher encrypts or decrypts a string using a key
func XORCipher(input, key string) string {
	output := make([]byte, len(input))
	keyLen := len(key)

	for i := 0; i < len(input); i++ {
		output[i] = input[i] ^ key[i % keyLen]
	}

	return string(output)
}

func main() {
	// Original message
	str := "May the Force be with you." 
	// Key for encryption/decryption
	key := "Obelus"

	// Encrypt the message
	encrypted := XORCipher(str, key)
	fmt.Println("Encrypted:\n", encrypted)

	// Decrypt the message
	decrypted := XORCipher(encrypted, key)
	fmt.Println("Decrypted:\n", decrypted)
}



/*
run:

Encrypted:
L :L *B# *B U &
Decrypted:
 May the Force be with you.

*/

 



answered Aug 16, 2025 by avibootz

Related questions

1 answer 67 views
67 views asked Jul 11, 2025 by avibootz
1 answer 73 views
73 views asked Jul 12, 2025 by avibootz
2 answers 87 views
87 views asked Jul 12, 2025 by avibootz
...