Q:

Seemingly Random Characters at End of Output When Using Scanner

The code below is intended to encrypt and decrypt messages input. When I encrypt and decrypt data, it sometimes works and at other times doesn't. The following is an example of the problems I am experiencing.
Encryption

Decryption

As you can see, when I try to decrypt, my program terminated and some gibberish is output into the console. What is the issue with my code?
I'm unsure if mentioning this helps, but I have Eclipse's file encoding set to UTF-8.
Please excuse any poor code. I'm still very much a beginner with Java and I'm puzzled as to why this is happening.
import java.util.Scanner;

public class Transcrypt {

    static String mode = "",
                  msg,
                  key;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (!mode.equals("e") && !mode.equals("d")) { // Ask for mode until equal to "e" or "d"
            System.out.print("Encrypt or decrypt? (e/d) ");
            mode = input.nextLine().toLowerCase();
        }

        System.out.print("Message: "); // Ask for message
        msg = input.nextLine();

        System.out.print("Passkey: "); // Ask for key
        key = input.nextLine();

        input.close();

        System.out.println(transcrypt(msg, key, mode.equals("d"))); // Transcrypt and output
    }

    public static String transcrypt(String msg, String key, boolean decode) {
        String result = "";

        for (int i=0; i<msg.length(); i++) {
            // Add or subtract Unicode index of key.charAt(i % key.length()) and/from msg.msg.charAt(i) and convert back to character
            result += (char) ((int) msg.charAt(i) + ((int) key.charAt(i % key.length())) * (decode ? -1 : 1));
        }

        return result;
    }

}

A:

Your encoded message looks like "¼ÊßàãÊãæÑ×", but actually, it is "¼Êßàã\u0085ÊãæÑ×\u0095".
Most notably, it contains the control character 0x85 in-between which has “new line” semantics. So when copying that string, you’re copying the control characters with it and when pasting into the console upon your application’s query for the message, you are basically entering ¼Êßàã as message, the input committed via the “new line” control character, causing the subsequent query for the password to consume the trailing ÊãæÑ× characters.
The garbage you see right after the Passkey: output, is the result of your attempt to decode ¼Êßàã using the key ÊãæÑ×, as there was no newline entered at this point as the characters being already in the console’s buffer have been used.
Generally, as already said by Nándor Előd Fekete in this comment, you should not write characters to the console, that are actually binary data, like the encoded string.

By the way, you shouldn’t declare variables as static fields that are actually local to a method, i.e. your main method. Further, you don’t need to cast char to int when doing computations. char values are already a subset of the int values.

