文字値を数値形式に変換し、「キー」を追加してから文字形式に変換し、単語を暗号化する基本的な暗号化プログラムを作成しようとしています。
ただし、数値を90以上(文字Z)にして、65(文字A)にループバックする方法がわかりません。
public class Encode {
/**
* Main method - this does not need to be changed at all.
*/
public static void main(String[] args) {
testing();
userInteraction();
}
/**
* This method is used to test the encrypt method.
*/
public static void testing() {
String testMessage1 = "test";
int testKey1 = 11;
String result = encrypt(testMessage1, testKey1);
System.out.println("Encrypted result: "+result);
}
/**
* This method changes each character in a String to a
* different character based on the key passed in as
* an integer. The new String created by the encryption
* process is returned.
*
* @param message the String to be encoded
* @param key the integer value used for encryption
* @return a new encoded String
*/
public static String encrypt(String message, int key) {
System.out.println("encoding: "+message+", with key: "+key);
String encodedMessage = "";
message=message.toUpperCase();
int length = message.length();
for(int i=0;i<length;i++)
{
int characterValue = (int)message.charAt(i);
characterValue = characterValue + key;
if(characterValue>90)
{
characterValue = (65 + key ); // <---- What needs to go here? In order to make value loop back to A.
}
char myChar = (char)characterValue;
encodedMessage=encodedMessage+myChar;
}
return encodedMessage;
}