http://www.perlmonks.org?node_id=63343
Category: Cryptography
Author/Contact Info Wombat
Description: I recognize that this is not a security group, but I figure there must be at least a few cypherpunks in the lot of you. I came up with this scheme for encrypting text, and submit it to you to see if anyone can come up with any obvious holes.

The way this works, is a user types a passphrase which gets turned into a number between 0 and the maximum size of an integer (4,294,967,924). This number then is used to seed the random number generator. The program then collects single numbers between 0 and 7 by repeatedly calling rand(), until it has a bit vector. Then for each character of input, it maps the characters bits to the bit vector producing a scrambled character which it prints to STDOUT. By entering the same passphrase again, you get the same srand seed and thus can decrypt your bits at a later date.

Features include:
Arbitrarily long passphrases: Type as much as you like, the seed won't mind.
Non-portability: (It's not a bug!) I realize that everyone has different random number generators. That's okay. I'm mostly using this to lock down my own personal secret files. I don't think the security would be compromised too much if a person sent the particulars of their random number generator along with cyphercode if they wanted a friend to get a message.
No way to decrypt the text: I haven't coded the decryptor yet, so as of now, once encrypted, things STAY encrypted! :-)

So yeah! Like, peer-review me and stuff! BTW: I do realize that this is "Weak security" at best, and probably can be defeated by brute force if so desired. I know. But BESIDES that... :-)

~W
#!/usr/bin/perl -w
 
$key=0;        #Initialize variables. 
$updown=0;
$i=0;
@arrdone=(0,0,0,0,0,0,0,0);
 
print "Enter passphrase: ";
 
$phrase=<STDIN>;
chomp $phrase;
 
@letters = split //,$phrase;
 
foreach $CHAR (@letters) {  #Use phrase to create random seed
  $newbits=ord($CHAR);
  $modulo = $newbits % 10;
  if ($updown == 0) {
    if ($key < 0){$key*=-1;}
    $key=$key*($modulo+1);
    $key+=$newbits;
    if ($key>4294967294){$updown=1;}
  }
  if ($updown == 1) {
    $key=$key/($modulo+1)-($key%($modulo+1)/($modulo+1));
    $key-=$newbits;
    if ($key<0){$updown=0;}
  }
}
 
srand $key;
 
while(<STDIN>) {
 chomp;
 @inputchars = split //;
 foreach $CHAR (@inputchars){
 $charnum=ord($CHAR);
 $oldt=$charnum;
 
 for (0..8) {      #Translate the input char into binary
  $t = $charnum % (2**$_);
  if($t==$oldt) {
   $binarr[8-$_]=0;
  }else {$binarr[8-$_]=1;}
  $oldt=$t;
 }  
 
  while ($i<8) {   #Assemble the key bit vector.
    $randout=rand(8)%8;
    if ($arrdone[$randout]==0) {
    $shifter[$i]=$randout;
    $arrdone[$randout]=1;
    $i++;
    }
  }
 
  for(0..7) {      #Assemble the scrambled bit vector.
    if ($binarr[$_]==1) {
    $outchar[$shifter[$_]] = 1;
    }
    else {$outchar[$shifter[$_]] =0;}
  }
 
  for(0..7) {     #Rejoin the new bits to an ord
    if ($outchar[$_]==1) {
    $sum+=2**(7-$_);
    }
  }
 
  $och= chr($sum);    #Print the char.
  print $och;
 
  $i=0;     #Reset variables
  $sum=0;
   for(0..7){
    $outchar[$_]=0;
    $arrdone[$_]=0;
    $shifter[$_]=0;
   }
  }
 }
Replies are listed 'Best First'.
Re: Wombat's Bit Scrambler
by arhuman (Vicar) on Mar 10, 2001 at 02:46 UTC
    I'm one of those who always look for new improved things.

    So I was rather interested by your post.

    Anyway I'd like to point out some weakness/problem which could be interesting to enhance/solve :

    1) The key space (the seed is the key) is really too small in my humble opinion (32 bits !!!!).
    why not use your passphrase to create several seed which could be used to introduce more entropy.
    (produce N seeds and use N pseudo random generator?)

    2) I think that you supposition that each pseudo random generator is different is risky.
    very few good generators exists and in fact it seems that a bad one is the most used (linear congruential generator)
    In fact it's probable that different system will produce the same values with the same seed.
    And even if I don't know if the Perl rand function will give the same values on different system given the same seed,
    It's higly possible that if someone implement this algo in another language the rand function will be a linear congruential generator.

    3) BTW if the rand function is a Linear congruential generator the period may be ridiculously short (16838 for ANSI one) so long text encrypting would lead to easy cryptanalisis.
    Moreover whatever the period of your generator you shouldn't use it to encrypt an unlimited plain text (or a very long message).

    4) you're algo seems weak against choosen (or even known) text attack

    But as I said before , I like your try and hope you'll go on trying to enhance this.

    As long as you remember that whatever your skill without a long review by (real) cryptanalists, it IS weak security.

    UPDATE : A good reading could be the article "Randomness - The Perl Journal, Winter 1996"

    "Trying to be a SMART lamer" (thanx to Merlyn ;-)
      There is a module on CPAN called Crypt::Random, which is supposed
      to provide numbers that are random enough for cryptological applications.
      This module might be worth looking at.

      TStanley
      In the end, there can be only one!
        If Crypt::Random is really random (and it seems to be) then you won't be able to decrypt the ciphered message giving the same seed.


        "Trying to be a SMART lamer" (thanx to Merlyn ;-)
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Wombat's Bit Scrambler
by bladx (Chaplain) on Mar 10, 2001 at 21:28 UTC
    Overall, wombat, I think you had a great idea for your program, and though I haven't studied the entirety of the program, I was thinking about that thing that arhuman was commenting on... and that was "If Crypt::Random is really random (and it seems to be) then you won't be able to decrypt the ciphered message giving the same seed." And that's something important to think about... make sure to not have it randomize to the point of not being able to reconvert the information after being scrambled!! Although you probably have already considered this, hope my opinion helps a bit!! Later.

    bladx ~ ¡muchas veces tengo preguntas!
      They are called pseudo random number generators for a reason. They are not really random, just close to it.
Re (tilly) 1: Wombat's Bit Scrambler
by tilly (Archbishop) on Mar 10, 2001 at 02:32 UTC
    What is wrong with MD5?

    UPDATE
    Nevermind, I misread the problem. But again I would suggest using standard encryption techniques unless you care to know a lot more about the subject than I do.