uebung8.plsrc


#!/usr/local/bin/perl -w
use strict; $|=1; no strict 'refs'; use POSIX; use Math::BigInt;


sub prob28 {
  sub toverbose {
    my $b = @_;
    '$'. join ('+', map { $b-- ; $_ . ($b == 0 ? '' : '\cdot 26' . 
         ($b == 1 ? '' : '^'.$b)); } @_) .'$'
  }

  sub e_rsa {  
    my ($n, $e, $bl) = splice @_,0,3;
    my @ptBs = split //, shift; my @ctBs;
     
    print "\\begin{tabular}{lclclclcl}\n";
    for my $i (0..@ptBs/$bl-1) {
      my @b = map { print $_; ord ($_) - ord ('a')} splice @ptBs, 0, $bl;
      print '&$\rightarrow$&'; my $b = @b;
      print '$(', join (',', @b), ')$&$\rightarrow$&$x_', $i+1, '$ &$=$& ', 
                 toverbose (@b); 
      my $blockVal; $blockVal += 26**(@b-1-$_) * $b[$_] for 0..@b-1;
      push @ptBs, $blockVal; print '&=&', $blockVal, "\\\\\n";
    }
    print "\\end{tabular}\n";
    print "\\begin{tabular}{lcrcrcrcl}\n";
    for (0..@ptBs-1) {
      my $t = $ptBs[$_]**$e%$n;
      print '$y_{', $_+1, '}$&$=$&$', $ptBs[$_], '^{', $e, '} \mod ', $n, 
            '$&$=$&', $t, ' &$\rightarrow$& '; 
      @_ = ();
      while ($t) {
        my $l = $t % 26; $t /= 26;
        push @_, $l
      }
      @_ = reverse @_;
      my $ct = join '', map { uc chr $_ + ord 'a' } @_;
      print toverbose (@_), ' &$\rightarrow$& ', $ct,  "\\\\\n";    
      push @ctBs, $ct
    }
    print "\\end{tabular}\n\n";
    return join ' ', @ctBs
  }    

  print 'Chiffretext : \textbf{', 
    e_rsa (new Math::BigInt (11413), new Math::BigInt(3533), 
           new Math::BigInt(2), "anrufumelf"), "}\n";
}

sub prob29 {
  sub witness {
    my ($a, $n) = @_;    
    my @b = split //, sprintf "%b", $n-1;
    my $d = 1;
      
    for (0..@b-1) {
      my $x = $d; 
      $d = ($d * $d) % $n;        
      return 2 if $d == 1 and $x != 1 and $x != $n-1;
      $d = $d * $a % $n if $b[$_]
    }
    
    return 1 if $d != 1;
    0
  }

  my ($ones, $twos) = '0'x2;
  for my $a (1..341) {
    if (witness ($a, 341)==1) {
      print "Witness $a gibt 1 zurück\n" if (witness ($a, 341)==1);
      $ones++;
      }
    if (witness ($a, 341)==2) {
      print "Witness $a gibt 2 zurück\n" if (witness ($a, 341)==2);
      $twos++;
    }
  }
  print "Es existieren $ones Zeugen die 1 provozieren".
        " und $twos Zeugen die 2 provozieren\n";

}

sub prob32 {
  my $n = 12;
  do {
    ++$n;
    while (witness (2, $n) or witness (3, $n)) { ++$n }
  } while not witness (5, $n);
  print $n, "\n"
}


&{'prob'.$ARGV[0]} (splice @ARGV,1)