# Encrypt and Decrypt script

**URL:** <https://community.unix.com/t/encrypt-and-decrypt-script/178834>\
**Category:** Shell Programming and Scripting\
**Created:** [December 11, 2007, 1:17am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834 "2007-12-11T01:17:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_449c533f586939b6e5f246632a46421f.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [December 11, 2007, 1:17am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/1 "2007-12-11T01:17:39Z")

</div>

Dear Experts,

I am using one script name :volume.sh and its written in bash shell script.  
I just want to encrypt the script so that any one else cannot see it.  
please tell me the commands how to encrypt the script as well as to decrypt it.

Regards,  
SHARY

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/6_449c533f586939b6e5f246632a46421f.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [December 11, 2007, 1:44am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/2 "2007-12-11T01:44:32Z")

</div>

Are other people supposed to be able to run it while it's encrypted, if so how is the interpretor going to understand the file?

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_449c533f586939b6e5f246632a46421f.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [December 11, 2007, 2:04am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/3 "2007-12-11T02:04:06Z")

</div>

Yes i want that the other people can also run the encrypted script .  
But i dont want to show them the code is there is any way out.

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/6_449c533f586939b6e5f246632a46421f.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [December 11, 2007, 2:14am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/4 "2007-12-11T02:14:08Z")

</div>

Out of interest, why?

Is this security through obscurity?

If the interpretor can decode the script to run it, why can't a person?

Start the file as follows

```nohighlight
#!/bin/sh
cat >/dev/null <<EOF
.....
tons of rubbish
....
EOF

```

then follow that with the working parts all on a single line using the ";" separator.

---

<div class="post-metadata">

**Author:** ![shary](https://community.unix.com/letter_avatar/shary/32/6_449c533f586939b6e5f246632a46421f.png) [@shary](https://community.unix.com/u/shary)\
**Post date:** [December 11, 2007, 2:38am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/5 "2007-12-11T02:38:49Z")

</div>

Dear Sir ,

I am totally lost what you have mentioned . I mean i didnt understand the technique to encryt and to run the shell script.

Regards,  
SHARY

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/6_449c533f586939b6e5f246632a46421f.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [December 11, 2007, 2:47am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/6 "2007-12-11T02:47:57Z")

</div>

An obscure script....

```nohighlight
#!/bin/sh
cat >/dev/null <<EOF
ssdf
sdf
sdfhgfgjhghk
ghjgfjgfjgh
jhkjh
k
jhk
jhlkjhljklkj
jghjghghj
jh
kljh
kljh
ljk
l
kjlkjljkkj

jk
l
kj
lkj
ljklkjlkjljkl
kjl
jk
l
kj
l
jk
l

jk
lkjljk
l
kjhjkljhklhfjkhfkjhkjhkjhkjh
EOF
first() { echo $1 }; second() { echo $2 }; third() { echo $3 }; ps -ef -o "pid etime comm" | while read PID ETIME COMM; do case "$ETIME" in; *:* ) DAYS=0; HOURS=0; MINUTES=0; SECONDS=0; case "$ETIME" in; *-* ) X=`echo $ETIME | sed y/-/\ /`; DAYS=`first $X`; ETIME=`second $X`; ;; * ) ;; esac; X=`echo $ETIME | sed y/:/\ /`; case "$ETIME" in; *:*:* ) HOURS=`first $X`; MINUTES=`second $X`; SECONDS=`third $X`; ;; *:* ) MINUTES=`first $X`; SECONDS=`second $X` ;; *) ;; esac; HOURS=`echo $HOURS + \( $DAYS \* 24 \) | bc`; MINUTES=`echo $MINUTES + \( 60 \* $HOURS \) | bc`; SECONDS=`echo $SECONDS + \( 60 \* $MINUTES \) | bc`; if test "$SECONDS" -gt "3600"; then echo $PID $COMM; fi ;; * ) ;;esac;done

```

---

<div class="post-metadata">

**Author:** ![encrypted](https://community.unix.com/user_avatar/community.unix.com/encrypted/32/114_2.png) [@encrypted](https://community.unix.com/u/encrypted)\
**Post date:** [December 11, 2007, 2:58am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/7 "2007-12-11T02:58:27Z")

</div>

How about  
#vi -C somefile

It will ask u for a password while creating it and later while opening it.  
Although the encryption might not be strong enough. Please read man vi  
for the options.

---

<div class="post-metadata">

**Author:** ![encrypted](https://community.unix.com/user_avatar/community.unix.com/encrypted/32/114_2.png) [@encrypted](https://community.unix.com/u/encrypted)\
**Post date:** [December 11, 2007, 3:18am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/8 "2007-12-11T03:18:33Z")

</div>

Addition/Correction to the above post:

You would not be able to run the shell script. However you can encrypt the contents.

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/6_449c533f586939b6e5f246632a46421f.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [December 11, 2007, 3:20am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/9 "2007-12-11T03:20:27Z")

</div>

> [@encrypted](#):
>
> You would not be able to run the shell script.

Yup, defeats the purpose, somewhat.

---

<div class="post-metadata">

**Author:** ![Vicky\_Narayan](https://community.unix.com/letter_avatar/vicky_narayan/32/6_449c533f586939b6e5f246632a46421f.png) [@Vicky\_Narayan](https://community.unix.com/u/Vicky_Narayan)\
**Post date:** [December 11, 2007, 7:11am UTC](https://community.unix.com/t/encrypt-and-decrypt-script/178834/10 "2007-12-11T07:11:13Z")

</div>

Hi Shary,

You can use the "crypt" command to encrypt your file....Pl go thorugh the man pages for its syntax. None wud be able to view the file...Hence ur first condition is satisfied...  
Command: For encryption

% compress myfile  
% ls -l myfile.Z  
% dd if=myfile.Z of=myfile.Z.strip bs=3 skip=1  
% crypt akey \< myfile.Z.strip

For Decryption:  
% (compress -cf /dev/null;crypt \< myfile.Z.strip) | uncompress -c \> myfile

Pl check the above code snippet...U ll be able to encrypt and decrypt ur file...

About ur next condition,I am sorry to say that you wont be able to run the encrypted script...You ll ve to first decrypt it before using it...
