[英]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.