如何让我的Caesar密码加密其加密?

时间:2022-01-21 20:33:51

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

我正在尝试使用Caesar密码程序来加密其加密。我从这段代码开始:

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.

基本上我想采用编码消息并对其进行编码以获得第二级加密,并且总共执行5次。然后我想逐步解码消息,所以采用最新的编码版本并将其向后工作到第4个编码版本,然后到第3个等,直到消息被解码回其原始状态。我还想打印每个编码和解码版本的消息。

1 个解决方案

#1


2  

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

复合替换密码(Caeser或Vigenere)实际上并没有改善加密。它只是创建了一个不同的有效密钥。

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.

考虑一个Caeser密码(它只涉及向左或向右移动给定数量的字母)。如果使用1个字符移位的键,则A变为B,B变为C,依此类推。一个2字符移位的键有A成为C,B成为D等等。但是,如果你同时应用它们(无论什么顺序),那么你所做的就是做两倍的工作来创建一个有效的替换3个字符移位的关键... A变成D.如果你完成数学运算,你最终会看到Vigenere密码具有相同的属性。将有一种机制将所有密钥组合在一起并创建从密码文本到纯文本的单个替换,从而使您的额外工作毫无意义。

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.

问题是,试图攻击密码的人并不关心你最初是如何进行加密的。他们只关心找到产生明文的任何密钥。因此,他们的工作将不会更加困难,因为你将1和2的密钥组合在一起制作密钥3.他们只是在寻找最终的有效密钥。

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

有趣的是,旧的DES算法具有类似的属性。如果使用56位DES密钥K1对某些内容进行加密,然后使用另一个56 DES K2密钥再次对其进行加密,则实际上并没有将其设置为安全两倍,因为存在56位密钥(既不是K1也不是K2)将加密文本直接转换为纯文本,这是受攻击的所有需要​​查找的内容。这就是3DES实现实际使用加密和解密组合的原因。加密块是

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

正如亨利指出的那样,我关于DES中双重加密的陈述是在错误的假设下进行的

#1


2  

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

复合替换密码(Caeser或Vigenere)实际上并没有改善加密。它只是创建了一个不同的有效密钥。

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.

考虑一个Caeser密码(它只涉及向左或向右移动给定数量的字母)。如果使用1个字符移位的键,则A变为B,B变为C,依此类推。一个2字符移位的键有A成为C,B成为D等等。但是,如果你同时应用它们(无论什么顺序),那么你所做的就是做两倍的工作来创建一个有效的替换3个字符移位的关键... A变成D.如果你完成数学运算,你最终会看到Vigenere密码具有相同的属性。将有一种机制将所有密钥组合在一起并创建从密码文本到纯文本的单个替换,从而使您的额外工作毫无意义。

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.

问题是,试图攻击密码的人并不关心你最初是如何进行加密的。他们只关心找到产生明文的任何密钥。因此,他们的工作将不会更加困难,因为你将1和2的密钥组合在一起制作密钥3.他们只是在寻找最终的有效密钥。

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

有趣的是,旧的DES算法具有类似的属性。如果使用56位DES密钥K1对某些内容进行加密,然后使用另一个56 DES K2密钥再次对其进行加密,则实际上并没有将其设置为安全两倍,因为存在56位密钥(既不是K1也不是K2)将加密文本直接转换为纯文本,这是受攻击的所有需要​​查找的内容。这就是3DES实现实际使用加密和解密组合的原因。加密块是

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

正如亨利指出的那样,我关于DES中双重加密的陈述是在错误的假设下进行的