简体   繁体   中英

How to Encrypt a Message With an Encryption Key

I'm attempting to write a small encryption program. One of the options is "encrypt with key". The thing I can't figure out is how to create a way to encrypt a message based on a String.

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);

}

I want to find the first two letters of their encrypt key, convert them to integers, and add them together. Then use that to encrypt the message (in the for loop, replacing the '1' with the new number.)

您可以按如下所示获得加密密钥的首字母总和:

int firstLettersSum = (int)encryptKey.charAt(0) + (int)encryptKey.charAt(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM