ROFL has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am trying to write a program that encrypts and decrypts data so it can be securely sent across the internet. It works by loading all of the unique characters into an array and then changing each one to another character within that array (each one receives a unique value as well). The decrypter simply unscrambles the nonsensical output and rebuilds the exact same file you had before. It works great for an all text file (I tried it on a few essays I had written) but I ran it on a .wmv file and I was getting a weird error. I could encrypt and decrypt it just fine and my input file and final output file seemed to be exactly the same except one had a few more characters than the other but I couldn't open the new .wmv. I wrote a quick script to compare strings from the two files and found no differences. I know the problem is either the Non-ASCii characters or the .wmv format. Any solutions?

Replies are listed 'Best First'.
Re: Non-ASCii character encryption
by Perlbotics (Archbishop) on Aug 31, 2011 at 22:11 UTC

    Do you use one of the well known ciphers like e.g. AES or a home-grown one?

    Designing a good cipher is extremely hard. Here's an ancient but nevertheless still true statement from a well-known cryptographer - Bruce Schneier - that might be discouraging at first sight, but can save you a lot of trouble in the long run.

    Your problem has most probably nothing to do with the AVI file but with the way it is read (binmode?) and processed (character range).

    In order to get a better answer, you might need to provide us with a meaningful code sample.

    Update: ... forgot to mention SFTP, which handles encryption and file-transfer (see Net::SFTP and friends, esp. Net::SFTP::Foreign).

      forgot to mention SFTP

      and let my add SSL/TLS, SSH, HTTPS, FTPS or PGP, as other standard protocols providing encryption in different ways.

Re: Non-ASCii character encryption
by ikegami (Patriarch) on Aug 31, 2011 at 21:56 UTC

    Make sure you open the files using binmode.

    open(my $fh, '<', 'file') or die("open \"file\": $!\n"); binmode($fh);

    If you want encryption, you should really uses Rijndael aka AES (or Twofish or Blowfish) via Crypt::CBC.

Re: Non-ASCii character encryption
by JavaFan (Canon) on Sep 01, 2011 at 06:38 UTC
    It works by loading all of the unique characters into an array and then changing each one to another character within that array (each one receives a unique value as well). The decrypter simply unscrambles the nonsensical output and rebuilds the exact same file you had before.
    That sounds like a simple substitution encryption. The complexity of that encryption is such that's it's used as childrens puzzles to unravel "secret codes". While it has nothing to do with the problem you are facing, I do wonder, which data is sensitive enough that it needs encryption when "sending it across the internet", but is innocent enough that a home-grown substitution encryption will do?
      Thanks for the suggestions,

      I realize that a substitution encryption in and of itself is very weak. Its only intended to be one step of the encryption process. This is largely a project that I'm doing for amusement to see how secure I can get a piece of data.

      To compare the strings I'm simply loading each line of each file into an array and saying if linex of file1 doesn't equal linex of file2 then print out the line number. Nothing is printing out even though the files are a slightly different size. Its a rather peculiar issue. I think it has something to do with perl reads and prints non ascii characters. Is there a way to just make perl read and print 0's and 1's instead of entire symbols? I'm rather new to perl and haven't been able to find this...

      Thanks!
Re: Non-ASCii character encryption
by choroba (Cardinal) on Aug 31, 2011 at 21:46 UTC
    Can you give more details on how the encryption works? How do you compare the files if files of different size are reported as "exactly the same"?