Sunday, 30 November 2014

Caesar Applet Demo in Java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CaesarApplet extends Applet implements ActionListener
{
    Label enterText;
    TextField text;

    Label enterKey;
    TextField key;

    Button encrypt;
    Button decrypt;

    Label result_text;
    Label result;

    public void init()
    {
            enterText = new Label("Enter Text: ");
            text = new TextField(25);
            enterKey = new Label("Enter key: ");
            key = new TextField(25);
            encrypt = new Button("Encrypt Text");
            decrypt = new Button("Decrypt Text");
            result_text = new Label("Resultant Text: ");
            result = new Label();

            add(enterText);
            add(text);
            add(enterKey);
            add(key);

            add(encrypt);
            encrypt.addActionListener(this);
            add(decrypt);
            decrypt.addActionListener(this);

            add(result_text);
            add(result);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == encrypt)
        {
            String pt = text.getText();
            String k = key.getText();
            int encrypt_key = Integer.parseInt(k);

            String res = encrypt(pt,encrypt_key);
            result.setText(res);
        }
        else if(ae.getSource() == decrypt)
        {
            String ct = text.getText();
            String k = key.getText();
            int decrypt_key = Integer.parseInt(k);

            String res = decrypt(ct,decrypt_key);
            result.setText(res);
        }
    }

    static public String encrypt(String pt,int encrypt_key)
    {
        char c;
        int num,i;

        StringBuffer sb = new StringBuffer(pt);
        int len = sb.length();

        for(i=0;i

Output:







Saturday, 29 November 2014

ActionListener in Java


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class FormAppletDemo extends Applet implements ActionListener
{
    Label entername;
    TextField name;
    Label enteremail;
    TextField email;
    Button click;
    Label result;
    public void init()
    {
        entername = new Label("Enter your name: ");
        name = new TextField(30);
        enteremail = new Label("Enter your email: ");
        email = new TextField(30);
        click = new Button("Click Here");
        result = new Label();

        add(entername);
        add(name);
        add(enteremail);
        add(email);
        add(click);
        click.addActionListener(this);
        add(result);
    }

    public void actionPerformed(ActionEvent ae)
    {
        String s = name.getText();
        String e = email.getText();
        String n = "";

        if(s.equals(n) || e.equals(n))
        {
            result.setText("Please enter appropriate information...");
        }
        else
        {
            result.setText("Welcome to Blogger's Group");
        }
    }
}

/*


*/
Output

Saturday, 1 November 2014

Caesar Cipher

import java.io.*;
public class Caesar_cipher
{
    public static void main(String soham[]) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // enter string to encrypt
        System.out.print("\nEnter String to Encrypt: ");
        String pt = br.readLine();

        // enter key to encrypt
        System.out.print("\nEnter key to Encrypt: ");
        int key = Integer.parseInt(br.readLine());

        // passing plain_text and key to method encrypt
        String ct = encrypt(pt,key);

        // printing encrypted text
        System.out.print("\nEncrypted Text: " + ct);

        System.out.println();

        // enter key to decrypt the encrypted text
        System.out.print("\nEnter key to Decrypt: ");
        int dkey = Integer.parseInt(br.readLine());

        // comparing encrypted_key and decrypted_key
        if(key == dkey)
        {
            // if match is found passing cipher_text and key to method decrypt
            String pt1 = decrypt(ct,key);
            System.out.print("\nDecrypted Text: " + pt1);
        }
        else
        {
            // if match is not found cipher_text will not be decrypted...
            System.out.print("\nNot Authorized...");
        }

        System.out.println();
    }

    public static String encrypt(String pt,int key)
    {
        char c;
        int i,num = 0;

        StringBuffer sb = new StringBuffer(pt);

        int len = sb.length();
        for(i = 0; i <= (len-1); i++)
        {
            num = pt.charAt(i) - 65;
            num = num + key;
            c = (char)(num+65);
            sb.setCharAt(i,c);
        }
        return new String(sb);
    }

    public static String decrypt(String ct,int key)
    {
        char c;
        int i,num = 0;

        StringBuffer sb = new StringBuffer(ct);

        int len = sb.length();
        for(i=0;i <= (len-1);i++)
        {
            num = ct.charAt(i) + 65;
            num = num - key;
            c = (char)(num-65);
            sb.setCharAt(i,c);
        }
        return new String(sb);
    }
}
Output