Skip to content

Instantly share code, notes, and snippets.

@jedahan
Created March 14, 2017 18:40
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 jedahan/0e4222189e277535c56fc50b44a37632 to your computer and use it in GitHub Desktop.
Save jedahan/0e4222189e277535c56fc50b44a37632 to your computer and use it in GitHub Desktop.
#!env zsh
set -e
function showhelp {
echo "$0 <encryption_key.bin> <encrypted_amiibo.bin> [seven_byte_uid] [--random]"
exit
}
function checksum {
[[ $#1 -eq 3 ]] || [[ $#1 -eq 4 ]] || showhelp
echo $(( $(($1[1] ^ $1[2])) ^ $(($1[3] ^ ${1[4]:-88)) ))
}
encryption_key=$1
encrypted_amiibo_bin=$2
uid_prefix=$3[1]$3[2]$3[3]
uid_suffix=$3[4]$3[5]$3[6]$3[7]
test -f $encryption_key || showhelp
test -f $encrypted_amiibo_bin || showhelp
tmpdir=$(mktemp -d awesomebo.XXXXXX)
echo "Decrypting file in $tmpdir..."
decrypted_amiibo_bin=${tmpdir}/${encrypted_amiibo_bin}.decrypted
amiitool -d -k $encryption_key -i $encrypted_amiibo_bin -o $decrypted_amiibo_bin
echo "Patching file..."
patched_decrypted_amiibo_bin=${decrypted_amiibo_bin}.patched
cp $decrypted_amiibo_bin $patched_decrypted_amiibo_bin
uid=$uid_prefix$(checksum $uid_prefix)$uid_suffix
echo "000468: $uid_prefix$uid_suffix" | xxd -r - $patched_decrypted_amiibo_bin
echo "00: $(checksum $uid_suffix)" | xxd -r - $patched_decrypted_amiibo_bin
echo "Encrypting file..."
patched_encrypted_amiibo_bin=${patched_decryped_amiibo_bin}.encrypted
amiitool -e -k $encryption_key -i $patched_decrypted_amiibo_bin -o $patched_encrypted_amiibo_bin
echo "Padding file..."
patched_encrypted_padded_amiibo_bin=${patched_encrypted_amiibo_bin}.padded
echo "00: 00040402010011030100000000808000" | xxd -r - $patched_encrypted_padded_amiibo_bin
dd of=$patched_encrypted_padded_amiibo_bin oflag=append bs=4 count=8 conv=notrunc
dd of=$patched_encrypted_padded_amiibo_bin oflag=append if=$patched_encrypted_amiibo_bin conv=notrunc
dd of=$patched_encrypted_padded_amiibo_bin oflag=append bs=4 count=108 conv=notrunc
uid_prefix=$(xxd -s 0x68 -l 3 -ps $patched_encrypted_padded_amiibo_bin)
uid_suffix=$(xxd -s 0x6C -l 4 -ps $patched_encrypted_padded_amiibo_bin)
eml=$uid_prefix$uid_suffix.eml
xxd -ps -c 8 $patched_encrypted_padded_amiibo_bin > $eml
echo "Saved as $eml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment