解密Java字符串

时间:2015-08-02 18:08:21

标签: java encryption

我试图在java中创建一个解密文本的程序。

示例:

输入:ABKBFA

输出:ABBA

规则:

  
      
  • 保留第一个字母和最后一个字母

  •   
  • 跳过字母表的一些字母依赖(A-Z),字母表的值为A = 1 B = 2 C = 3 ...所以如果单词是" HZBKRYAFEAAAAJ"它将首先保持字母H,并且因为H = 8,它将跳跃8步并落在E上。当它落在E上时,该字是HE,但它没有完成。 E = 5,所以跳5个步骤,它落在J上,这个词将是" HEJ"。

  •   

我还没有到目前为止知道如何解决这个问题。现在它只保存最后一个和第一个。

    public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       String text = sc.nextLine();
       char[] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
       char firstLetter = 0;
       char lastLetter = (char) (text.length()-1);

          System.out.println(text.charAt(firstLetter)+""+text.charAt(lastLetter));


}

5 个答案:

答案 0 :(得分:0)

只需编写原始函数即可:

public static String decrypt(String text) {
        String returned = "";
        //Read first letter
        char c = text.charAt(0);
        returned+=c;
        //Skip value
        int skip = 0;

        if(Character.isLowerCase(c))
            skip = c - (int)'a' + 1;
        else
            skip = c -(int)('A') + 1;

        if(text.length() >= skip+2)
            returned +=  decrypt(text.substring(skip));
        else
            //Return the last character
            returned += String.valueOf(text.charAt(text.length()-1));

        return returned;
    }

输出:

ABBA
HEJ

答案 1 :(得分:0)

假设这是你的作业(https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions),我不会发表完整的答案,但有些提示。

所以你一次连接一个字符

  1. 字符串开头的字符
  2. 前一个字符后面的字符N个位置(如果c是char,c - 'a'+ 1,'a'是1)。
  3. 重复#2直到到达终点位置(i> = s.length() - 1)
  4. 追加最后一个角色。

答案 2 :(得分:0)

你走了:

import java.util.*;

public class Decryptor{

    public HashMap<String,Integer> map = new HashMap<>();

    public static void main(String args[]){
        Decryptor d = new Decryptor();
        d.setAlphabet();
        d.decrypt();
    }

    public void setAlphabet(){      
        int i = 1;
        for(char alphabet = 'A'; alphabet <= 'Z';alphabet++) {
            map.put(alphabet+"",i);
            i++;
        }
    }

    public void decrypt(){
        String text = "HZBKRYAFEAAAAJ";
        String answer = "";
        int pos = 0;
        while(true){
            answer += text.charAt(pos);
            pos = map.get("" + text.charAt(pos));
            if(pos >= text.length){
                break;
            }
        }
        System.out.println(answer);
    }
}

答案 3 :(得分:0)

假设只输入大写字母作为数据:

1. Append the first letter to the result
2. The jump point is calculated by taking the (current letter) - 'A' + 1
   'A' - 'A' + 1 = 1
   'B' - 'A' + 1 = 2
   etc...
3. Check if the current position plus the jump is less than the location of the last letter
   If true, append the letter at the jump point to the result and move to that point
   If false, you're done
4. Repeat 2 & 3 until 3 results false
5. Append the last letter to the result

代码示例:

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();

    // Add the first letter
    StringBuilder sb = new StringBuilder();
    sb.append(input.charAt(0));

    for (int i = 0; i < input.length() - 2;) {
        // Find out how far to jump
        int jump = input.charAt(i) - 'A' + 1;

        // Check if the jump from the current position is before the last letter
        if (i + jump < input.length() - 1) {
            // Append the letter at the jump point
            sb.append(input.charAt(i + jump));

            // Move to the jump point
            i += jump;
        } else {
            // We're done
            break;
        }
    }

    // Add the last letter
    sb.append(input.charAt(input.length() - 1));

    // Display result
    System.out.println(sb);
}

结果:

ABKBFA => ABBA
HZBKRYAFEAAAAJ => HEJ
AZZZ => AZZ

答案 4 :(得分:0)

只是为了好玩:)

public class decrypt {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Error! Exiting...");
            System.out.println("Usage: java decrypt ENCRYPTED_WORD");
            System.out.println("       ENCRYPTED_WORD not found !!");
        } else {
            //Convert the Encrypted text to upper case, in case it was lower case
            StringBuilder sb = new StringBuilder(args[0]);
            for (int i = 0; i < sb.length(); i++) {
                char c = sb.charAt(i);
                if (Character.isLowerCase(c)) {
                    sb.setCharAt(i, Character.toUpperCase(c));
                }
            }
            boolean mError = false;
            int mCounter = 0;
            StringBuilder Rezult = new StringBuilder("");
            Rezult.append(sb.charAt(mCounter));
            while (mCounter < sb.length()-1) {
                try {
                    mCounter += sb.charAt(mCounter) - 'A' + 1;
                    Rezult.append(sb.charAt(mCounter));
                } catch (Exception e) {
                    mError = true;
                }
            }
            if (mError) {
                System.out.println("Your input is incorrect!! Cannot Decrypt");
            } else {
                System.out.println("Decrypted Code is: " + Rezult);
            }
        }
    }
}

编译为:

javac decrypt.java

运行方式:

java decrypt abkbfa

示例:

>>java decrypt
Error! Exiting...
Usage: java decrypt ENCRYPTED_WORD
       ENCRYPTED_WORD not found !!

>>java decrypt ams
Your input is incorrect!! Cannot Decrypt

>>java decrypt abkbfad
Decrypted Code is: ABBAD

>>java decrypt HZBKRYAFEAAAAJ
Decrypted Code is: HEJ