Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Encrypt/Decrypt: possible? 2

Status
Not open for further replies.

ivanmcp

Technical User
Apr 25, 2003
14
GB
Hi all,

How easy or difficult would it be to write a plain text encryption/decryption shell script to a most basic level?

Text would be stored in a file, with ability to produce an encrypted file output.

Decryption should restore original plain text.

Is this possible or do I need 3rd party utility?

I
 
I am not sure if this is the basic level,
However this is a script I wrote some time ago to store passwords in for many unix boxes I had to work with.
If you have questions about it post them here and I’ll check back
Doug


#!/usr/bin/ksh

A=/$HOME/bin # Location of this file
B=/$HOME/textfiles/ # Location to store the encryped file ( can be the same location)
C=zz.crypt # The name of the encrypted file
D=zz.txt # The name of the temporary text file to be encrypted

if [ $1 = help ]
then
clear
echo
echo &quot;Usage: pass <condition> &quot;
echo
echo &quot;Possible conditions are:&quot;
echo
echo &quot;help shows this text&quot;
echo &quot;new First time use sets up a new encrypted file for you&quot;
echo &quot;change To make changes to the existing encrypted file&quot;
echo &quot;<blank> Shows you the entire encrypted file&quot;
echo &quot;<any other word> Shows you just that line of the encrypted file&quot;
echo
echo &quot;Before using this program you WILL need to edit this file and change VARIABLES A & B&quot;

echo
echo &quot;For first time use you will be in VI editor, when you save that file you will&quot;
echo &quot;be prompted for a Key. This will be your password for use in all future views.&quot;
echo
echo &quot;For view purposes you will be prompted for this Key. Possible views are either&quot;
echo &quot;the entire file, or one single line of that file.&quot;
echo
exit 1
fi

if [ $1 = new ]
then
clear
echo
echo
echo &quot; Are you sure this is a new install?&quot;
echo
echo &quot; If so press y then enter.&quot;
echo
echo &quot; If not press n then enter. &quot;
echo
echo &quot; If this is not a new install your present encryted file will be erased.&quot;
echo
echo
read x

if [ $x = y ]
then
vi $B$D
clear
crypt <$B$D>$B$C
rm $B$D
else
exit 1
fi
exit 1
fi
if [ $1 = change ]
then
clear
crypt <$B$C >$B$D
vi $B$D
clear
crypt <$B$D>$B$C
rm $B$D


exit 1
fi
if [ -z &quot;$1&quot; ]
then
clear
crypt <$B$C | cat
else
clear
crypt <$B$C |grep $1
fi

 
Let me see: you want a low level , reversible, cipher
created in the shell that is not trivially breakable
and that allows you to secure your files?
Let me stop laughing for a second..okay done.

In return I give you this, free:
Code:
function rot13(line, lineout,ascii,p,x,flag,i,cc) {
for (x=0 ; x <= 256 ; x++) {
     ascii[x] = sprintf(&quot;%c&quot;, x)
}
flag=0
         for (p=1 ; p <= length(line) ; p++) {
             cc = substr(line,p,1) 
             for (i in ascii) {
                  if (ascii[i] == cc) {
                      lineout = length(lineout) >= 1 ? lineout ascii[i + 13] : ascii[i + 13]
                      flag=1
                   }
                  }
                  if (flag < 1) {
                       lineout = length(lineout) >= 1 ? lineout substr(line,p,1) : substr(line,p,1)
                  }
                flag=0
           }
print lineout
return lineout
}

function unrot13(line, lineout,ascii,p,x,flag,i,cc) {
for (x=0 ; x <= 256 ; x++) {
     ascii[x] = sprintf(&quot;%c&quot;, x)
}
flag=0
       
        for (p=1 ; p <= length(line) ; p++) {
            cc = substr(line,p,1) 
             for (i in ascii) {
                  if (ascii[i] == cc) {
                      lineout = length(lineout) >= 1 ? lineout ascii[i - 13] : ascii[i - 13]
                      flag=1
                   }
              } 
                 
                 if (flag < 1) {
                       lineout = length(lineout) >= 1 ? lineout substr(line,p,1) : substr(line,p,1)
                  }
              flag=0
           }
print lineout
return lineout
}


{
       if (v == &quot;e&quot;) {
                   $0 = rot13($0)
       } else if (v == &quot;d&quot;) {
                  $0 = unrot13($0)
       }
}
And this so you can enjoy the spectacle harmlessly:
Code:
while read line
do  
   a=`echo $line | awk -v v=&quot;e&quot; -f cript.awk`
   echo -e &quot;Encrypted = $a\n&quot; 
   echo -n &quot;Unencrypted = &quot; 
   echo $a | awk -v v=&quot;d&quot; -f cript.awk
   sleep 2 
done < $filename

Sample OP:
Encrypted = `201|202200201202}G-_vtu201;-`|-204un201-un}}r{rqL-VOZ-t|201-200vpx-

Unencrypted = Stroustrup: Right. So what happened? IBM got sick of it, and inves

Encrypted = zvyyv|{200-v{-201nv{v{t-}|tnzzr2009-201vyy-201ur206-204rr-n-qvzr-n-q

Unencrypted = millions in training programmers, till they were a dime a dozen.

Encrypted =

Unencrypted =

Encrypted = V{201r203vr204rG-aun2014200-204u206-V-t|201-|202201;-`nynvr200-q|}}r

Unencrypted = Interviewer: That's why I got out. Salaries dropped within a year,

Encrypted = 201ur-}|v{201-204urr-orv{t-n-w|202{nyv200201-np201202nyy206-}nvq-or

Unencrypted = the point where being a journalist actually paid better.
 
Thanx squash and marsd. I will try these in couple of hours.

marsd wrote:
&quot;Let me see: you want a low level , reversible, cipher created in the shell that is not trivially breakable and that allows you to secure your files? Let me stop laughing for a second..okay done.&quot;

My reason for what I wrote in my original post is that I just started using Unix (one week to be precise), let alone writing a shell script for it. I do not realy want to secure any files, I just want an easy example which will demonstrate to a complete newbie how encryption and decryption work at the most basic level. Once that is out of the way, than I might be looking into more complex stuff.

Believe me, the above examples might look simple to all, but I will still sweat my braincells at how to implement them. Hehe, let's see, can I make these work...

I'll reply in several hour with any progress made. But whatever happens, thank you again for your replies.

Ivan K. Grant
 
Squash,

I tried your code, but I am obviously doing something completely wrong. I am not sure how to integrate your code into my application I wrote so far. This is what I am trying to do:

Bellow is a code for my menu, from where I can choose various options for my VERY basic translation program.

****************************
# menu for language translation program

quit=n
clear
while test &quot;$quit&quot; = &quot;n&quot;
do

echo &quot;Word translation:&quot;
echo &quot;-----------------&quot;
echo &quot; 0 -- English to French&quot;
echo &quot; 1 -- English to Serbian&quot;
echo
echo &quot; 2 -- French to English&quot;
echo &quot; 3 -- French to Serbian&quot;
echo
echo &quot; 4 -- Serbian to English&quot;
echo &quot; 5 -- Serbian to French&quot;
echo
echo &quot;Language dictionary options:&quot;
echo &quot;----------------------------&quot;
echo &quot; 6 -- List all currently stored words&quot;
echo &quot; 7 -- Add new word&quot;
echo &quot; 8 -- Amend existing word&quot;
echo &quot; 9 -- Delete word&quot;
echo &quot; 10 -- Quit the menu&quot;
echo &quot; 11 -- Encryption Test&quot;

read option

case $option in
0) sh lang_engfra.txt;;
1) sh lang_engser.txt;;
2) sh lang_fraeng.txt;;
3) sh lang_fraser.txt;;
4) sh lang_sereng.txt;;
5) sh lang_serfra.txt;;

6) sh lang_list.txt;;
7) sh lang_append.txt;;
8) sh lang_amend.txt;;
9) sh lang_remove.txt;;
10) quit=y;;
11) sh lang_crypttest.txt;;
*) echo &quot;unknown option&quot;;;
esac

echo
sleep 1
done
*******************************

As you can see, I added option 11, which will make a call to (at the moment (lang_crypttest.txt), where I have cut and pasted your code. This is what top section of this file looks like at the moment as I have changed path to my files:

************************

A=/$home/ivan/test              # Location of this file
B=/$home/ivan/test # Location of encryped file (can be the same)
C=zz.crypt                              # encrypted file
D=zz.txt                                # temporary text file to be encrypted

        if [ $1 = help ]
        then
clear
                        echo
                        echo &quot;Usage:  pass <condition> &quot;
                        echo
                        echo &quot;Possible conditions are:&quot;
.
.
.
**************************

I am doing something fundamentally wrong in here, but as a complete newbie, I cannot see what for the life of me. I am happy that I managed to write rest of the program from scratch while learning a lot in the process, but this encryption is much more complicated than anything else I did so far, 7 days to be precise.

Let the newbie bashing begin :)

Ivan K. Grant
 
ivancmp..
If you just started, I apologize.
This code I gave is not really suited for
someone new to unix.

I would try to learn shell scripting to
a larger degree (6 months should get you
proficient enough not to hurt yourself)
and awk/sed before messing with any of
this.

Good luck
 
Hehe, I'll reffer to this post in six months time :). Thank you anyway. Star is headed your way.
 
Also my appoligies,
As I did not know of your newness to unix.
So just for your learning try this.

At the Unix prompt type

man crypt

This will teach your about encryption and is the basic command used in my script above.



As always we thank you for your support
Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top