Thursday, 8 September 2011

Encryption & Decryption

import java.io.*;
public class encrypt
{
char[] temp=new char[100];
char current,s;
int asc;
String s1;
int i=0;
void getdata(int y)throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("enter the text file name to encrypt or decrypt");
s1=in.readLine();
File file=new File(s1);
if(!file.exists())
{
System.out.println(s1+"does not exists");
return;
}
FileInputStream fis=new FileInputStream(file);
while(fis.available()>0)
{
temp[i]=(char)fis.read();
i=i+1;
}
int n=i;
FileOutputStream fos=new FileOutputStream(file,false);
for(int j=0;j<n;j++)
{
current=temp[j];
asc=(int)current;
asc=asc+y;
s=(char)asc;
fos.write(s);
}
fos.close();
}
public static void main(String args[])throws IOException
{
encrypt x=new encrypt();
char[] temp=new char[100];
int asc;
int i=0;
String s1;
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("1.encryption");
System.out.println("2.decryption");
System.out.println("\tenter ur choice");
int ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("encrypting the file");
x.getdata(4);
System.out.println("the file is encrypted successfully");
break;
case 2:
System.out.println("decrypting the file");
x.getdata(-4);
System.out.println("the file is decrypted successfully");
break;
}
}
catch(IOException e)
{
System.out.println("file not found");
}
}
}

No comments:

Post a Comment