Encrypt a file: Difference between revisions

From EggeWiki
mNo edit summary
mNo edit summary
Line 1: Line 1:
How to quickly encrypt a file.
How to quickly encrypt a file on Solaris 10 using [http://docs.sun.com/app/docs/doc/819-2239/encrypt-1?a=view encypt].


<geshi lang="bash">
<geshi lang="bash">
echo "Hello world" > message.txt
echo "Hello world" > message.txt
encrypt -a 3des -i message.txt -o message.txt.encrypted
/usr/bin/encrypt -a 3des -i message.txt -o message.txt.encrypted
Enter key:
Enter key:
decrypt -a 3des -i message.txt.encrypted
/usr/bin/decrypt -a 3des -i message.txt.encrypted
Enter key:
Enter key:
Hello world
Hello world

Revision as of 23:48, 14 September 2009

How to quickly encrypt a file on Solaris 10 using encypt.

<geshi lang="bash"> echo "Hello world" > message.txt /usr/bin/encrypt -a 3des -i message.txt -o message.txt.encrypted Enter key: /usr/bin/decrypt -a 3des -i message.txt.encrypted Enter key: Hello world </geshi>

One way to encrypt a password for a script

First, create a 192-bit key:

<geshi lang="bash"> openssl rand 24 > ~/.ssh/des3

  1. Alternatively, this command does about the same:

dd if=/dev/random of=~/.ssh/des3 bs=24 count=1 </geshi>

We'll put this in our .ssh directory, because that directory will have proper permissions, and many programs will complain if they are wrong. The key thing is to keep this key separate from the script.

Next, change the permissions on the file itself:

<geshi lang="bash"> chmod 400 ~/.ssh/des3 </geshi>

Now take your unencrypted password, and pipe it into encrypt. In bash, you can do this

<geshi lang="bash"> read -s PASSWD echo $PASSWD | /usr/bin/encrypt -a 3des -k ~/.ssh/des3 | base64 AAAAAQAAA+iXGpYztX7Eidzu44k5wEgjSkkUBpSnj13faKbrJpoDPN2+sgIhPeSz </geshi>

"read -s" won't echo it to your terminal, nor leave it in the history.

Now, take the base64 encoded, encrypted password, and use that in your script:

<geshi lang="bash">

  1. !/bin/bash

PASSWD="AAAAAQAAA+iXGpYztX7Eidzu44k5wEgjSkkUBpSnj13faKbrJpoDPN2+sgIhPeSz" echo "$PASSWD" | base64 -d | /usr/bin/decrypt -a 3des -k ~/.ssh/des3 | isql -X ... </geshi>


Encrypting a file using openssl and a password.

<geshi lang="bash">

  1. encrypt file.txt to file.enc using 256-bit AES in CBC mode

openssl enc -aes-256-cbc -salt -in file.txt -out file.enc </geshi>

<geshi lang="bash">

  1. decrypt binary file.enc

openssl enc -d -aes-256-cbc -in file.enc </geshi>