![](/img/trans.png)
[英]How to replace a random letter in a triple with a random character from the alphabet, if the letters in the triple are repeated?
[英]How can i add letters that result in another letter in the alphabet?
我正在嘗試創建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;
}
我用char數組創建了一個新方法,所有輸入都是字母表。 我用有問題的數字調用了方法並返回了一個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];
}
由於'a'
到'z'
的數字代碼是連續的char
類型(UTF-16),你可以簡單地使用加法:
public char lookup(int num) {
return (char)('a' + num);
}
因為char + int
將導致int
,我們需要將類型轉換回char
。
字符可以自動轉換為整數。 例如試試這個:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.