Java - No funciona mi "if"

 
Vista:
sin imagen de perfil
Val: 87
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

No funciona mi "if"

Publicado por Tomas (76 intervenciones) el 05/11/2015 23:37:35
Hola, el programa lo que hace es:
- Pedir un fichero ( con la ruta completa).
- Si el fichero existe(este encriptado o no) pedir la llave del usuario (AES192, CBC)
- generar la secretkey a partir de la llave del usuario
- leer todos los bytes del fichero y guardarlos en una variable de tipo byte [ ].

Ahora, si { (si no es un fichero .aes), guarda en "method", la accion (encriptar de tipo int) y el "path " donde se va a escibir el fichero+extension} else{fichero acaba en ".aes" (osea, encriptado), guarda en una variable "method" la accion (desencriptar) y el "path" donde se va a escribir el fichero+extension.}

- llama al metodo cifrar ( le pasa los argumentos), que esta en otra clase.
- crea y escribe el fichero.

mi problema es que nunca entra en el else. Creo que el error esta en comprobar si acaba en ".aes". Aunque no le veo el error al if. el metodo funciona, cifra y desifra como tiene que ser, pero no consigo que me genere el archivo txt (desifrado), es decir que entre en el else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
static Clase dec = new Clase();
 
	private static Path InputPath;
	private static Path FinalPath;
	private static byte[] Data;
	private static byte[] cipherOutput;
	private static SecretKey skey;
	private static int keySize=192; //tama�o de la password 192 bits /8 bits = 24 bytes , 1 byte x caracter: 24 caracteres max.
 
 
	public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
 
		int method = 0;
		Scanner input = new Scanner(System.in);
		String fichero= null,UserPass;
 
		//Pedimos fichero
		System.out.println("Introduce el fichero:");
		fichero = input.nextLine();
		FileInputStream in = new FileInputStream(fichero);
 
		InputPath = Paths.get(fichero);
 
		System.out.println("Escribe la password:");
		UserPass = input.nextLine();
		skey = dec.passwordKeyGeneration(UserPass,keySize);
 
		Data = Files.readAllBytes(InputPath);
 
		System.out.println(UserPass+keySize);
 
		if(!InputPath.endsWith(".aes")){
 
			method=Cipher.ENCRYPT_MODE;
			String p = new String (InputPath+".aes");
			FinalPath = Paths.get(p);
			System.out.println(FinalPath);
		}
		else{
			method=Cipher.DECRYPT_MODE;
			String p = new String (InputPath+"");
			FinalPath = Paths.get(p);
			System.out.println(FinalPath+" nooo");
			}
 
		cipherOutput = dec.cipher(skey, Data,method);
 
		 Files.write(FinalPath, cipherOutput);
 
 
		//in.close();
	}
 
	}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

No funciona mi "if"

Publicado por Renzo (232 intervenciones) el 06/11/2015 16:13:25
Hola
te recomiendo usar:


if( InputPath.indexOf(".aes") <0 ){

Renzo


www.imagineanddo.com
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

No funciona mi "if"

Publicado por Andrés (340 intervenciones) el 06/11/2015 17:05:17
Cambia la condición del if a:

1
!InputPath.toFile().getName().endsWith(".aes")
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 87
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

No funciona mi "if"

Publicado por Tomas (76 intervenciones) el 07/11/2015 16:18:16
Funcionó. Gracias.
Aqui el codigo modificado si alguien lo quiere :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if(!InputPath.toFile().getName().endsWith(".aes")){
 
	method=Cipher.ENCRYPT_MODE;
	String fileNameWithOutExt = fichero.replaceFirst("[.][^.]+$", "");
	String F = fileNameWithOutExt+".aes";
	FinalPath = Paths.get(F);
	System.out.println(FinalPath);
}
else{
	method=Cipher.DECRYPT_MODE;
	String fileNameWithOutExt = fichero.replaceFirst("[.][^.]+$", "");
	String F = fileNameWithOutExt+".txt";
	FinalPath = Paths.get(F);
	System.out.println(FinalPath);
	}
 
	cipherOutput = dec.cipher(skey, Data,method);
 
		Files.write(FinalPath, cipherOutput);
	in.close();
	f.delete();
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar