Last active
August 29, 2015 14:19
-
-
Save mullikine/56d08561af7313ec541c to your computer and use it in GitHub Desktop.
Create self-decrypting encrypted files using your ssh public key. The self decrypting file will only decrypt if you have the private key. Only works on small files like short messages.
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 [[ "$#" == 1 ]]; then | |
PUBKEYFILE="${HOME}/.ssh/id_rsa.pub" | |
INFILE="$1" | |
OUTFILE="${1}.sh" | |
elif [[ "$#" == 2 ]]; then | |
PUBKEYFILE="${HOME}/.ssh/id_rsa.pub" | |
INFILE="$1" | |
OUTFILE="$2" | |
elif [[ "$#" == 3 ]]; then | |
PUBKEYFILE="$1" | |
INFILE="$2" | |
OUTFILE="$3" | |
else | |
PUBKEYFILE="--help" | |
fi | |
[ "${PUBKEYFILE}" == "--help" ] && exec echo "Usage: makesmsg.sh [ <ssh rsa public key file> ] <infile> [ <outfile.sh> ]" | |
( | |
cat <<END | |
#!/bin/bash | |
openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in <( | |
END | |
echo "echo \"$(openssl enc -base64 -in <(openssl rsautl -encrypt -inkey <( | |
php_cwd=`/usr/bin/env php <<EOF | |
<?php | |
echo openssh2pem("$PUBKEYFILE"); | |
function len(\\$s) | |
{ | |
\\$len = strlen(\\$s); | |
if (\\$len < 0x80) { | |
return chr(\\$len); | |
} | |
\\$data = dechex(\\$len); | |
\\$data = pack('H*', (strlen(\\$data) & 1 ? '0' : '') . \\$data); | |
return chr(strlen(\\$data) | 0x80) . \\$data; | |
} | |
function openssh2pem(\\$file) | |
{ | |
list(,\\$data) = explode(' ', trim(file_get_contents(\\$file)), 3); | |
\\$data = base64_decode(\\$data); | |
list(,\\$alg_len) = unpack('N', substr(\\$data, 0, 4)); | |
\\$alg = substr(\\$data, 4, \\$alg_len); | |
if (\\$alg !== 'ssh-rsa') { | |
return FALSE; | |
} | |
list(,\\$e_len) = unpack('N', substr(\\$data, 4 + strlen(\\$alg), 4)); | |
\\$e = substr(\\$data, 4 + strlen(\\$alg) + 4, \\$e_len); | |
list(,\\$n_len) = unpack('N', substr(\\$data, 4 + strlen(\\$alg) + 4 + strlen(\\$e), 4)); | |
\\$n = substr(\\$data, 4 + strlen(\\$alg) + 4 + strlen(\\$e) + 4, \\$n_len); | |
\\$algid = pack('H*', '06092a864886f70d0101010500'); // algorithm identifier (id, null) | |
\\$algid = pack('Ca*a*', 0x30, len(\\$algid), \\$algid); // wrap it into sequence | |
\\$data = pack('Ca*a*Ca*a*', 0x02, len(\\$n), \\$n, 0x02, len(\\$e), \\$e); // numbers | |
\\$data = pack('Ca*a*', 0x30, len(\\$data), \\$data); // wrap it into sequence | |
\\$data = "\x00" . \\$data; // don't know why, but needed | |
\\$data = pack('Ca*a*', 0x03, len(\\$data), \\$data); // wrap it into bitstring | |
\\$data = \\$algid . \\$data; // prepend algid | |
\\$data = pack('Ca*a*', 0x30, len(\\$data), \\$data); // wrap it into sequence | |
return "-----BEGIN PUBLIC KEY-----\n" . | |
chunk_split(base64_encode(\\$data), 64, "\n") . | |
"-----END PUBLIC KEY-----\n"; | |
} | |
?> | |
EOF` | |
echo "$php_cwd" | |
) -pubin -in $INFILE))\"|openssl enc -base64 -d | |
) 2>&-" | |
) > "$OUTFILE" | |
if [[ -e "$OUTFILE" ]]; then | |
chmod a+x "$OUTFILE" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment