How do I run an encryption program multiple times

2019-09-07 00:45发布

Here is my code so far. I need to run the encode part of the code 5 times and then decode the encode the same number of times. I figured out how to encode the message but now I can't figure out how to run the "encode" or "decode" variable back through the code to strengthen the ecryption.

public class Codes
    {
        /**
         * Encode and decode a message using a key of values stored in
         * a queue.
         */
        public static void main(String[] args)
        { 
            int[] key = {7, 6, 5, 2, 8, 5, 8, 6, 4, 1};
            Integer keyValue;
            String encoded = "", decoded = "";
            String message = "Queues are useful for encoding messages.";
            Queue<Integer> encodingQueue = new LinkedList<Integer>();
            Queue<Integer> decodingQueue = new LinkedList<Integer>();

            // load key queues 
            for (int scan = 0; scan < key.length; scan++)
            {
                encodingQueue.add(key[scan]);
                decodingQueue.add(key[scan]);
            }

            // encode message
            for (int scan = 0; scan < message.length(); scan++)
            {
                keyValue = encodingQueue.remove();
                encoded += (char) (message.charAt(scan) + keyValue);
                encodingQueue.add(keyValue);
            }

            System.out.println ("Encoded Message:\n" + encoded + "\n");


            // decode message 
            for (int scan = 0; scan < encoded.length(); scan++)
            {
                keyValue = decodingQueue.remove();
                decoded += (char) (encoded.charAt(scan) - keyValue);
                decodingQueue.add(keyValue);
            }

            System.out.println ("Decoded Message:\n" + decoded);
        }


    }

as of right now I am receiving this output:

Encoded Message:
X{jwmx(gvf'{xgnzt&jpy&jpktlorh'sju{fokw/

Decoded Message:
Queues are useful for encoding messages.

In order to complete this program I need the output to look like this:

Encoded Message 1: X{jwmx(gvf'{xgnzt&jpy&jpktlorh'sju{fokw/ 
Encoded Message 2: _?oyu}0mzg.?}iv•|,nq?,orsytuvi.yow?kwq{0 
Encoded Message 3: f?t{}?8s~h5??k~??2rr?2tt{~|{zj5•ty?p•w•1 
Encoded Message 4: m?y}??@y?i<??m???8vs?8yv????~k<?y{?u?}?2 
Encoded Message 5: t?~•??H•?jC??o???>zt?>~x?????lC?~}?z???3 
Decoded Message 5: m?y}??@y?i<??m???8vs?8yv????~k<?y{?u?}?2 
Decoded Message 4: f?t{}?8s~h5??k~??2rr?2tt{~|{zj5•ty?p•w•1 
Decoded Message 3: _?oyu}0mzg.?}iv•|,nq?,orsytuvi.yow?kwq{0 
Decoded Message 2: X{jwmx(gvf'{xgnzt&jpy&jpktlorh'sju{fokw/ 
Decoded Message 1: Queues are useful for encoding messages.

I estimate that in order to make this happen I need to use a loop to run the "encode" and "decode" variables back through the program. However I cannot figure out how to make that happen.

1条回答
Emotional °昔
2楼-- · 2019-09-07 01:11

This will be easier if you use separate functions for the encode() and decode() operations:

class Codes {

  public static void main(String[] args) {
    ...
  }

  private static String encode(String plaintext, Queue<Integer> encodingQueue) {
    ...
  }

  private static String decode(String ciphertext, Queue<Integer> decodingQueue) {
    ...
  }

}

Does that help?

查看更多
登录 后发表回答