Register
It is currently Tue Oct 21, 2014 12:24 pm

BASH Script for text decoding from a given key table


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sat Jan 05, 2008 10:16 am   

Joined: Sat Jan 05, 2008 9:46 am
Posts: 1
I would like to make a BASH script with which I can decode an encoded text when I have the key character table.
For instance:
I have a file with this encoded text: 8D90908B
I have the following decoding:
8D=r
90=o
8B=t
As you can see the result after the decoding must be the word "root".
My idea is to automate this process with a BASH script which can decode the encoded file.
Unfortunately the only way I see how can I do it is with "sed 's/8D/r/g; s/90/o/g; s/8B/t/g'" which is still convenient for files like the above one.
I want to make it possible to decode the full English alphabet with small and upper case letters and numbers as well, which means 26 small case letters, 26 upper case letters and 10 numbers - general 62 characters. It is quite inconvenient to type something like s/8D/r/g for all these 62 characters, that's why I need a more clever solution.

Thank you in advance for the assistance.


Top
 Profile  
 PostPosted: Thu Jan 10, 2008 9:47 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i personally would run through the string and take little 2 character bites out of it and run it against a case statement, then add the result to the new string...
that's just a rundown of how i would approach it... i'm sure you could write a nice little function to handle that :)


Top
 Profile  
 PostPosted: Fri Jan 11, 2008 5:14 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hi,

Note that your sed option is not going to work as you expect...

inputstream=B8D593

then your first sed would replace like so...

Br593

i strongly suspect that you are going to have to split the input into 2 byte chunks and check against each.
If the 2byte codes are in order then the code can be made short.

A0...BA matches to a...z

sorry but I don't have time to code this right now.


Top
 Profile  
 PostPosted: Sun Jan 20, 2008 12:51 pm   

Joined: Sun Jan 20, 2008 12:46 pm
Posts: 3
Code:
chars="zyxwvutsrqponmlkjihgfedcba"
for i in $(seq 134 159); do
    c[$i]=${chars:$((i-134)):1}
done   

enctext="8D90908B"
textlength=${#enctext}
echo -n "$enctext -->> "
for n in $(seq 1 2 $textlength); do
    hexbyte=${enctext:$n-1:2}
    decbyte=$((0x$hexbyte))
    echo -n ${c[$decbyte+1]}
done   
echo


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Alexa [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP