我正在尝试创建维吉尼亚密码的加法部分,并且需要将字母表中的字母添加在一起,从而产生字母表中的另一个字母。这必须是标准字母表,不包含特殊字符。全部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;
}
最佳答案
我创建了一个带有字符数组的新方法,所有输入都是字母表。我用有问题的数字调用该方法并返回一个字符。
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];
}
关于java - 如何添加字母以产生字母表中的另一个字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803387/