lundi 28 novembre 2016

I have a java application and I would like to make it into an applet to use on a website

Basically, I want to take this program and I want to make it run on a website. Could someone tell me how to do this? Is there some code that I can add to it to be able to do this or...?

import java.util.Scanner;

public class Hashing4 {

    static Scanner stdIn = new Scanner(System.in);

    public static void main(String[] args) {

        int process = 0;

        do{

        System.out.println("Please select an action:");
        System.out.println("1: Encrypt");
        System.out.println("2: Decrypt");
        System.out.println("3: Hash");
        System.out.println("4: Exit");
        process = stdIn.nextInt();

        while (process!=1 && process!=2 && process!=3 && process!=4){   //While to make valid input
            System.out.println("Invalid input.");
            System.out.println("Please select an action:");
            System.out.println("1: Encrypt");
            System.out.println("2: Decrypt");
            System.out.println("3: Hash");
            System.out.println("4: Exit");
            process = stdIn.nextInt();
        }

        if (process == 1){
    //          System.out.println("This is where to put encrypt method call");
                System.out.println("Please enter a text to encrypt: ");
                String plaintext;
                stdIn.nextLine();       //Added extra nextLine cause scanner needs extra for input
                plaintext = stdIn.nextLine();
                System.out.println("Input encryption key value (number of letters to shift by):");
                int key = stdIn.nextInt();
                System.out.println(encrypt(key,plaintext));
        }
        else if (process ==2){
            System.out.println("Input text to decrypt:");
            String plaintext;
            stdIn.nextLine();       
            plaintext = stdIn.nextLine();
            System.out.println("Input encryption key value (number of letters to shift by):");
            int key = stdIn.nextInt();
            key = key%26;
            System.out.println(encrypt(26-key,plaintext));  //Decrypt 26-key
        }
        else if (process ==3){
            String decrypted = null;
            System.out.println("Please enter a text to hash: ");
            stdIn.nextLine();   
            decrypted = stdIn.nextLine();
            System.out.println(hash(decrypted));
        }
        System.out.println("");
        }while(process!=4);     //Loops back to do statement
    }


    public static String encrypt(int key,String plaintext) {    //int is variable of shift

        key = key%26;   //Makes encryption key stay within 0-25 as a container to keep input from going out of range
        char ctext[] = plaintext.toCharArray();

        for (int i = 0; i < plaintext.length(); i++){
            ctext[i] = plaintext.charAt(i);
            if(((ctext[i]>=65)&&(ctext[i]<=90))||((ctext[i]>=97)&&(ctext[i]<=122))){    //As long as ctext is within ascii letter ranges
                ctext[i] += key;
                if(((ctext[i]<65)||(ctext[i]>90))&&((ctext[i]<(97+key))||(ctext[i]>122))){  //97+key prevent spillover into other case by making the gap wider
                    ctext[i] -= 26;     //If encrypted letters go outside of uppercase or lowercase range, minuses 26 to bring it back into range
                }
            }
        }
        String encrypted = "";
        for (int i = 0; i < plaintext.length(); i++){
//          System.out.print(ctext[i]);
            encrypted += ctext[i];
        }
        return encrypted;
    }

    public static String hash(String hash) {
        int len = hash.length();
        if(len==0){
            return "\0";
        }
        len = len%16;
        if(len!=0){
            for(int i = 0;i<(16-len);i++){  //16-len is how many characters you need to add to get to set length
                hash += hash.charAt(0); //Takes hash and adds character at(0)to the end
            }
        }
        int hstuff[] = {0,0,0,0,0,0,0,0};
        for(int i = 0;i<hash.length();i++){
            hstuff[i%8] += hash.charAt(i);  //If string is bigger than array, will add value to each cell again
        }
        for(int i = 0;i<8;i++){
            hstuff[i] /= (hash.length()/8); //Takes values from hstuff(i) and divides by number of values added together to get average ascii values to use
        }
        String hout = "";       //Out value name
        for(int i = 0;i<8;i++){
            hout += (char)hstuff[i];    //converting hstuff into String
        }
        return hout;
    }
}




Aucun commentaire:

Enregistrer un commentaire