/*
This Aglet do the dispatching ,loading Sec information from the file
and encryption and return back to home to send values to Decrypt message
*/
//the folder examples contained in ASDK folders
//and considered by ASDK as default folder where
//to find all agent projects
//the folder Sec is sub folder at examples folder
//I created to place my java classes in
package examples.Sec;
//com.ibm.aglet is an aglet package has
//set of classes have to with aglet contained again
//in ASDK folders , the star (*) means to the ability
//to reuse all the classes at the package
import com.ibm.aglet.*;
//Interface MobilityListener support the methods
//have to do with aglet transfering for example
//onDispatching() called before the aglet dispatch
//since calling disaptch(URL) method
import com.ibm.aglet.event.*;
//package needed for networking applications,I import it
//since I need to use th URL java class to point for
//the host on the WWW , that contains the user name and password
import java.net.*;
//package at JDK contains the Scanner class and others
import java.util.*;
//Package contained into JDK contains the classes to handle
//I/O from data storage ,for example contains the File class
//to read or write from or to a simple file
import java.io.*;
public class Encrypt extends Aglet
{
//Declare the variables to handle the secret information
//from the remote host at the WWW
public String userName=new String();
public String password=new String();
//Declare and initialiation the indicators to
//determine the aglet location
//_At_Home status means the aglet newly created and
//did not transfere and read the secret information
//from the remote host and the dispatch method shall be called
public int _AT_HOME=0;
//_At_REMOTE means the aglet reached successfully to the
//remote host over the WWW containing the user name and the password
//and so the loading and encryption shall take place
public int _At_REMOTE=1;
//_Back_AT_HOME means the aglet read the secret information
//and encrypt them and return back to the home and the time is
//suitable to fill in tabular message and send them to the decrypt aglet
public int _Back_AT_HOME=2;
//_STATE can be _AT_HOME,_At_REMOTE,_Back_AT_HOME
public int _STATE=0;
//create interface to handle the file open,close,read
//or write
public static File infile=new File("C:/Secret.txt");;
public static Scanner input;
//help to store the secret infoemation in hash table with two
//columns one for keys and other for value , to reach val1 you
//need to know Key1
Message secretInformation=new Message("secretInformation");
//method executed when the aglet created,dispatched,reverted
//main operations of the aglet can take place here
public void run()
{
if(_STATE==_AT_HOME)
{
try
{
//the _STATE changed since next time the run()
//executed the aglet will be at remote host
_STATE=_At_REMOTE;
//the method will dispatch (say push !)
//to the URL specified staticall by the value :
//"atp://OMAR-PC:1024" at the object of type
//URL
dispatch(new URL("atp://OMAR-PC:1024"));
}//end try block
catch(Exception e)
{
//reaching to this block the failure
//to dispatch takes place
System.out.println("Error -- "+e);
}//end catch block
}//end if statement
else if(_STATE==_At_REMOTE)
{
//array will help the Encrypt() method
//to store each character in the user name and the
//password and value of ASCII code of each character
//at the user name and password
Global.array_char=new char[10];
Global.array_int=new int[10];
try
{
//Scanner class can hanbdle the read operation from
//text file contains the secret information
input=new Scanner(infile);
//read the user name from the file and store at the
//username variable using next() methos that will read entire
//line
userName=input.next();
//convert the user name from string to array of
//characters for issues have to with the methodology
//how encrypt method encrypt data
Global.array_char=userName.toCharArray();
//print the user name as clear text on the console
System.out.println("user name as clear text : "+userName);
encrypt();//call the encrypt method
userName="";
//convert the array of characters ,contains the characters
//of the username in encrypted manner,to string
userName=Global.array_in_string();
//read the password from the file and store at the
//password variable using next() methos that will read entire
//line
password=input.next();
//print the Password as clear text on the console
System.out.println("Password as clear text : "+password);
//convert the array of characters ,contains the characters
//of the username in clear text manner,to string
Global.array_char=password.toCharArray();
encrypt();//call the encrypt method
password="";
//convert the array of characters ,contains the characters
//of the password in encrypted manner,to string
password=Global.array_in_string();
System.out.println();
//print the username and the password in encrypted manner
System.out.println("user name after encryption : "+userName);
System.out.println("password after encryption : "+password);
System.out.println();
}//end try block
catch(Exception n)
{
//catch block can catch any type of exception , which
//may be fail to find the file to open
System.out.println("to open the file : "+n);
}//end catch block
try
{
//the _STATE changed since next time the run()
//executed the aglet will be at remote host
_STATE=_Back_AT_HOME;
//the method will dispatch (say push !)
//to the URL specified staticall by the value :
//"atp://OMAR-PC:4434" at the object of type
//URL that defines the aglet Home URL
//over WWW
dispatch(new URL("atp://OMAR-PC:4434"));
}//end try block
catch(Exception e)
{
//reach to this block means an exception
//takes place that may be failure to dispatch
System.out.println("Fail to return home ");
}//end catch block
}//end if statement
else if(_STATE==_Back_AT_HOME)
{
//filll the hash table at the object of type message
//that will contain two pairs one for the password and its keyword
//and the other the user name and the paired keyword
//setArg is a method at Message class takes two parameters
//to fill hash table one the key and the other is the value matched
//by the Key
secretInformation.setArg("User Name",userName);
secretInformation.setArg("Password",password);
try
{
//Father Aglet contains the decryptProxy that object
//of type AgletProxy that help to handle Decrypt Aglet
//and will be used here to send message to Decrypt Aglet
//I defined the decryptProxy as static variable at Father Aglet
//And so I can use it here at the current aglet
Father.decryptProxy.sendMessage(secretInformation);
dispose();//the current aglet killed here
}//end try block
catch(InvalidAgletException e)
{
//Signals that the aglet proxy
//is not valid any longer.
System.out.println("InvalidAgletException is thrown");
}//end catch block
catch(NotHandledException e)
{
//the message not handled by the
//destined to Aglet
System.out.println("NoHandledException is thrown");
}//end try block
catch(MessageException e)
{
//Signals that the exception occured
//while processsing the message.
System.out.println("MessageException thrown");
}//end catch block
catch(Exception e)
{
//The class Exception and its subclasses are
//a form of Throwable that indicates conditions
//that a reasonable application might want to catch
System.out.println("fail to send due to : "+e);
}//end catch block
}//end nested if have to do with aglet location
}//end the run() method
public void encrypt()
{
//for lop on the characters of the array of characters
//represents the clear text usename and password
for(int h1=0;
h1 < Global.array_char.length;h1++)
{
Global.array_int[h1]=(int)Global.array_char[h1];
if(Global.array_int[h1]>=97&&Global.array_int[h1]<=122)
{
//handle the lower case letters
if((Global.array_int[h1]+3)> 122)
{
//for example we have z we need to encrypt to
//c lower case letter and the below formula do that and then
//convert the ASCII code into letter again and store into array
//of characters
Global.array_int[h1]=Global.array_int[h1]+3;
Global.array_int[h1]=124-Global.array_int[h1];
Global.array_int[h1]+=96;
Global.array_char[h1]=(char)Global.array_int[h1];
}
else
{
//the process that you need to encrypt Only to
//move three forwarding steps in the alphabetical order
Global.array_int[h1]=Global.array_int[h1]+3;
Global.array_char[h1]=(char)Global.array_int[h1];
}
}
else if(Global.array_int[h1]>=65&&Global.array_int[h1]<=90)
{
//handle the upper case letters
if((Global.array_int[h1]+3)>90)
{
//for example we have Y we need to encrypt to
//B lower case letter and the below formula do that and then
//convert the ASCII code into letter again and store into array
//of characters
Global.array_int[h1]=Global.array_int[h1]+3;
Global.array_int[h1]=Global.array_int[h1]%90;
Global.array_int[h1]+=64;
Global.array_char[h1]=(char)Global.array_int[h1];
}
else
{
//the process that you need to encrypt Only to
//move three forwarding steps in the alphabetical order
Global.array_int[h1]=Global.array_int[h1]+3;
Global.array_char[h1]=(char)Global.array_int[h1];
}
}
//handle the case that the current item
//not english alphabetical letter
else Global.array_char[h1]=(char)Global.array_int[h1];
}//end for loop that check the characters of the array of characters
//contains the user name and password characters
}//end encrypt() method
}//end Encrypt Aglet
Saturday, August 14, 2010
My Agent Security Scenario: The Code-Encrypt Class
Subscribe to:
Post Comments (Atom)






No comments:
Post a Comment