Skip to content

Instantly share code, notes, and snippets.

@adelcambre
Created February 5, 2013 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelcambre/4718414 to your computer and use it in GitHub Desktop.
Save adelcambre/4718414 to your computer and use it in GitHub Desktop.

Recursive encryption/decryption

The benefit of this approach is simple: The original passphrase can't be brute-forced from only the encrypted result without trying all the recusrive encryptions and knowing how many recusion levels were used during encryption.

Test

echo hello | red e world | tee hello-world.enc.txt | red d world

Now the hello-world.enc.txt file is "hello" encrypted recursively using sha512-recursive digests of the "world" passphrase.

Explanation

echo hello | red e world 3

results in essentially:

hello-world.enc.txt = e(e(e("hello",sha("world")),sha(sha("world"))),sha(sha(sha("world"))))

which can be decrypted using:

cat hello-world.enc.txt | red d world 3

results in essentially:

"hello" = d(d(d(hello-world.enc.txt,sha(sha(sha("world")))),sha(sha("world"))),sha("world"))

More

This technique is strengthened further by changing the cipher algorithms based on the passphrase. It feels kinda like salting each iteration.

#!/bin/bash
# GitHub, this file isn't JavaScript. See the above line.
# Recursively compress stdin based on a recursive sha512'd passphrase.
# Direction: e d
c=${1:-e}
# The number of recursive steps.
n=${2:-10}
p=${3}
[ "${p}" ] || {
read -s -p 'Passphrase: ' p >/dev/tty </dev/tty
[ "${p}" ] || exit 1
printf '\e[12D\e[2K' >/dev/tty
cat | ${0} ${c} ${n} "${p}"
exit $?
}
# For now, restrict the maximum iterations.
((n>100)) && n=100
((n<=0)) && {
cat
exit 0
}
# The passphrase salted with the iteration, digested on each iteration.
p=$(echo "${n}${p}" | shasum5.12 | awk '{print $1}')
# Cycle ciphers based on password checksum.
ciphers=(bf aes-256 des-ede3 cast)
w=$(echo ${p} | sum | awk -v c=${#ciphers[*]} '{print $1 % c}')
cipher=${ciphers[w]}-cbc
# For the last iteration, use base64.
((n==1)) && ascii='-a' || ascii=
exec 2>/dev/null
set -o pipefail
[ ${c} = 'd' ] && {
# When decrypting, push all the digested passwords before starting to decrypt.
${0} ${c} $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+Jx8uP3
txFjJb0xx4kQHlVkkxpb7KSA/nyU5+gay6e6VN1OhWoKHYsg6EWWJiHZ0RW/2LBG
W0PJV7KyusyNq4v6ADYVqMq92+R9P1CH8vxxS+ZkDpMyKknitoYt4/auPahLEJeu
L/UVxETLkdyxzMsWLGYHOAsQ6lq6s/l56Uqq9/vlZPKHEL8/rjNMb44hd4opQB/h
v1plS6+GNr6U04zFUatoZQ7RPBe/JcA8BiZgOY0TiVw=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment