-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Sep 30, 2015
1 parent
7eebed5
commit 1de83a6
Showing
12 changed files
with
1,086 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Given a periodic sequence, checks its lineal complexity and its conection polinomial, using the Berlekamp-Massey algorithm. | ||
|
||
use strict; | ||
use warnings; | ||
|
||
my $secuencia = '10111100010011'; | ||
my $i; | ||
|
||
sub pr_complejidad { | ||
my @C = (1); | ||
my $c_len = 0; | ||
my @B = (1); | ||
my $b_len = -1; | ||
|
||
foreach my $bit (0..(@_-1)) { | ||
my $disc = $_[$bit]; | ||
|
||
foreach my $i (1..$c_len) { | ||
no warnings 'uninitialized'; | ||
$disc += $C[$i] * $_[$bit-$i]; | ||
} | ||
$disc = $disc % 2; | ||
|
||
if ($disc == 1) { | ||
my @T = @C; | ||
|
||
foreach my $i (0..(@B-1)) { | ||
no warnings 'uninitialized'; | ||
$C[$i+($bit-$b_len)] += $B[$i]; | ||
$C[$i+($bit-$b_len)] %= 2; | ||
} | ||
if ($c_len <= $bit / 2) { | ||
$c_len = $bit + 1 - $c_len; | ||
$b_len = $bit; | ||
@B = @T; | ||
} | ||
} | ||
} | ||
return ($c_len, @C); | ||
} | ||
|
||
my @sequence = split("", $secuencia); | ||
my ($complejidad, @tap) = pr_complejidad(@sequence); | ||
|
||
print "Complejidad: ".$complejidad."\nPolinomio: "; | ||
for ($i=0; $i<@tap; $i++){ | ||
if (defined $tap[$i]){print $tap[$i]} | ||
else{print '0'} | ||
} | ||
#print @tap; | ||
print "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Generate asymetric DSS keys. | ||
|
||
#!/bin/perl | ||
use strict; | ||
use warnings; | ||
use bigint; | ||
use Math::BigInt::Random qw/ random_bigint /; | ||
|
||
sub potencia{ | ||
my ($a,$m,$n) = @_; | ||
my $b = 1; | ||
while($m != 1){ | ||
if ($m%2 == 1){$b = ($b*$a)%$n} | ||
$a = ($a*$a)%$n; | ||
$m = $m/2 | ||
} | ||
$b = ($a*$b)%$n; | ||
return $b | ||
} | ||
|
||
sub miller_rabin | ||
{ | ||
my ($n,$k) = @_; | ||
return 1 if $n == 2; | ||
return 0 if $n < 2 or $n % 2 == 0; | ||
my $d = $n - 1; | ||
my $s = 0; | ||
my $x; | ||
|
||
while(!($d % 2)) | ||
{ | ||
$d /= 2; | ||
$s++ | ||
} | ||
LOOP: for(1..$k) | ||
{ | ||
$a = 2 + int(rand($n-2)); | ||
$x = $a->bmodpow($d, $n); | ||
next if $x == 1 or $x == $n-1; | ||
|
||
for(1..$s-1) | ||
{ | ||
$x = ($x*$x) % $n; | ||
return 0 if $x == 1; | ||
next LOOP if $x == $n-1 | ||
} | ||
return 0 | ||
} | ||
return 1 | ||
} | ||
|
||
my $salida_pub="clave_publica.txt"; | ||
my $salida_pri="clave_privada.txt"; | ||
|
||
#my $primo = 10000000000000000000000000000000000000000000023887;#50 digitos. p-1/2 tambien es primo | ||
|
||
#my $primo = random_bigint(min => (2**159), max => (2**160)); | ||
#while(miller_rabin($primo, 4) == 0){ | ||
# $primo = random_bigint(min => (2**159), max => (2**160)); | ||
# print "Probando: $primo\n" | ||
#} | ||
#print "Comprobacion: ".miller_rabin($primo, 15)."\n"; | ||
|
||
my $q = random_bigint(min => (2**159), max => (2**160)); | ||
while(miller_rabin($q, 10) == 0){$q = random_bigint(min => (2**159), max => (2**160))} | ||
my $c = int(rand(512))+512-160;#Numero aleatorio entre 512 y 1024, restado 160 -> Tamaño de $c | ||
$c = random_bigint(min => (2**($c-1)), max => (2**$c)); | ||
if ($c%2 != 0){$c++} | ||
my $p = ($c*$q)+1; | ||
while(miller_rabin($p, 10) == 0){$p += (2*$q); print "Probando...".$p."\n"} | ||
my $g = random_bigint(min => (2), max => ($p-2)); | ||
my $alpha = potencia($g, ($p-1)/$q, $p); | ||
if ($alpha == 1){$alpha++} | ||
|
||
my $x = random_bigint(min => 2, max => ($q-2)); | ||
|
||
print "Escribiendo clave publica...\n"; | ||
open(PUB, ">$salida_pub") || die "Error al crear el archivo\n"; | ||
print PUB "$p\n$q\n$alpha\n".potencia($alpha, $x, $p)."\n"; | ||
close(PUB); | ||
print "Escribiendo clave privada...\n"; | ||
open(PRI, ">$salida_pri") || die "Error al crear el archivo\n"; | ||
print PRI $x."\n"; | ||
close(PRI); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Given the keys created with DSS_gen, signs the SHA1 hash of a message | ||
|
||
#!/bin/perl | ||
use strict; | ||
use warnings; | ||
use bigint; | ||
use Math::BigInt; | ||
use Math::BigInt::Random qw/ random_bigint /; | ||
use Digest::SHA::PurePerl; | ||
|
||
sub potencia{ | ||
my ($a,$m,$n) = @_; | ||
my $b = 1; | ||
while($m != 1){ | ||
if ($m%2 == 1){$b = ($b*$a)%$n} | ||
$a = ($a*$a)%$n; | ||
$m = $m/2 | ||
} | ||
$b = ($a*$b)%$n; | ||
return $b | ||
} | ||
|
||
sub inverso{ | ||
my($a,$n) = @_; | ||
my($t,$nt,$r,$nr) = (0, 1, $n, $a % $n); | ||
while ($nr != 0) { | ||
my $quot = int( ($r - ($r % $nr)) / $nr ); | ||
($nt,$t) = ($t-$quot*$nt,$nt); | ||
($nr,$r) = ($r-$quot*$nr,$nr); | ||
} | ||
return if $r > 1; | ||
$t += $n if $t < 0; | ||
$t; | ||
} | ||
|
||
print "Cargando clave publica..."; | ||
open(PUB, "<clave_publica.txt") || die "Error al cargar el archivo\n"; | ||
my @entrada = <PUB>; | ||
my $p = Math::BigInt->new($entrada[0]); | ||
my $q = Math::BigInt->new($entrada[1]); | ||
my $alpha = Math::BigInt->new($entrada[2]); | ||
my $y = Math::BigInt->new($entrada[3]); | ||
close(PUB); | ||
print "Hecho\n"; | ||
|
||
print "Cargando clave privada..."; | ||
open(PRI, "<clave_privada.txt") || die "Error al cargar el archivo\n"; | ||
@entrada = <PRI>; | ||
my $x = Math::BigInt->new($entrada[0]); | ||
close(PRI); | ||
print "Hecho\n"; | ||
|
||
print "Cargando fichero..."; | ||
open(DATA, "<$ARGV[0]") || die "Error al cargar el archivo para firmar. Pasa el nombre del archivo como parametro en la linea de comandos\n"; | ||
my $sha = Digest::SHA::PurePerl->new(1); | ||
$sha->addfile(*DATA); | ||
$sha = $sha->hexdigest;#La salida del hash es en hexadecimal | ||
$sha = hex('0x'.$sha);#Conversion a decimal | ||
close(DATA); | ||
print "Hecho\n"; | ||
|
||
my $k = random_bigint(min => 2, max => ($q-2)); | ||
my $r = (potencia($alpha, $k, $p)) % $q; | ||
my $s = ($sha+($x*$r)) % $q; | ||
$s *= inverso($k, $q); | ||
$s = $s % $q; | ||
|
||
print "Escribiendo firma..."; | ||
open(FIR, ">firma.txt") || die "Error al crear el archivo de firma\n"; | ||
print FIR "$r\n$s\n"; | ||
close(FIR); | ||
print "Hecho\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Given a sign, a message, and a public DSS key, it checks if the sign is or is not right. | ||
|
||
#!/bin/perl | ||
use strict; | ||
use warnings; | ||
use bigint; | ||
use Math::BigInt; | ||
use Math::BigInt::Random qw/ random_bigint /; | ||
use Digest::SHA::PurePerl; | ||
|
||
sub potencia{ | ||
my ($a,$m,$n) = @_; | ||
my $b = 1; | ||
while($m != 1){ | ||
if ($m%2 == 1){$b = ($b*$a)%$n} | ||
$a = ($a*$a)%$n; | ||
$m = $m/2 | ||
} | ||
$b = ($a*$b)%$n; | ||
return $b | ||
} | ||
|
||
sub inverso{ | ||
my($a,$n) = @_; | ||
my($t,$nt,$r,$nr) = (0, 1, $n, $a % $n); | ||
while ($nr != 0) { | ||
my $quot = int( ($r - ($r % $nr)) / $nr ); | ||
($nt,$t) = ($t-$quot*$nt,$nt); | ||
($nr,$r) = ($r-$quot*$nr,$nr); | ||
} | ||
return if $r > 1; | ||
$t += $n if $t < 0; | ||
$t; | ||
} | ||
|
||
print "Cargando firma..."; | ||
open(FIR, "<firma.txt") || die "Error al cargar el archivo de firma\n"; | ||
my @entrada = <FIR>; | ||
my $r = Math::BigInt->new($entrada[0]); | ||
my $s = Math::BigInt->new($entrada[1]); | ||
close(FIR); | ||
print "Hecho\n"; | ||
|
||
print "Cargando fichero..."; | ||
open(DATA, "<$ARGV[0]") || die "Error al cargar el archivo para firmar. Pasa el nombre del archivo como parametro en la linea de comandos\n"; | ||
my $sha = Digest::SHA::PurePerl->new(1); | ||
$sha->addfile(*DATA); | ||
$sha = $sha->hexdigest;#La salida del hash es en hexadecimal | ||
$sha = hex('0x'.$sha);#Conversion a decimal | ||
close(DATA); | ||
print "Hecho\n"; | ||
|
||
print "Cargando clave publica..."; | ||
open(PUB, "<clave_publica.txt") || die "Error al cargar el archivo\n"; | ||
@entrada = <PUB>; | ||
my $p = Math::BigInt->new($entrada[0]); | ||
my $q = Math::BigInt->new($entrada[1]); | ||
my $alpha = Math::BigInt->new($entrada[2]); | ||
my $y = Math::BigInt->new($entrada[3]); | ||
close(PUB); | ||
print "Hecho\n"; | ||
|
||
my $u = $sha % $q; | ||
$u *= inverso($s, $q); | ||
$u = $u % $q; | ||
my $v = $r % $q; | ||
$v *= inverso($s, $q); | ||
$v = $v % $q; | ||
my $r_prima = potencia($alpha, $u, $p); | ||
$r_prima *= potencia($y, $v, $p); | ||
$r_prima = $r_prima % $p; | ||
$r_prima = $r_prima % $q; | ||
|
||
if ($r == $r_prima){print "Firma VALIDA\n"} | ||
else{print "Firma NO valida\n"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Geffe generator implementation. | ||
|
||
use strict; | ||
use warnings; | ||
|
||
my $mensaje = '1000101101011010100101001010101001010111010111000000011111'; | ||
|
||
my @coef; | ||
my $i; | ||
my $j; | ||
my $sig; | ||
my $res; | ||
my $res_2; | ||
|
||
################## Primer LSFR | ||
|
||
my $seed='1001'; | ||
my @sr_temp = split(//,$seed); | ||
my @taps=(4,2,0); | ||
my $long = 4; | ||
my $lsfr1=LSFR(); | ||
|
||
################## Segundo | ||
|
||
$seed='1101'; | ||
@sr_temp = split(//,$seed); | ||
@taps=(4,2,0); | ||
$long = 4; | ||
@coef = (); | ||
my $lsfr2=LSFR(); | ||
|
||
################# Tercero | ||
|
||
$seed='1101'; | ||
@sr_temp = split(//,$seed); | ||
@taps=(4,3,2,1,0); | ||
$long = 4; | ||
@coef=(); | ||
my $lsfr3=LSFR(); | ||
|
||
################### | ||
|
||
$res = multiplica($lsfr1, $lsfr2); | ||
$res_2 = multiplica($lsfr2, $lsfr3); | ||
$res = suma1($res, $res_2); | ||
$res = suma1($res, $lsfr3); | ||
|
||
print "Clave para cifrar: $res\n"; | ||
print "Mensaje cifrado: ".suma1($res,$mensaje)."\n"; | ||
|
||
sub multiplica{ | ||
my @a= split(//,$_[0]); | ||
my @b= split(//,$_[1]); | ||
my $z; | ||
my $m = ''; | ||
|
||
for ($z=0; $z<@a; $z++){ | ||
if (($a[$z] == '1') && ($b[$z] == '1')){$m = $m.'1'} | ||
else{$m = $m.'0'} | ||
} | ||
return $m; | ||
} | ||
|
||
sub suma1{ | ||
my @a= split(//,$_[0]); | ||
my @b= split(//,$_[1]); | ||
my $z; | ||
my $m = ''; | ||
|
||
for ($z=0; $z<@a; $z++){ | ||
if ($a[$z] == $b[$z]){$m = $m.'0'} | ||
else{$m = $m.'1'} | ||
} | ||
return $m; | ||
} | ||
|
||
sub LSFR{ | ||
my $long = length($seed); | ||
for ($i=0; $i<@taps-1; $i++){ | ||
push(@coef, $long-$taps[$i]); | ||
} | ||
for ($j=0; $j<length($mensaje)-$long; $j++){ | ||
#print "Sumo: "; | ||
$sig='0'; | ||
for ($i=0; $i<@coef; $i++){ | ||
$sig = suma($sig, $sr_temp[$coef[$i]]); | ||
#print $sr_temp[$coef[$i]]." + "; | ||
} | ||
$seed=$seed.$sig; | ||
desplaza(); | ||
} | ||
return $seed; | ||
} | ||
|
||
sub suma{ | ||
my $uno = shift; | ||
my $dos = shift; | ||
|
||
if ($uno == $dos){return '0'} | ||
else{return '1'} | ||
} | ||
|
||
sub desplaza{ | ||
my @temp2 = @sr_temp; | ||
my $cont; | ||
|
||
@sr_temp = (); | ||
for ($cont=1; $cont<@temp2; $cont++){ | ||
push(@sr_temp,$temp2[$cont]); | ||
} | ||
push(@sr_temp, $sig); | ||
} |
Oops, something went wrong.