Monday, April 18, 2011

Simple encryption script

I first googled for such a script, when I needed one. I didn't want the complex encryption logic that were offered. I wanted a simple script, which served my purpose. When my search went in vain, I thought of writing one myself. Its pretty self explanatory. You would need to watch out for the last few ASCII characters. The logic is not very strong but will serve the purpose.

Also the encryption logic can be your choice. The script can be made execute only to conceal the logic from other users. Here goes...


#!/usr/bin/ksh

# encrypt_string.ksh
#
#
# Change record:
# Author Date Comments
# Gokula Kandaswamy 09/23/2010 Created V1
# **************************************************************************************
# Purpose: This script applies a simple encryption to a string. It has the capability to also decrypt the same string
#
# # Command line parameters to be passed
# first: The string which has to be encrypted or decrypted
# second: The action needed on the string i.e. whether it should encrypted or decrypted.
# Valid values="encrypt" for Encryption and "decrypt" for decryption
# **************************************************************************************
# Usage for encryption: ./encrypt_string.ksh encrypt
# Usage for decryption: ./encrypt_string.ksh '' decrypt

MyString=$1
Type=$2

StrChange=''
i=1
k=1

while (( i <= ${#MyString} ))
do

[[ $k -eq 4 ]] && k=1

char=$(expr substr "$MyString" $i 1)
chr=`echo -n $char | od -A n -t u1`
chr=`echo $chr | sed 's/^0//'`
if [ $Type = "encrypt" ] && [ $chr -ne 126 ]
then
(( chr += $k ))
elif [ $Type = "decrypt" ] && [ $chr -ne 126 ]
then
(( chr -= $k ))
fi
var=`awk -v char=$chr 'BEGIN { printf "%c\n", char; exit }'`
StrChange=`echo "$StrChange$var"`

(( i += 1 ))
(( k += 1 ))
done

echo $StrChange

# End of Script

No comments: