Created
July 25, 2015 06:40
-
-
Save yrps/92d700b25c46c0be7d55 to your computer and use it in GitHub Desktop.
encrypt an ASCII message with an RSA public key; decrypt with the private key
This file contains 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 | |
if [ "$#" -lt 0 -o "$#" -gt 3 ];then | |
echo -e "\nUsage: decr [<input file>] [<RSA's d>] [<RSA's n>]\n" | |
echo -e 'input file is "ciphermessage" by default' | |
echo -e 'private key is 5783-and-7031 by default\n' | |
exit 1 | |
fi | |
# default private key is 5783-and-7031 | |
PRIVATE=$1 #a.k.a. "d" | |
: "${PRIVATE:=5783}" | |
PRIMESPRODUCT=$2 #a.k.a. "n" | |
: "${PRIMESPRODUCT:=7031}" | |
INFILE=$3 | |
: "${INFILE:=ciphermessage}" | |
decrypt() { | |
local c="$1" d="$2" n="$3" m=1 i | |
for (( i=0; i<d; i++ )); do | |
m=$(( m * c % n )) | |
done | |
# given its ascii value, return a letter | |
printf "\\$(printf '%03o' $m)" | |
} | |
while read -r INCODE | |
do | |
# asc=$( echo "d=$PRIVATE;n=$PRIMESPRODUCT;c=$INCODE;c^d%n" | bc ) | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
# ^^^^^^^^^ | |
# private key cipertext | |
decrypt "$INCODE" "$PRIVATE" "$PRIMESPRODUCT" | |
done < "${INFILE}" | |
echo |
This file contains 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 | |
if [ "$#" -lt 1 -o "$#" -gt 4 ];then | |
echo -e "\nUsage: encr <input file> [<RSA's e>] [<RSA's n>] [<output file>]\n" | |
echo -e 'output file is "ciphermessage" by default' | |
echo -e 'output file receives one character, encrypted, per line\n' | |
echo -e 'public key is 1943-and-7031 by default\n' | |
exit 1 | |
fi | |
INFILE=$( cat "$1" ) | |
# default public key is 1943-and-7031 | |
SOME_RELATIVE_PRIME=$2 #a.k.a. "e" | |
: "${SOME_RELATIVE_PRIME:=1943}" | |
PRIMESPRODUCT=$3 #a.k.a. "n" | |
: "${PRIMESPRODUCT:=7031}" | |
OUTFILE=$4 | |
: "${OUTFILE:=ciphermessage}" | |
len=${#INFILE} | |
> "${OUTFILE}" | |
encrypt() { | |
local m="$1" e="$2" n="$3" c=1 i | |
for (( i=0; i<e; i++ )); do | |
c=$(( c * m % n )) | |
done | |
echo $c | |
} | |
i=0 | |
while [ "$i" -lt "$len" ] | |
do | |
letter=${INFILE:i:1} | |
# given a letter, returns its ascii value | |
# this obscure systax of printf relies on the single quote to work | |
asc=$(printf "%d" "'${letter}") | |
#echo "$letter becomes $asc" | |
#echo "e=$SOME_RELATIVE_PRIME;n=$PRIMESPRODUCT;m=$asc;m^e%n" | bc >> ${OUTFILE} | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
# together, the public key ^^^^^^ | |
# cleartext | |
encrypt "$asc" "$SOME_RELATIVE_PRIME" "$PRIMESPRODUCT" >> "$OUTFILE" | |
let i=i+1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[cooper@sputnik ~]$ time ./decr-bash 216835 278441 /home/common/ciphermessage-cooper
VAP
real 0m22.524s
user 0m20.746s
sys 0m1.543s
[cooper@sputnik ~]$ time ./decr 216835 278441 /home/common/ciphermessage-cooper
VAP
real 3m44.986s
user 3m42.710s
sys 0m0.111s
[cooper@sputnik ~]$ time ./encr-bash tempin 15811 278441 tempout
real 0m1.605s
user 0m1.432s
sys 0m0.155s
[cooper@sputnik ~]$ time ./encr tempin 15811 278441 tempout
real 0m0.668s
user 0m0.631s
sys 0m0.022s