<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Últimas mensagens do tópico "Encriptar ficheiro"]]></title>
		<link>http://www.guj.com.br/posts/list/5.java</link>
		<description><![CDATA[Últimas mensagens enviadas no tópico "Encriptar ficheiro"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Encriptar ficheiro</title>
				<description><![CDATA[ Boas pessoal,<br /> Esta é a classe que tem os metodos de encriptação e desencriptação que eu apanhei no google em java almanac em [url]http://www.exampledepot.com/egs/javax.crypto/DesFile.html[/url]<br /> [code]<br />     public class DesEncrypter {<br />         Cipher ecipher;<br />         Cipher dcipher;<br />     <br />         DesEncrypter(SecretKey key) {<br />             // Create an 8-byte initialization vector<br />             byte[] iv = new byte[]{<br />                 (byte)0x8E, 0x12, 0x39, (byte)0x9C,<br />                 0x07, 0x72, 0x6F, 0x5A<br />             };<br />             AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);<br />             try {<br />                 ecipher = Cipher.getInstance(&quot;DES/CBC/PKCS5Padding&quot;);<br />                 dcipher = Cipher.getInstance(&quot;DES/CBC/PKCS5Padding&quot;);<br />     <br />                 // CBC requires an initialization vector<br />                 ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);<br />                 dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);<br />             } catch (java.security.InvalidAlgorithmParameterException e) {<br />             } catch (javax.crypto.NoSuchPaddingException e) {<br />             } catch (java.security.NoSuchAlgorithmException e) {<br />             } catch (java.security.InvalidKeyException e) {<br />             }<br />         }<br />     <br />         // Buffer used to transport the bytes from one stream to another<br />         byte[] buf = new byte[1024];<br />     <br />         public void encrypt(InputStream in, OutputStream out) {<br />             try {<br />                 // Bytes written to out will be encrypted<br />                 out = new CipherOutputStream(out, ecipher);<br />     <br />                 // Read in the cleartext bytes and write to out to encrypt<br />                 int numRead = 0;<br />                 while ((numRead = in.read(buf)) &gt;= 0) {<br />                     out.write(buf, 0, numRead);<br />                 }<br />                 out.close();<br />             } catch (java.io.IOException e) {<br />             }<br />         }<br />     <br />         public void decrypt(InputStream in, OutputStream out) {<br />             try {<br />                 // Bytes read from in will be decrypted<br />                 in = new CipherInputStream(in, dcipher);<br />     <br />                 // Read in the decrypted bytes and write the cleartext to out<br />                 int numRead = 0;<br />                 while ((numRead = in.read(buf)) &gt;= 0) {<br />                     out.write(buf, 0, numRead);<br />                 }<br />                 out.close();<br />             } catch (java.io.IOException e) {<br />             }<br />         }<br />     }<br /> [/code]<br /> <br /> <br /> eu criei uma classe que tem um metodo que chama a class acima e usa o metodo encript da seguinte forma : <br /> [code]<br />    try {<br /> <br />                       key = KeyGenerator.getInstance(&quot;DES&quot;).generateKey();<br />                       <br />                       System.out.println(key);<br /> <br />                       // Create encrypter/decrypter class<br />                       DesEncrypter encrypter = new DesEncrypter(key);<br /> <br />                       // Encrypt<br />                       String nfile = new String (lemails.getSelectedValue().toString().trim());<br />                       encrypter.encrypt(new FileInputStream(&quot;processados/&quot;+nfile),<br />                       new FileOutputStream(&quot;processados/&quot;+ nfile+&quot;_&quot;+key));<br /> <br />                       // Decrypt<br />                       /*<br />                       encrypter.decrypt(new FileInputStream(&quot;ciphertext&quot;),<br />                               new FileOutputStream(&quot;cleartext2&quot;));<br />                      */ <br />                       <br />                   } catch (Exception e) {<br />                       e.printStackTrace();<br />                   }<br /> [/code]<br /> <br /> tudo funciona beleza .<br /> <br /> O problema está em desencriptar este ficheiro encriptado. Quando tento usar o metodo decrypt  ele criar um novo ficheiro mas ainda encriptado .<br /> [code]<br /> ~try {<br />                        <br />                        <br />                    SecretKey key = KeyGenerator.getInstance(&quot;DES&quot;).generateKey();  <br />                    <br />                    <br />                    System.out.println(key);<br /> <br />                       DesEncrypter encrypter = new DesEncrypter(key);<br /> <br />                       String nfile = new String (lemails.getSelectedValue().toString().trim());<br />   <br />                       // Decrypt<br />                     <br />                       encrypter.decrypt(new FileInputStream(&quot;processados/&quot;+nfile),<br />                               new FileOutputStream(&quot;processados/cleartext2.txt&quot;));     <br />                     <br />                 }catch(Exception ex){                  <br />                     ex.printStackTrace();<br />                     <br />                 }<br /> [/code]<br /> <br /> minha duvida é :<br /> <br /> Como faço para poder desencriptar este ficheiro ]]></description>
				<guid isPermaLink="true">http://www.guj.com.br/prepost/195412/981197/encriptar-ficheiro
</guid>
				<link>http://www.guj.com.br/prepost/195412/981197/encriptar-ficheiro
</link>
				<pubDate><![CDATA[Thu, 20 Nov 2008 14:19:17]]> GMT</pubDate>
				<author><![CDATA[ LuisClaudioPJ]]></author>
			</item>
	</channel>
</rss>
