我正在尝试编写一个小型加密程序。选项之一是“用密钥加密”。我无法弄清楚的是如何创建一种基于字符串加密消息的方法。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句