#!/bin/sh # # All rights reserved, copyright (c) 2011, Mitzyuki IMAIZUMI # $Id: cipher,v 1.1 2011/02/09 10:32:09 mitz Exp $ # myname=${0##*/} tmpfile=${TMP:-/tmp}/${myname}.$$ enc="openssl enc -e -aes-128-cbc -in " dec="openssl enc -d -aes-128-cbc -in " trap 'rm -r ${tmpfile}; exit' 0 1 2 3 9 15 if opt=`getopt dep: $*` then set -- ${opt} while [ -n "${1}" ] do case "${1}" in -d ) force=${dec};; -e ) force=${enc};; -p ) pass="-pass pass:${2}" shift;; -- ) shift break;; esac shift done else echo "Usage: ${myname} [-d][-e][-p pass] file[, ... file]" 1>&2 exit 255 fi for i do if [ -f ${i} ] then if [ -z "${force}" ] then # magic number を取得してファイル種別を自動判別 if [ "`dd if=${i} bs=8 count=1 2> /dev/null`" = "Salted__" ] then command=${dec} else command=${enc} fi else command=${force} fi # 暗号化/復号化が成功した場合は元ファイルを置換 ${command} ${i} -out ${tmpfile} ${pass} && mv ${tmpfile} ${i} else echo "${myname}: cannot open ${i} for input" 1>&2 fi done