将html前端连接到java后端

k97glaaz  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(304)

关闭。这个问题需要详细或明确。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,添加细节并澄清问题。

五天前关门。
改进这个问题
我正在做关于caesar cipher encryption platform的最后一年项目,所以我制作了一个html前端,并选择java作为后端,不幸的是,我是这方面的新手。问题是如何将我的前端连接到我已经用java编写的程序。提前谢谢。html代码:

<div class="encryptionbox">
    <h2>Caesar Cipher Tool</h2>
    <form action="Caesarcipher" method="post">
        <input type="text" name="input" placeholder="Type Message Here">
        <br><br>
        <p>Set Key</p>
        <input type="text" name="key" placeholder="">                
        <p>Result</p>
        <input type="text" name="result" placeholder="">
        <select id="language">
        <option value="lang">Select Language</option>
        <option value="english">English</option>
        <option value="german">Germany</option>
        <option value="indonesia">Indonesia</option>
        <option value="malay">Melayu</option> 
        <input type="button" name="button1" value="Encode">
        <input type="button" name ="button2" value="Decode">
        <input type="button" name="button3" value="Brute Force">
        <button type="button" onclick="">Guide</button>
    </form>
</div>

这是我用java编写的程序:

public static String readString(String a){
    int character;
    String str = "";
    boolean done = false;
    byte [] b = a.getBytes();

    while(!done){
        try{
            character =System.in.read(b);
            if (character <0 || (char) character == '\n')
                done=true;
            else if ((char) character != '\r')
                str = str + (char) character;
            }
        catch(java.io.IOException e) {
            System.err.println("There is an error");
            done = true;
        }
    }
  return str;
}

private static String encrypting(String st, int rtint){
    int pjgstr=st.length();
    char [] ciphertext = new char [pjgstr];

    int[] ascist;
    ascist = new int [pjgstr];

    int[] cipherasci1;
    cipherasci1 = new int [pjgstr];
    int[] cipherasci2;
    cipherasci2 = new int [pjgstr];
    int wrong = 0;

    if (rtint >= 26)
        rtint = rtint%25;

    for (int a=0; a < pjgstr; a++) {
    ascist[a] = String.valueOf(st).codePointAt(a);

    if (ascist[a] >= 97 && ascist[a] <= 122 ){
        cipherasci1 [a] = ascist[a]+rtint;
        if (cipherasci1 [a] >= 123)
            cipherasci2 [a] = 97 + (cipherasci1 [a] % 123);
        else
            cipherasci2 [a] = cipherasci1 [a];   
    }
    else if (ascist[a] >= 65 && ascist[a] <= 90 ) {
        cipherasci1 [a] = ascist[a]+rtint;
        if (cipherasci1 [a] >= 91)
            cipherasci2 [a] = 65 + (cipherasci1 [a] % 91);
        else
            cipherasci2 [a] = cipherasci1 [a];
    }
    else if (ascist[a] == 32 ) {
        cipherasci2 [a] = 32;
    }

    else {
        wrong = 1;
    }

    ciphertext [a] = (char) cipherasci2 [a];
        }

    String Ct = String.valueOf(ciphertext);

    if (wrong == 1)
        System.out.println("Insert words and space only"); 
    return Ct;
}

private static String decrypting(String st, int rtint){
    int pjgstr=st.length();
    char [] ciphertext = new char [pjgstr];

    int[] ascist;
    ascist = new int [pjgstr];

    int[] cipherasci1;
    cipherasci1 = new int [pjgstr];
    int[] cipherasci2;
    cipherasci2 = new int [pjgstr];
    int wrong = 0;

    if (rtint >= 26)
        rtint = rtint%25;

    for (int a=0; a < pjgstr; a++) {
    ascist[a] = String.valueOf(st).codePointAt(a);

    if (ascist[a] >= 97 && ascist[a] <= 122 ){

        cipherasci1 [a] = ascist[a]-rtint;
        if (cipherasci1 [a] <= 96)
            cipherasci2 [a] = -96 + 122 + (cipherasci1 [a] % 97);
        else
            cipherasci2 [a] = cipherasci1 [a]; 

    }

    else if (ascist[a] >= 65 && ascist[a] <= 90 ) {
        cipherasci1 [a] = ascist[a]-rtint;
        if (cipherasci1 [a] <= 64)
            cipherasci2 [a] = -64 + 90 + (cipherasci1 [a] % 65);
        else
            cipherasci2 [a] = cipherasci1 [a];
    }
    else if (ascist[a] == 32 ) {
        cipherasci2 [a] = 32;
    }

    else {
        wrong = 1;
    }

    ciphertext [a] = (char) cipherasci2 [a];
        }

    if (wrong == 1)
        System.out.println("Insert words and space only");

    String pt = String.valueOf(ciphertext);
    return pt;

}

private static Language detect (String st) throws Exception{
    Translate.setKey(ApiKeys.YANDEX_API_KEY);
     Language detectText2 = Detect.execute(st);
     return detectText2;
}
private static Boolean detect2 (String st, Language lang) throws Exception{
      Translate.setKey(ApiKeys.YANDEX_API_KEY);

      Language lang2 = Language.ENGLISH;
      Language lang1 = Language.ENGLISH;
      String translatedText;
      if (lang != lang2) {
          translatedText = Translate.execute(st, lang, lang2);
      }
      else {
          translatedText = Translate.execute(st, lang, lang1);
      }
      Language detectText = Detect.execute(translatedText);
      Boolean status = detectText==lang2;
      return status;
}

private static String [] BruteForce (String st){
    String plainAr [] = new String [26];
    for (int i=0; i < 26; i++){
        String plain = decrypting (st, i);
        plainAr [i]=plain;
    }
    return plainAr;
}

public void encode(String a, String b) {   
    String st = readString(a);
                System.out.print("Key: ");
                String rt = readString(b);
                int rtint = Integer.parseInt(rt);
                String cipher = encrypting (st, rtint);
                System.out.print("Ciphertext: ");
                System.out.println( cipher);

}

public void decode(String a, String b) {
                String st2 = readString(a);
                System.out.print("Key: ");
                String rt2 = readString(b);
                int rtint2 = Integer.parseInt(rt2);
                String plain = decrypting (st2, rtint2);
                System.out.print("Plaintext: ");
                System.out.println( plain );
}

public void bruteforce(String a, String b) throws Exception {
    System.out.print("Insert Message: ");
                String st3 = readString(a);
                System.out.print("Key: ");
                String rt3 = readString(b);
                int rtint3 = Integer.parseInt(rt3);
            String [] plainAr = BruteForce (st3);

                   for (int i =0; i <26; i++){
                    Language Lang =  detect (plainAr[i]);
                   if (Lang!=null) {
                   Boolean status2 =  detect2 (plainAr[i],Lang);
                   if (status2 == true) {
                   System.out.print("Brute Force attack " + i + " : ");
                   System.out.println( plainAr[i]);
                                }
                        }
                   }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题