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:







No comments:

Post a Comment