I am trying to create an One Time Pad for cryptography, But I am stuck in an error
The plan is
- Generate Text
- Generate Key
- Convert text and key from Ascii to binary
- Create Cipher using Key_binary XOR text_binary
#!/bin/bash
#Create Message #1
plaintext=`cat /dev/urandom | tr -dc 0-9 | head -c8`
sleep 1
#Generate Random Sequence
key=`cat /dev/urandom | tr -dc 0-9 | head -c8`
#Ascii to Binary Convertion
key_bin=$(echo $key | perl -lpe '$_=unpack"B*"')
plaintext_bin=$(echo $plaintext | perl -lpe '$_=unpack"B*"')
plaintext_bin_size=${#plaintext_bin}
key_bin_size=${#key_bin}
for ((i=0; i < $plaintext_bin_size; i++)); do
byte=$((${plaintext_bin[$i]} ^ ${key_bin[$i % $key_bin_size]}))
cipher="$cipher($byte)"
done
echo -e "$cipher"
But I get
syntax error: operand expected (error token is "^ ")
Focusing solely on the syntax error ...
As mentioned in comments,
${variable[...]}is an array reference but your variables are not arrays. Net result is you're trying to access array entries that do not exist, ie, you're trying to run anxor / ^operation against blank fields. We can generate the same error with:Assuming the intention is to access the individual digits/positions of the values stored in a variable try
${variable:start:length}, eg:Pulling this into the current code: