简体   繁体   中英

How do I get my Caesar cipher to compound its encryption?

I'm trying to get a Caesar cipher program to compound its encryption. I'm starting with this code:

import java.util.*;
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: " + encoded);
    }

    //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: " + decoded);


    }

  }

}

Basically I want to take the encoded message and encode it to get a second level of encryption, and do it a total of 5 times. Then I want to decode the message step by step, so take the latest encoded version and work it backwards to the 4th encoded version, then to the 3rd etc., until the message is decoded back into its original state. I also want to print each encoded and decoded version of the message.

Compounding a substitution cipher (Caeser or Vigenere) does not actually improve the encryption. It just creates a different effective key.

Consider a Caeser cipher (which just involves shifting the letters left or right by a given amount). If you use a key of a 1 character shift, then A becomes B, B become C and so on. A key of a 2 character shift has A become C, B become D, etc. However, if you apply them both (in whatever order), then all you've done is do twice as much work to create an effective substitution with a key of a 3 character shift... A becomes D. If you follow through the math you'll end up seeing that a Vigenere cipher has the same property. There will be a mechanism to take all your keys together and create a single substitution that goes from the crypt text to the plain text, rendering your extra work pointless.

The problem is that people who are trying to attack the cipher don't care how you originally did the encryption. They only care about finding any key that produces the plaintext. So they're work will not be more difficult because you combined a key of 1 and and key of 2 to make the key 3. They're just going to be looking for the final effective key.

Interestingly, the old DES algorithm has a similar property. If you encrypt something with a 56 bit DES key K1 and then encrypt it again with another 56 DES K2 key you haven't actually made it twice as secure, because there exists a 56 bit key (that is neither K1 nor K2) that will convert the encrypted text directly to the plain text, which is all an attacked needs to find. This is why a 3DES implementation actually uses a combination of encryption and decryption. the encrypted block is

ciphertext = Encrypt(K3, Decrypt(K2, Encrypt(K1, plaintext)))

Edit

As Henry points out, my statement about double encrypting in DES is working from a false assumption

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