[英]How to Encrypt a Message With an Encryption Key
我正在嘗試編寫一個小的加密程序。 選項之一是“使用密鑰加密”。 我不知道的事情是如何創建一種基於字符串的消息加密方法。
public static void encodeWithKey(){
System.out.println("What is the encryption key?");
String encryptKey = scan.nextLine();
System.out.println("What is the message to encrypt?");
String messageWithKey = scan.nextLine();
StringBuilder encryptWithKeyBuilder = new StringBuilder();
for (char c : messageWithKey.toCharArray()) {
// Add 1 to each character and append it
encryptWithKeyBuilder.append((char) (c - 1));
}
// Now the builder contains the String with the shifted values
System.out.println("Your encoded message is: ");
System.out.print(encryptWithKeyBuilder);
}
我想找到其加密密鑰的前兩個字母,將它們轉換為整數,然后將它們加在一起。 然后使用它來加密消息(在for循環中,將新數字替換為“ 1”。)
您可以按如下所示獲得加密密鑰的首字母總和:
int firstLettersSum = (int)encryptKey.charAt(0) + (int)encryptKey.charAt(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.