[Jreepad-CVS] jreepad/src/jreepad/io EncryptedWriter.java, NONE, 1.1 EncryptedReader.java, NONE, 1.
Brought to you by:
danstowell
From: PeWu <pe...@us...> - 2007-09-28 14:29:28
|
Update of /cvsroot/jreepad/jreepad/src/jreepad/io In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv28006/src/jreepad/io Modified Files: AutoDetectReader.java Added Files: EncryptedWriter.java EncryptedReader.java Log Message: Added experimental encrypted file format Index: AutoDetectReader.java =================================================================== RCS file: /cvsroot/jreepad/jreepad/src/jreepad/io/AutoDetectReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AutoDetectReader.java 3 Apr 2007 12:41:30 -0000 1.2 --- AutoDetectReader.java 28 Sep 2007 14:29:22 -0000 1.3 *************** *** 24,27 **** --- 24,28 ---- import jreepad.JreepadTreeModel; + import jreepad.ui.PasswordDialog; /** *************** *** 37,44 **** --- 38,48 ---- TreepadReader treepadReader; + EncryptedReader encryptedReader; + public AutoDetectReader(String encoding, boolean autoDetectHtmlArticles) { xmlReader = new XmlReader(); treepadReader = new TreepadReader(encoding, autoDetectHtmlArticles); + encryptedReader = new EncryptedReader(this); // Use this reader as the underlying reader } *************** *** 52,56 **** in.reset(); // reset stream, so the specific readers read from the beginning ! if (currentLine.startsWith("<?xml version=\"1.0\"")) { // Try and find out what character encoding to use --- 56,60 ---- in.reset(); // reset stream, so the specific readers read from the beginning ! if (currentLine.startsWith("<?xml")) { // Try and find out what character encoding to use *************** *** 81,84 **** --- 85,96 ---- return treepadReader.read(in); } + else if (currentLine.startsWith(EncryptedWriter.HEADER)) + { + String password = PasswordDialog.showPasswordDialog("This file is encrypted. Please enter password:"); + if (password == null) + throw new IOException("Could not decrypt. No password entered."); + encryptedReader.setPassword(password); + return encryptedReader.read(in); + } else { --- NEW FILE: EncryptedReader.java --- /* Jreepad - personal information manager. Copyright (C) 2004-2006 Dan Stowell This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The full license can be read online here: http://www.gnu.org/copyleft/gpl.html */ package jreepad.io; import java.io.IOException; import java.io.InputStream; import java.security.GeneralSecurityException; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.spec.SecretKeySpec; import jreepad.JreepadPrefs; import jreepad.JreepadTreeModel; /** * Reads encrypted input into Jreepad. This reader is constructed with * any other reader as the actual file format which will used when decrypted. * * @version $Id: EncryptedReader.java,v 1.1 2007/09/28 14:29:22 pewu Exp $ */ public class EncryptedReader implements JreepadReader { private JreepadReader reader; private String password = ""; public EncryptedReader(JreepadReader reader) { this.reader = reader; } public JreepadTreeModel read(InputStream in) throws IOException { // Read header while (in.read() != '\n'); Cipher cipher = null; try { cipher = Cipher.getInstance(EncryptedWriter.ALGORITHM); Key key = new SecretKeySpec(password.getBytes(), EncryptedWriter.ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); } catch (GeneralSecurityException e) { throw new IOException(e.toString()); } InputStream in2 = new CipherInputStream(in, cipher); JreepadTreeModel document; try { document = reader.read(in2); } catch (IOException e) { throw new IOException("Password incorrect or read problem occurred", e); } document.setFileType(JreepadPrefs.FILETYPE_XML_ENCRYPTED); document.setPassword(password); return document; } public void setPassword(String password) { this.password = password; } } --- NEW FILE: EncryptedWriter.java --- /* Jreepad - personal information manager. Copyright (C) 2004-2006 Dan Stowell This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The full license can be read online here: http://www.gnu.org/copyleft/gpl.html */ package jreepad.io; import java.io.IOException; import java.io.OutputStream; import java.security.GeneralSecurityException; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import javax.crypto.spec.SecretKeySpec; import jreepad.JreepadTreeModel; /** * Writes an encrypted file. This writer is constructed with * any other writer as the actual file format which will be encrypted. * * @version $Id: EncryptedWriter.java,v 1.1 2007/09/28 14:29:22 pewu Exp $ */ public class EncryptedWriter implements JreepadWriter { static final String ALGORITHM = "Blowfish"; static final String HEADER = "JreepadEncrypted"; private JreepadWriter writer; private String password = ""; public EncryptedWriter(JreepadWriter writer) { this.writer = writer; } public void write(OutputStream out, JreepadTreeModel document) throws IOException { out.write(HEADER.getBytes()); out.write("\n".getBytes()); Cipher cipher = null; try { cipher = Cipher.getInstance(ALGORITHM); Key key = new SecretKeySpec(password.getBytes(), ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); } catch (GeneralSecurityException e) { throw new IOException(e.toString()); } OutputStream out2 = new CipherOutputStream(out, cipher); writer.write(out2, document); } public void setPassword(String password) { this.password = password; } } |