2016-10-01 155 views
1

我想創建一個vigenere密碼的另外部分,並且需要在字母表中一起添加字母,導致字母表中的另一個字母。這必須是沒有特殊字符的標準字母表。全部26個字母。 我可以得到與字母數字關聯的數字。例如A = 0 B = 1 ... z = 25,那麼我如何能夠創建字符串充滿相當於該數字的字母?如何添加導致字母表中另一個字母的字母?

public String encrypt(String orig, String iv, String key) { 
    int i, j, result; 

    String cipherText = ""; 
    int b = iv.length(); 
    //loops through the entire set of chars 
    for (i = 0; i < text.length; i += b) { 
     //Splits the char into block the size of the IV block. 
     for (j = 0; j < b; j++) { 
      //checks to for first block. If so, begains with iv. 
      if (i == 0) { 
       //adding the iv to the block chars 
       char one = text[j], two = iv.charAt(j); 
       result = (((iv.charAt(j) - 'a') + (text[j] - 'a')) % 26); 
       //prints out test result. 
       System.out.println(one + " + " + (iv.charAt(j) - 'a') + "= " + result); 
      } else { 
       //block chainging, addition, with new key. 
       result = ((key.charAt(j) - 'a') + (text[j + i] - 'a')) % 26; 

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

    return cipherText; 
} 
+0

建議:添加更多標籤,你的問題,如加密或這樣,一個更好,如果你的標題清楚地表明,你在說密碼 – gia

回答

0

我用char數組創建了一個新的方法,所有的輸入都是字母表。我用有問題的號碼調用方法並返回一個字符。

public char lookup(int num){ 
    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'}; 

    return alphabet[num]; 
} 
0

由於'a''z'的數字代碼是連續與char類型(UTF-16),你可以簡單地使用除:

public char lookup(int num) { 
    return (char)('a' + num); 
} 

因爲char + int將導致int,我們需要將其類型轉換回char

0

字符可以自動轉換爲整數。 例如試試這個:

final char aChar = 'a'; 
    char bChar = aChar + 1; 
    char uChar = (char) (bChar + 19); 
    char qChar = (char) (uChar - 4); 

    System.out.println(aChar+" "+ bChar + " " + qChar + " " + uChar); 
相關問題