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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encryption Problem

Status
Not open for further replies.

DANZIG

Technical User
Mar 8, 2001
142
US
Hellos,
I'm trying to write a script to encrypt files (some text, some autocad some *.doc ect) prior to ftp'ing them to an outsourced firm we use. I've found the example below but it doesn't seem to be working.

Code:
use Crypt::CBC;
use FileHandle;
use strict;
open(CT,">c:\\test\\enc\\MouseOver-Status.Htm");
open(PT,"<c:\\test\\enc\\MouseOver-Status.Enc");

my $cipher = new Crypt::CBC({'key'         => 'abc12345',
                             'cipher'      => 'Blowfish_PP',
                             'padding'     => 'space'});
undef $/;
print CT $cipher->encrypt(<PT>);

close(CT);
close(PT);
exit(0)

Results:
Code:
C:\Test\enc>dir /b
enc.pl
MouseOver-Status.Htm

C:\Test\enc>enc.pl

C:\Test\enc>dir /b
enc.pl
MouseOver-Status.Htm

C:\Test\enc>

Installed Modules:
Code:
ppm> query *
Querying target 1 (ActivePerl 5.8.6.811)
   1. ActivePerl-DocTools     [0.04] Perl extension for Documentation TOC Gene~
   2. ActiveState-Relocat~    [0.03] Relocate a Perl installation
   3. ActiveState-Rx          [0.60] Regular Expression Debugger
   4. Archive-Tar             [1.22] Manipulates TAR archives
   5. Compress-Zlib           [1.33] Interface to zlib compression library
   6. Crypt-CBC               [2.12] Encrypt Data with Cipher Block Chaining M~
   7. Crypt-DES               [2.03] Perl DES encryption module
   8. Crypt-DES_EDE3          [0.01] Triple-DES EDE encryption/decryption
   9. Crypt-DES_EEE3          [0.01] Triple-DES EEE encryption/decryption
  10. Crypt-DES_PP            [1.00] Perl extension for DES encryption
  11. Data-Dump               [1.06] Pretty printing of data structures
  12. Digest-HMAC             [1.01] Keyed-Hashing for Message Authentication
  13. Digest-MD2              [2.03] Perl interface to the MD2 Algorithm
  14. Digest-MD5-M4p          [0.01] Perl interface to a variant of the MD5 al~
  15. Digest-SHA1             [2.10] Perl interface to the SHA-1 Algorithm
  16. File-CounterFile        [1.04] Persistent counter class
  17. Font-AFM                [1.19] Interface to Adobe Font Metrics files
  18. HTML-Parser             [3.38] HTML parser class
  19. HTML-Tagset             [3.03] Data tables useful in parsing HTML
  20. HTML-Tree               [3.18] build and scan parse-trees of HTML
  21. IO-String               [1.06] Is an IO::File compatible class that read~
  22. IO-Zlib                 [1.04] IO:: style interface to Compress::Zlib
  23. libwin32                [0.24] A collection of extensions that aims to p~
  24. lib[URL unfurl="true"]www-perl[/URL]            [5.801] Library for [URL unfurl="true"]WWW access[/URL] in Perl
  25. MD5                     [2.03] Perl interface to the MD5 Algorithm (obso~
  26. PPM3                     [3.1] Perl Package Manager: locate, install, up~
  27. SOAP-Lite               [0.55] Library for Simple Object Access Protocol~
  28. Tk                   [804.027] A Graphical User Interface Toolkit
  29. URI                     [1.35] Uniform Resource Identifiers (absolute an~
  30. Win32-AuthenticateU~    [0.02] Win32 User authentication for domains
  31. XML-Parser              [2.34] A Perl module for parsing XML documents
  32. XML-Simple              [2.12] Easy API to read/write XML (esp config fi~
ppm>

Any Idea what I'm missing?


Thanks, Danzig
 
Your script (the way it is right now)
creates a file named MouseOver-Status.Htm and opens it for writting
open a file named MouseOver-Status.Enc for reading

creates a crypt object from the .Enc file and writes it into the .Htm file

So if you don't have the MouseOver-Status.Enc to read from then it creates every time an empty file named MouseOver-Status.Htm

Did this cleared things up a bit?

I suggest to change it like this (look the red changes)
Code:
use Crypt::CBC;
use FileHandle;
use strict;
[red]open(CT,">c:\\path\\to\\new\\encrypted_file.whatever");
open(PT,"<c:\\path\\to\\your\\file.whatever");[/red]

my $cipher = new Crypt::CBC({'key'         => 'abc12345',
                             'cipher'      => 'Blowfish_PP',
                             'padding'     => 'space'});
undef $/;
print CT $cipher->encrypt(<PT>);

close(CT);
close(PT);


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
One other thing you might want to do is add either a die statement to your file opens or a warn. This way, you get notified when a file cannot be opened, as was the case with your code above.

Code:
open(CT,">c:\\path\\to\\new\\encrypted_file.whatever") or die "Cannot open Encryption file: $!\n";
open(PT,"<c:\\path\\to\\your\\file.whatever") or die "Cannot open Original file: $!\n";

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top