3 Replies - 405 Views - Last Post: 05 January 2013 - 05:52 PM Rate Topic: -----

#1 shashank24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-January 13

How to encrypt the documents that get saved in this notepad like app

Posted 05 January 2013 - 03:17 AM

package niitproject;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class NIITProject extends JFrame implements ActionListener {
    MenuBar mbar;
Menu file,edit,format,font,font1,font2;
MenuItem item1,item2,item3,item4;
MenuItem item5,item6,item7,item8,item9,item10,item11;
MenuItem fname1,fname2,fname3,fname4;
MenuItem fstyle1,fstyle2,fstyle3,fstyle4;
MenuItem fsize1,fsize2,fsize3,fsize4;
JPanel mainpanel;
TextArea text;
Font f;
String command=" ";
GregorianCalendar gcalendar;
String str=" ";
String path,content;
int i=0;
int pos1;
public NIITProject(String str)
{
super(str);

mainpanel=new JPanel();
mainpanel=(JPanel)getContentPane();
mainpanel.setLayout(new GridLayout());

mbar=new MenuBar();
setMenuBar(mbar);
file=new Menu("File");
edit=new Menu("Edit");
format=new Menu("Format");
font=new Menu("Font");
font1=new Menu("Font Style");
font2=new Menu("Size");

file.add(item1=new MenuItem("New..."));
file.add(item2=new MenuItem("Open"));
file.add(item3=new MenuItem("Save As..."));
file.add(item8=new MenuItem("Print"));
file.add(item4=new MenuItem("Exit"));
mbar.add(file);


edit.add(item5=new MenuItem("Cut"));
edit.add(item6=new MenuItem("Copy"));
edit.add(item7=new MenuItem("Paste"));
edit.add(item9=new MenuItem("Time/Date"));


mbar.add(edit);

format.add(font);
format.add(font1);
format.add(font2);
format.add(item10=new MenuItem("Background Color"));
format.add(item11=new MenuItem("Font Color"));

font.add(fname1=new MenuItem("Courier"));
font.add(fname2=new MenuItem("Sans Serif"));
font.add(fname3=new MenuItem("Monospaced"));
font.add(fname4=new MenuItem("Symbol"));

font1.add(fstyle1=new MenuItem("Regular"));
font1.add(fstyle2=new MenuItem("Bold"));
font1.add(fstyle3=new MenuItem("Italic"));
font1.add(fstyle4=new MenuItem("Bold Italic"));

font2.add(fsize1=new MenuItem("12"));
font2.add(fsize2=new MenuItem("14"));
font2.add(fsize3=new MenuItem("18"));
font2.add(fsize4=new MenuItem("20"));

mbar.add(format);



item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
item6.addActionListener(this);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
item10.addActionListener(this);
item11.addActionListener(this);
fname1.addActionListener(this);
fname2.addActionListener(this);
fname3.addActionListener(this);
fname4.addActionListener(this);
fstyle1.addActionListener(this);
fstyle2.addActionListener(this);
fstyle3.addActionListener(this);
fstyle4.addActionListener(this);
fsize1.addActionListener(this);
fsize2.addActionListener(this);
fsize3.addActionListener(this);
fsize4.addActionListener(this);


text=new TextArea();
mainpanel.add(text,BorderLayout.CENTER);

f=new Font("Monotype Corsiva",Font.PLAIN,15);
text.setFont(f);

}

   public void actionPerformed(ActionEvent ae)
{
command=(String)ae.getActionCommand();

if(command.equals("New..."))
{
try
{

dispose();
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
NIITProject note1 = new NIITProject("Untitled-Notepad");
note1.setSize(d);
note1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
note1.setVisible(true);
}
catch(Exception abcdefghij)
{

}
}
if(command.equals("Open"))
{
try
{
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(mainpanel);
File file=jfc.getSelectedFile();
text.setText(null);
try
{
FileInputStream fin=new FileInputStream(file);
int data=0;
while((data=fin.read())!=-1)
{
text.append(String.valueOf((char)data));
}
}catch(IOException e1)
{
e1.printStackTrace();
}
}
catch(Exception ez)
{

}
}
if(command.equals("Save As..."))
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = fc.showSaveDialog(this);
if(r==fc.CANCEL_OPTION)
return;
File myfile = fc.getSelectedFile();
if(myfile==null || myfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Please enter a file name!","Error",JOptionPane.ERROR_MESSAGE);
return;
}
if(myfile.exists())
{
r = JOptionPane.showConfirmDialog(this, "A file with same name already exists!\nAre you sure want to overwrite?");
if(r!= 0)
return;
}
try
{
FileWriter fw = new FileWriter(myfile);
fw.write(text.getText());
String content = text.getText();
setTitle(myfile.getName()+" - Notepad");
fw.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"Failed to save the file","Error",JOptionPane.ERROR_MESSAGE);
}
}

if(command.equals("Print"))
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new Print());
if (pj.printDialog()) 
{

try 
{
pj.print();
}
catch (PrinterException e) 
{
System.out.println(e);
}

}

}


if(command.equals("Exit"))
{
//System.exit(0);
if(text.getText().equals("") || text.getText().equals(content))
System.exit(0);
else
{
int b = JOptionPane.showConfirmDialog(null, "The text has been changed.\nDo you want to save the changes?");
if(b==0)
{
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = fc.showSaveDialog(this);
if(r==JFileChooser.CANCEL_OPTION) {
        return;
    }
File myfile = fc.getSelectedFile();
if(myfile==null || myfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Please enter a file name!","Error",JOptionPane.ERROR_MESSAGE);
return;
}
if(myfile.exists())
{
r = JOptionPane.showConfirmDialog(this, "A file with same name already exists!\nAre you sure want to overwrite?");
if(r!= 0)
return;
}
try
{
FileWriter fw = new FileWriter(myfile);
fw.write(text.getText());
String content = text.getText();
setTitle(myfile.getName()+" - Notepad");
fw.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"Failed to save the file","Error",JOptionPane.ERROR_MESSAGE);
}
}
else if(b==1)
{
System.exit(0);
}
else if(b==2)
{
return;
}
}
}
if(command.equals("Cut"))
{
str=text.getSelectedText();
i=text.getText().indexOf(str);
text.replaceRange(" ",i,i+str.length());
}

if(command.equals("Copy"))
{
str=text.getSelectedText();
}

if(command.equals("Paste"))
{
pos1=text.getCaretPosition();
text.insert(str,pos1);
}

if(command.equals("Time/Date"))
{
gcalendar=new GregorianCalendar();
String h=String.valueOf(gcalendar.get(Calendar.HOUR));
String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
String s=String.valueOf(gcalendar.get(Calendar.SECOND));
String date=String.valueOf(gcalendar.get(Calendar.DATE));
String mon=String.valueOf(gcalendar.get(Calendar.MONTH));
String year=String.valueOf(gcalendar.get(Calendar.YEAR));
String hms="Time"+" - "+h+":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year;
int loc=text.getCaretPosition();
text.insert(hms,loc);
}
if(command.equals("Courier"))
{

String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Courier",fontStyle,fontSize);
text.setFont(f);
}
if(command.equals("Sans Serif"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Sans Serif",fontStyle,fontSize);
text.setFont(f);
}
if(command.equals("Monospaced"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Monospaced",fontStyle,fontSize);
text.setFont(f);
}

if(command.equals("Symbol"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font("Symbol",fontStyle,fontSize);
text.setFont(f);
System.out.println(f.getFamily());
}
if(command.equals("Regular"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.PLAIN,fontSize);
text.setFont(f);
}
if(command.equals("Bold"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.BOLD,fontSize);
text.setFont(f);
}
if(command.equals("Italic"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.ITALIC,fontSize);
text.setFont(f);
}
if(command.equals("Bold Italic"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
text.setFont(f);
}

if(command.equals("12"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,12);
text.setFont(f);
}

if(command.equals("14"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,14);
text.setFont(f);
}
if(command.equals("18"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,18);
text.setFont(f);
}
if(command.equals("20"))
{
String fontName=f.getName();
String fontFamily=f.getFamily();
int fontSize=f.getSize();
int fontStyle=f.getStyle();

f=new Font(fontName,fontStyle,20);
text.setFont(f);
}

if(command.equals("Background Color"))
{
Color bgColor= JColorChooser.showDialog(this,"Choose Background Color",text.getBackground());
if (bgColor != null)
{
text.setBackground(bgColor);
}
}

if(command.equals("Font Color"))
{
Color bgColor= JColorChooser.showDialog(this,"Choose Background Color",text.getBackground());
if (bgColor != null)
{
text.setForeground(bgColor);
}
}

}

public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception xyz)
{

}
try
{
NIITProject note = new NIITProject("Untitled-Notepad");
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
note.setSize(d);
note.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
note.setVisible(true);
}
catch(Exception hello)
{

}
}
}
class Print implements Printable
{

public int print(Graphics g, PageFormat pf, int pageIndex) 
{
if (pageIndex != 0)
{ 
return NO_SUCH_PAGE;
}    
else
{
return PAGE_EXISTS;
}

}
}


I have written this code for creating a very basic notepad application. I have written it on my own and I have not used any GUI editors. I am having problem in encrypting the files for security and also in having to remember the passwords for the individual file. please help. thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: How to encrypt the documents that get saved in this notepad like app

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: How to encrypt the documents that get saved in this notepad like app

Posted 05 January 2013 - 04:41 AM

Unless I completely missed it, I see nothing regarding any kind of encryption or passwords.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to encrypt the documents that get saved in this notepad like app

Posted 05 January 2013 - 04:54 PM

Learn how to indent your code, the way it is it is a real nightmare to read
So I will not waste my time trying to verify/validate farrell2k's sayings, He must be right as usual. So where is your encoding code ?
Was This Post Helpful? 1
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: How to encrypt the documents that get saved in this notepad like app

Posted 05 January 2013 - 05:52 PM

In the place where you write to file using the getText() method. Right there you need to implement some sort of method to encrypt the data before writing. Conversely, in the place where you select the file you would also need to open some sort of Dialog with the relevant decryption password and proceed to decrypt the retrieved bytes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1