2016-10-01 58 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); 
相关问题