Como adiciono dados (String) a um ficheiro cifrado?
Acompanhem o codigo:
System.out.println("Cifrando o ficheiro de alunos");
System.out.println("Aguarde...");
String fichVt = "e:/EstudandoDuro/alunos.txt"; // ficheiro normal
String fichCif = "e:/AprendendoSolucoes/alns.txt"; // ficheiro cifrado
Cipher cifrar = Cipher.getInstance("DES");
cifrar.init(Cipher.ENCRYPT_MODE, ks);
//Ficheiro a ser Encriptado
InputStream simples = new FileInputStream(fichVt);
// Ficheiro Encriptado
OutputStream cifrado = new FileOutputStream(fichCif);
byte[] buffer = new byte[1024];
byte[] linha;
String texto_cifrado = new String();
int fim_arquivo = -1;
int leidos; // numero de bytes lidos
leidos = simples.read(buffer);
while(leidos!=fim_arquivo)
{
linha = cifrar.update(buffer, 0, leidos);
texto_cifrado = texto_cifrado + new String(linha, "ISO-8859-1");
leidos = simples.read(buffer);
}
simples.close();
linha = cifrar.doFinal();
texto_cifrado = texto_cifrado + new String(linha,"ISO-8859-1");
cifrado.write(texto_cifrado.getBytes("ISO-8859-1"));
System.out.println("Ficheiro Cifrado");
// adicionando nomes
System.out.println("Adicionando nomes ao ficheiro");
// o codigo abaixo adiciona o nome mais ele aparece não cifrado
String ch = "Manuel Tiago"; // aluno a ser adicionado ao ficheiro
OutputStream escr = new FileOutputStream(fichCif ,true);
OutputStreamWriter osw = new OutputStreamWriter(escr);
BufferedWriter bw = new BufferedWriter(osw);
bw.append(ch.trim());
bw.newLine();
bw.close();
Se eu quiser adicionar mais dados (Linhas diferentes) no
- ficheiro cifrado, o que fazer?