Página 1 de 1

Problema con Vigenere

NotaPublicado: 2017-04-01 08:19 @388
por BigBear
Hola, tengo este código simple para codificar con vigenere:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use Crypt::Vigenere;
  2.  
  3. my $encode = encrypt_vigenere("test","123");
  4. #my $decode = decrypt_vigenere($encode,"123");
  5.  
  6. sub encrypt_vigenere {
  7.         $vigenere = Crypt::Vigenere->new($_[1]);
  8.         return $vigenere->encodeMessage($_[0]);
  9. }
  10.  
  11. sub decrypt_vigenere {
  12.         $vigenere = Crypt::Vigenere->new($_[1]);
  13.         return $vigenere->decodeMessage($_[0]);
  14. }
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4

Devuelve:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
Can't call method "encodeMessage" on an undefined value
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Debería funcionar bien, ¿cómo arreglo este error?

Re: Problema con Vigenere

NotaPublicado: 2017-04-01 12:31 @563
por explorer
Lo dice en el archivo README:
The Crypt::Vigenere module accepts only alpha characters in the keyword
and will return an undefined object if any other characters are entered.
The module also only encrypts/decrypts alpha characters, any other
characters will be stripped out of the resulting encrption/decryption.

Traducido:
El módulo Crypt::Vigenere sólo acepta caracteres alfabéticos en la palabra clave
y devolverá un objeto indefinido si se entra cualquier otro carácter.
El módulo, también, solo (des)codifica caracteres alfabéticos; cualesquiera otros
caracteres serán excluidos del resultado de la (des)codificación.

Re: Problema con Vigenere

NotaPublicado: 2017-04-01 19:02 @835
por BigBear
Ok, gracias por la ayuda, explorer. Una pregunta: estoy haciendo lo mismo en Blowfish, este es el código:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use Crypt::Blowfish;
  2.  
  3. my $key = pack("H16", "12345678");
  4. my $text = pack("H16", "test");
  5.  
  6. my $encode = encrypt_blowfish($text,$key);
  7. my $decode = decrypt_blowfish($encode,$key);
  8. my $text_decode = unpack("H16", $decode);
  9.  
  10. print $encode."\n";
  11. print $decode."\n";
  12. print $text_decode."\n";
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Quería codificar el texto "test". Al parecer la clave tiene que tener un mínimo de 8 caracteres y el texto también :? Entonces, ¿no puedo codificar un "test"?, porque lo intento pero solo recibo esta salida:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
øN■B3j?À
Ì═
decd000000000000
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Re: Problema con Vigenere

NotaPublicado: 2017-04-02 09:20 @431
por explorer
Hay varios problemas...

En la línea 3 estás diciendo que quieres empaquetar 16 códigos hexadecimales, pero solo pasas 8, con lo que estás generando una clave de 4 bytes.

En la línea 4 estás pidiendo lo mismo, pero... "test" no contiene códigos hexadecimales (salvo una 'e').

Según la documentación, efectivamente, hay que pasarle información al módulo en bloques de un cierto tamaño. Pero el problema lo tenemos si esos bloques son más pequeños. Una solución es... rellenar con blancos (o ceros o cualquier otro carácter que luego nos sea fácil quitar).

El tamaño del bloque nos lo da el método blocksize().
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. use v5.20;
  3. use Crypt::Blowfish;
  4.  
  5. my $key  = pack "H*", "1234567812345678";       # convertimos 16 códigos hex a binario => 8 bytes
  6.  
  7. my $cipher = Crypt::Blowfish->new($key);
  8.  
  9. # relleno del texto al tamaño que nos pide blocksize()
  10. my $text = "test";
  11. say "[$text]";
  12. $text = substr(($text . ( ' ' x $cipher->blocksize() )), 0, $cipher->blocksize);
  13. say "[$text]";
  14.  
  15. my $encode = $cipher->encrypt($text);
  16. my $decode = $cipher->decrypt($encode);
  17. (my $textdc = $decode) =~ s/ +$//;
  18.  
  19. say "[$key]\n[$text]\n[$encode]\n[$decode]\n[$textdc]";
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

La salida es:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
[test]
[test    ]
[4Vx4Vx]
[test    ]
[6]
[test    ]
[test]
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Re: Problema con Vigenere

NotaPublicado: 2017-04-04 13:17 @595
por BigBear
Ok, gracias por la ayuda, explorer. Una duda: ¿si el texto a codificar supera los 8 caracteres entonces no se podría codificar?

Re: Problema con Vigenere

NotaPublicado: 2017-04-04 13:51 @619
por explorer
En la sección NOTES de Crypt::Blowfish, dice:

In fact, if you have any intentions of
encrypting more than eight bytes of data with this, or any other block
cipher, you're going to need some type of block chaining help. Crypt::CBC
tends to be very good at this.

Que creo que quiere decir
De hecho, si tiene la intención de
codificar más de ocho bytes de datos, o cualquier otro cifrador
de bloques, va a necesitar la ayuda de algún tipo de encadenamiento de bloques. Crypt::CBC
suele ser muy bueno en esto.