Ceasar Cipher w /模块化算术

时间:2017-02-23 02:04:29

标签: java encryption cryptography caesar-cipher

我在这里有一个加法密码,暴力测试固定密文的所有可能的排列。它的功能很好,但就我而言,我无法弄清楚如何实现模运算和解密公式。 p =((c - key)%26)其中p是明文值,c是字母数组中的密文(即A = 0,B = 1等)。

这是代码

public class Problem1 {

public static void main(String[] args) {

String guess = "";

String ct = "UOISCXEWLOBDOX"; // ciphertext
    int key;// key to test
    char ch;
    for (key = 0; key < 26; key++) { // for each key value
        for (int i = 0; i < ct.length(); ++i) { // test char
            ch = ct.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                ch = (char) (ch - key);
                if (ch < 'A') {
                    ch = (char) (ch + 'Z' - 'A' + 1);
                }
                guess += ch;
            } else {
                guess += ch;
            }
        }

        System.out.println("key: " + key + "     " + "Decrypted Message = " + guess);
        guess = "";
    }
}

}// main

这是模块化算法的更新代码

import java.util.*;
import java.io.*;
`public class AdditiveCipher {

public static void main(String[] args)
{

    String guess = "";
    //char pt = 'a';
    String cipherText = "UOISCXEWLOBDOX";
    int key,i,x = 0;

    //array of characters we can use 
    char[] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
            'P','Q','R','S','T','U','V','W','X','Y','Z'};

    //try each key on the cipher text
    for(key = 0; key < 26; key++)//for each key
    {
        for( i = 0; i < cipherText.length(); i++)//for each letter
        {
             int pt = (cipherText.charAt(i) - key - 'A') % 26;
             guess += (char)pt;
        }
        System.out.print(guess + "\n"); //display each guess
        guess = "";
    }//outer for
}//end main
}//end AdditiveCipher.java`

这是我尝试一段时间后输出大量盒子的最新尝试。任何有关逻辑的帮助都会受到赞赏,或者只是告诉我我做错了什么

2 个答案:

答案 0 :(得分:2)

此模数解密方法的实现相当简单,并且完全按照您在问题中描述的方式完成。您所要做的就是按照描述(A = 0, B = 1, ...)创建您的字符数组,并为每个字符执行转换p = ((c - key) % 26)以查找原始文本。至少,这就是它在理想世界中的运作方式。我愿意打赌你遇到的问题是字符的ASCII值。如您所见here,值'A'等于整数65,大写字母的值从那里开始增加。为了弥补这一点,在执行模数除法之前添加13(或'A',作为'A' % 26 = 13)(如:p = ((c - key + 13) % 26)),你应该好好去。为了清楚起见,p是字符数组中存储该字符的原始文本值的位置,而不是解密字符的实际整数值。我希望这有所帮助,祝你好好实施!

答案 1 :(得分:0)

解决方案:::

import java.util.*;
import java.io.*;
public class AdditiveCipher 
{
public static void main(String[] args)
{
    int key, x, i = 0;
    String guess = "";
    String cipherText = "UOISCXEWLOBDOX";

    //array of characters we can use 
    char[] alphabet =     {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
            'P','Q','R','S','T','U','V','W','X','Y','Z'};

//int w = ((cipherText.charAt(7) - 15 - 'A') % 26); // supposed to equal W
    //System.out.println(w);
    //int y = cipherText.charAt(0);

    //try each key on the cipher text   
    for(key = 0; key < 26; key++)
    {
        System.out.println("key is:" + key);
        for( i = 0; i < cipherText.length(); i++)
        {

就在这里......我必须在mod之前添加26以避免否定,以及追加字母[pt]的任何内容而不是施放pt

int pt = (cipherText.charAt(i) - key - 'A' + 26) % 26; 
                 guess += alphabet[pt];             

        }
System.out.print(guess + "\n"); //display each guess
        guess = "";
    }
}//end main
}//end AdditiveCipher.java