Created
June 26, 2023 21:42
-
-
Save CalfCrusher/94bde0bd870dee3ab617ed9c8a7c6306 to your computer and use it in GitHub Desktop.
XOR_string.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to XOR a string with a key | |
xor_string() { | |
local string=$1 | |
local key=$2 | |
local result="" | |
for ((i = 0; i < ${#string}; i++)); do | |
local char=${string:i:1} | |
local ascii_val=$(printf "%d" "'$char") | |
local xor_val=$((ascii_val ^ key)) | |
local xor_char=$(printf "\\$(printf '%03o' "$xor_val")") | |
result+="$xor_char" | |
done | |
echo "$result" | |
} | |
# Usage example | |
input_string="HELLO" | |
xor_key=50 | |
encrypted_string=$(xor_string "$input_string" $xor_key) | |
echo "Encrypted: $encrypted_string" | |
decrypted_string=$(xor_string "$encrypted_string" $xor_key) | |
echo "Decrypted: $decrypted_string" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment