Problemas descifrar fichero Algoritmo DES
Publicado por Antonio (9 intervenciones) el 07/02/2007 09:30:17
Buenas,
Tengo un pequeño problema que no se como resolver. El problema en cuestión es que cifro un fichero xml con el algoritmo DES, guardando el fichero cifrado. Luego cuando intento descifrar el fichero cifrado, resulta que me lo descifra pero me mete un montón de basura (caracteres raros), que no aparece en el original.
Alguien sabe porque puede ocurrir esto.
Un ejemplo de lo que me aparece.
<num__hijo>2</num__hijo>
?3p??}B</apell1>
<apell2>B</ap???*?M?y <nombre>B</nombre>??P]??`? <sexo>V</sexo>
<fec__nacimiento>02/02/2KVicy?__nacimiento>
<fec__nacimiento__(????>N</fec__nacimiento__ficticia>
<lugar__nacimiento></lugar__nacimiento>
<lugar__residencia></lugar__residencia>
</lista__hijos>
<lista__hijos/>
</hijos>
<panel>
<observaciones__paises__transito>
<str?MkO??FRANCIA</string>??P]??`?Yu8@=??>OBS UK</string>
</observaciones__paises__transito>
Aqui un ejemplo del codigo que utilizo para cifrar y para descifrar sería igual pero descifrando.
Security.addProvider (new BouncyCastleProvider ()); // Usa provider BC
//
/* PASO 1: Crear e inicializar clave */
String textoPlano = "";
/*KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(128);
Key clave = keyGen.generateKey();*/
//Key clave = KeyDES.ObtenerKey();
SecretKeySpec key = new SecretKeySpec (clave_simetrica, "DES");
/* PASO 2: Crear cifrador */
Cipher cifrador= Cipher.getInstance ("DES/ECB/PKCS7Padding","BC");
// Algoritmo DES
// Modo : ECB (Electronic Code Book)
//
/* PASO 3a: Inicializar cifrador en modo CIFRADO */
cifrador.init (Cipher.ENCRYPT_MODE, key);
File fichero_cifrado = new File ("C://Antonio//fichero_cifrado.xml");
System.out.println ("fichero_cifrado "+fichero_cifrado);
FileOutputStream fout = new FileOutputStream (fichero_cifrado);
BufferedOutputStream bout = new BufferedOutputStream (fout);
File fichero = new File ("C://Antonio//01022007_113154.xml");
byte[] bufferPlano = new byte[Integer.parseInt (String.valueOf (fichero.length ()))];
FileInputStream fin = new FileInputStream (fichero);
BufferedInputStream bin = new BufferedInputStream(fin);
int total = 0;
int bytesLeidos = bin.read (bufferPlano);
//int bytesLeidos = fin.read (bufferPlano);
//while(bytesLeidos != -1)
//{ // Mientras no se llegue al final del fichero
total = total + bytesLeidos;
System.out.println ("total "+total);
textoPlano = textoPlano + new String (bufferPlano);
//bytesLeidos = bin.read (bufferPlano,0, 1024);
//bytesLeidos = in.read (bufferPlano,0, 1024);
System.out.println ("bytesleidos "+bytesLeidos);
//}
bin.close ();
fin.close ();
byte[] textofinal = textoPlano.getBytes ();
/* Leer fichero de 1k en 1k y pasar fragmentos leidos al cifrador */
byte[] cipherText = cifrador.doFinal (textofinal,0,total);
System.out.println ("cipherText "+cipherText);
bout.write (cipherText);
//fout.write (cipherText);
System.out.println ("resultado "+new String (cipherText,"UTF-8"));
bout.close ();
fout.close ();
File fich_descif = AlgoritmoDES.Descifrador (clave_simetrica,fichero_cifrado);
return fichero_cifrado;
Gracias por vuestro tiempo.
Tengo un pequeño problema que no se como resolver. El problema en cuestión es que cifro un fichero xml con el algoritmo DES, guardando el fichero cifrado. Luego cuando intento descifrar el fichero cifrado, resulta que me lo descifra pero me mete un montón de basura (caracteres raros), que no aparece en el original.
Alguien sabe porque puede ocurrir esto.
Un ejemplo de lo que me aparece.
<num__hijo>2</num__hijo>
?3p??}B</apell1>
<apell2>B</ap???*?M?y <nombre>B</nombre>??P]??`? <sexo>V</sexo>
<fec__nacimiento>02/02/2KVicy?__nacimiento>
<fec__nacimiento__(????>N</fec__nacimiento__ficticia>
<lugar__nacimiento></lugar__nacimiento>
<lugar__residencia></lugar__residencia>
</lista__hijos>
<lista__hijos/>
</hijos>
<panel>
<observaciones__paises__transito>
<str?MkO??FRANCIA</string>??P]??`?Yu8@=??>OBS UK</string>
</observaciones__paises__transito>
Aqui un ejemplo del codigo que utilizo para cifrar y para descifrar sería igual pero descifrando.
Security.addProvider (new BouncyCastleProvider ()); // Usa provider BC
//
/* PASO 1: Crear e inicializar clave */
String textoPlano = "";
/*KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(128);
Key clave = keyGen.generateKey();*/
//Key clave = KeyDES.ObtenerKey();
SecretKeySpec key = new SecretKeySpec (clave_simetrica, "DES");
/* PASO 2: Crear cifrador */
Cipher cifrador= Cipher.getInstance ("DES/ECB/PKCS7Padding","BC");
// Algoritmo DES
// Modo : ECB (Electronic Code Book)
//
/* PASO 3a: Inicializar cifrador en modo CIFRADO */
cifrador.init (Cipher.ENCRYPT_MODE, key);
File fichero_cifrado = new File ("C://Antonio//fichero_cifrado.xml");
System.out.println ("fichero_cifrado "+fichero_cifrado);
FileOutputStream fout = new FileOutputStream (fichero_cifrado);
BufferedOutputStream bout = new BufferedOutputStream (fout);
File fichero = new File ("C://Antonio//01022007_113154.xml");
byte[] bufferPlano = new byte[Integer.parseInt (String.valueOf (fichero.length ()))];
FileInputStream fin = new FileInputStream (fichero);
BufferedInputStream bin = new BufferedInputStream(fin);
int total = 0;
int bytesLeidos = bin.read (bufferPlano);
//int bytesLeidos = fin.read (bufferPlano);
//while(bytesLeidos != -1)
//{ // Mientras no se llegue al final del fichero
total = total + bytesLeidos;
System.out.println ("total "+total);
textoPlano = textoPlano + new String (bufferPlano);
//bytesLeidos = bin.read (bufferPlano,0, 1024);
//bytesLeidos = in.read (bufferPlano,0, 1024);
System.out.println ("bytesleidos "+bytesLeidos);
//}
bin.close ();
fin.close ();
byte[] textofinal = textoPlano.getBytes ();
/* Leer fichero de 1k en 1k y pasar fragmentos leidos al cifrador */
byte[] cipherText = cifrador.doFinal (textofinal,0,total);
System.out.println ("cipherText "+cipherText);
bout.write (cipherText);
//fout.write (cipherText);
System.out.println ("resultado "+new String (cipherText,"UTF-8"));
bout.close ();
fout.close ();
File fich_descif = AlgoritmoDES.Descifrador (clave_simetrica,fichero_cifrado);
return fichero_cifrado;
Gracias por vuestro tiempo.
Valora esta pregunta


0