Return to Linux Home Page.


Cannot find the "crypt" command in Linux

by Jeff Hunter, Sr. Database Administrator

Contents

  1. Overview
  2. Usage
  3. Code

Overview

While attempting to port several applications written on the Solaris platform to Linux, I needed to use the "crypt" program, to encode a text file (for those of you unfamiliar with it, it encrypts the contents of a file using a password that you supply. A fairly useful little utility when you want just a bit of added security on a publicly-readable file.) Imagine my surprise when I found that Linux does not have one! Thanks to Ben Okopnik for creating a small Perl utility that provides the functionality of the "crypt" program.

This version follows the syntax of the original "crypt", although I'm certain that the algorithm is different. The encryption and the decryption are symmetric, meaning that the same syntax is used for both (with the obvious exception of the relevant filenames)

Usage

     # This encrypts the contents of "mysecret.txt" using "iAMw0tIam" as the 
     # password and saves the results to "mysecret.encrypted". 
     crypt iAMw0tIam < mysecret.txt > mysecret.encrypted 

     # This prints out the decrypted contents of "mysecret.encrypted" to the 
     # screen. 
     crypt iAMw0tIam < mysecret.encrypted 

     # This decrypts the contents of "mysecret.encrypted" and writes the results 
     # to "mysecret.decrypted". 
     crypt iAMw0tIam < mysecret.encrypted > mysecret.decrypted 
     Long passwords are preferable to short ones for better security; 
     "crypt" accepts them without a problem, provided that any password containing 
     spaces or other "weird characters" is quoted (preferably in single quotes). 

     crypt 'Praeterea, censeo Carthaginem esse delendam.' < file > file.enc

Code

     #!/usr/bin/perl -w
     # Created by Ben Okopnik on Sat Sep  8 10:11:23 UTC 2001

     $pass = shift or die "Usage: ", $0 =~ /([^\/]*$)/, " passwd < file\n";

     undef $/;                 # Set "slurp mode"
     @file = split //, <>;     # Make a 'character' array from file

     # Tweak the password itself to avoid "known text XOR" attack
     @pass = map{
       $x = ord;
       $y = ( $x + 10 ) ** 2 % 256;
       $z = ( $x - 10 ) ** 2 % 256;
       chr ( $y ^ $z )
     } split //, $pass;

     # Do The Right Thang!
     print chr( ord $file[$_] ^ ord $pass[$_ % scalar @pass] ) for 0 .. $#file



Last modified on: Tuesday, 26-Jul-2005 13:55:36 EDT
Page Count: 12247