Encrypt a file

From EggeWiki

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>

Encrypt a password for a script using des3

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>

Encrypt a password for a script using AES

This is much the same as the above. You'll want to create a 256-bit key.

<geshi lang="bash"> openssl rand 32 > ~/.ssh/aes256 && chmod 400 ~/.ssh/aes256

  1. or

dd if=/dev/random of=~/.ssh/aes256 bs=32 count=1 && chmod 400 ~/.ssh/aes256 </geshi>

You can test the encrypt/decrypt with the following command. <geshi lang="bash"> echo test | /usr/bin/encrypt -a aes -k ~/.ssh/aes256 | /usr/bin/decrypt -a aes -k ~/.ssh/aes256 </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>