Skip to content

Instantly share code, notes, and snippets.

@Bishwas-py
Created March 24, 2025 03:25
Show Gist options
  • Save Bishwas-py/ed8d89b4b7ffc80615cad6f2d5b0a9c4 to your computer and use it in GitHub Desktop.
Save Bishwas-py/ed8d89b4b7ffc80615cad6f2d5b0a9c4 to your computer and use it in GitHub Desktop.
package main
import (
"slices"
"strings"
)
var alphabets = []string{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z",
}
var specials = []string{
"`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+",
"[", "]", "{", "}", "\\", "|", ";", ":", "'", "\"", ",", "<", ".", ">", "/", "?",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
}
func shiftAlphabets(char string, shiftAmount int) string {
lowerChar := strings.ToLower(char)
isLowerCase := lowerChar == char
index := slices.Index(alphabets, lowerChar)
alphabetLength := len(alphabets)
shiftedIndex := index + shiftAmount
newIndex := (shiftedIndex) % alphabetLength
if newIndex < 0 {
newIndex += alphabetLength
}
newAlphaChar := alphabets[newIndex]
if isLowerCase {
return newAlphaChar
}
return strings.ToUpper(newAlphaChar)
}
func reverseString(s string) string {
// Convert string to rune slice to handle Unicode correctly
runes := []rune(s)
// Reverse the rune slice
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
// Convert back to string
return string(runes)
}
func shiftSpecials(char string, shiftAmount int) string {
index := slices.Index(specials, char)
specialLength := len(specials)
shiftedIndex := index + shiftAmount
newIndex := (shiftedIndex) % specialLength
if newIndex < 0 {
newIndex += specialLength
}
newSpChar := specials[newIndex]
return newSpChar
}
func encrypt(text string, sA int) string {
e := ""
for _, j := range text {
char := string(j)
if slices.Contains(alphabets, char) {
e += shiftAlphabets(char, sA)
} else {
e += shiftSpecials(char, sA)
}
}
return reverseString(e)
}
func decrypt(text string, sA int) string {
d := ""
for _, j := range text {
char := string(j)
if slices.Contains(alphabets, char) {
d += shiftAlphabets(char, -sA)
} else {
d += shiftSpecials(char, -sA)
}
}
return reverseString(d)
}
func main() {
password := "applie##"
sA := 2
enc := encrypt(password, sA)
println(enc)
println(decrypt(enc, sA))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment