逆向工程实验——lab2(密码学)

1.lab2的内容为上次课堂练习的内容,把做过的题目写到实验报告里,要解释步骤,可以额外再去课堂练习的网站找一部分题目做。

第一题、01-Number-sequence

这题刚开始试过分解因数,发现没结果。上题做出来后,受到上一题后来那一串质数的启发,发现这几个数和那一串数都分别差1。这才发现是将质数减一,40=41-1,而41后面的第一个质数为43,所以下一个数为42。

第二题.02-Number Sequence - Part 2

很容易发现给出的数字都是5的倍数,将它们除以5后得到2,3,5,7,11,13,17,19,23,发现它们都是质数,并且是按顺序排列的,故后面两个数应该是295=145,315=155.

第三题:NUMBER SEQUENCE – PART 3

刚开始看到这个数变大再变小,第一反应是二次函数,结果发现不是。然后看到前两个数是4,16,以为是前一个数的平方,但发现后面不是256,而是37,而且这样也不可能变小。于是猜测是不是每位数字的平方和,发现16=44,37=66+11,58=77+33,所以接下来应该是55+88=89,88+99=145,而且验算发现11+44+55=42,4*4+2+2=20,更确定了这个规律,所以这两个数应该是89,145。

第四题:NUMBER SEQUENCE – PART 4

第一眼看到前几个数就觉得有些特殊,尤其是101,101本身是个质数,没有什么好多想的,但它等于100+1,然后试着将其余几个数都减去1得到0,1,9,?,100,225,?,784,?,2025,?
发现已知的数都是一些平方数,分别是0,1,3,?,10,15,?,28,?,45,?的平方,再找这些数之间的关系,发现1=0+1,3=1+2,15=10+5,而第一个问号处的数与相邻的两个数之间应该也有某种联系,故猜测是每次多加一,所以第一个问号应该是3+3=6,而10正好等于6+4,满足上述条件,接着往后验证,第二个问号应该是15+6=21,而21+7=28也满足我们的推测。到这儿已经基本能够肯定我们的推测是对的了,但还有一个已知的数,我们接着验证,28+8=36,36+9=45,也是满足我们的推测,所以下一个数就能肯定是45+10=55。然后根据规则将这些数平方加一后得到问号处的几个数依次应该是66+1=37、2121+1=442、3636+1=1297、5555+1=3026。

第五题:
Ciphertext:
JQRRG, JQRRG, TGKVGT JQRRG, JQRRG, TGKVGT, YGPP GT HCGNNV, FCPP UEJTGKV GT. HCGNNV GT KP FGP ITCDGP, HTGUUGP KJP FKG TCDGP, HCGNNV GT KP FGP UWORH, FCPP OCEJV FGT TGKVGT RNWORU! JWORVA FWORVA JWORVA FWORVA UCV QP C YCNN JWORVA FWORVA JCF C ITGCV HCNN CNN VJG MKPI’U JQTUGU CPF CNN VJG MKPI’U OGP EQWNFP’V RWV JWORVA VQIGVJGT CICKP.
题目是原始凯撒密码解密:
源代码:
Affine.java

/**
 * @Auther: 
 * @Date: 2018/9/27 18:58
 * @Description:
 */
class Affine {
    
    
        String deciphering(String s, int a, int b){
    
    // 解密的实现
            char[] ch = s.toCharArray();
            int length = ch.length;// 密文长度
            int[] in = new int[length];
            for (int i = 0; i < ch.length; i++) {
    
    
                if(ch[i] == ' '|| ch[i] == '.' || ch[i] == ','||ch[i] == '!'||ch[i] == '\''){
    
    
                }
                else {
    
    
                    in[i] = ch[i] - 97;// 利用ascii变成0-25数字
                    in[i] = ((in[i] - b) * a) % 62;// 解密算法
                    if (in[i] < 0) {
    
    
                        in[i] += 62;
                    }
                    ch[i] = (char) (in[i] + 97);// 将数字变成字母
                }
            }
            return String.valueOf(ch);// 将字符串数字变成String类型的字符串,返回
        }
}

Test.java

import First.Arithmetic;
import java.util.Scanner;

/**
 * @Auther: 
 * @Date: 2018/9/27 19:36
 * @Description:
 */
public class Test {
    
    
    public static void main(String[] args) {
    
    
        Arithmetic arithmetic = new Arithmetic();
        final int MOD = 26;
        int [] gcd = new int[12];
        int m = 0;
        String out = null;
        for(int i=1;i<MOD;i++){
    
    
            //求与26互素的数
            if((arithmetic.euclid(i,MOD)) == 1) {
    
    
                //求这些数mod26的逆,并把它加入gcd数组
                gcd[m] = (arithmetic.euclid_2(i,MOD)+26)%26;
                m++;
            }
        }
        Scanner input = new Scanner(System.in);
        System.out.println("请输入需要解密的密文:");
        // 输入密文
        String s = input.nextLine();
        Affine affine = new Affine();
        int k =1;
        for(int i=0;i<12;i++) {
    
    
            for (int j = 0; j < 26; j++) {
    
    
                out = affine.deciphering(s.toLowerCase(), gcd[i], j);
                //System.out.println("第"+k+"条明文为:"+out);
                System.out.println("第"+k+"条明文为:"+out.toUpperCase());
                k++;
            }
        }
    }
}

实验结果截图:

解密后的文本:HOPPE, HOPPE, REITER HOPPE, HOPPE, REITER, WENN ER FAELLT, DANN SCHREIT ER. FAELLT ER IN DEN GRABEN, FRESSEN IHN DIE RABEN, FAELLT ER IN DEN SUMPF, DANN MACHT DER REITER PLUMPS! HUMPT DUMPT HUMPT DUMPT SAT ON A WALL HUMPT DUMPT HAD A GREAT FALL ALL THE KING’S HORSES AND ALL THE KING’S MEN COULDN’T PUT HUMPT TOGETHER AGAIN.
这是两首德国的儿歌,题目的输入提示是:The solution to this challenge is the last word of the German part of the plaintext and must be typed in with capital letters.正确答案是:PLUMPS。

第六题:17-templer-03-en
首先将这个题翻译过来以后得到如下信息: 首先,所有空格都从明文中删除。 然后,将消息分成与密钥长度相同的块。 在这种情况下,块长度为七个字符。 最后,每个块都单独加密。 例: 明文是邮政客户。 在它被分成七个字符的块之后,它产生postalc ustomer。 在此示例中,键为7531426.这意味着第一个字母移动到第七个位置,第二个字母移动到第f个位置,依此类推。 因此,之后的消息是tlsaocp oetmsru。 所以密文是tlsaocpoetmsru。

所以我们采用暴力破解的方法,将7的阶乘次方种可能列出来,然后根据前七个单词进行大致猜测即可。
密文如下:
ydonoaT4ethh1tpStefeo1e30brmKuinor7saighsgndareanwsaretr-n-aatrsityangdaalunopelaTmltwhnsirexptecoonuaoYibteceorrtedpuaet13nhoOocthft1e30broaBck(l7aiy)rdFodnvncadtasceitriceehrobeyusinngogllniqadssdseiauncfisoteadTctsyraleohbaerhmcesntasheadlelsrtstteealdmalotiransiovffietweitcsichhatehoeopgtretmonhennotadnrfeteobrmnieohtohegfnOtct3h11e30brotnosad7tilyrctolwtolfsitrenhotnsciuaninotctthedoeIrfyodwifltuaoyrouamoveesslreewihrtabnulelcredpendtwaneeaorrefv-t-assektecdanfeyoroalsvereufnaiadstoountleestdrneaoumtileorwlrznedaigclefoipTchereoaplsoePlinowlokayobctlhipPiuaVsdIhpoepevleoiwnhsdtregtasiellhwyhtePutpnedepuosrsuperwHileerehattrlstplnoeaFnctriucrcsheinnsadhtgeaiateossrcpetgrnhosnofudotriceehhneeitstnhaetvPhoptetlastfieciondsoenhiiutpuorspsyooufrtnloteyRethenhooothpfl-spur-erofayutnhthtiiawleonesnsaterymoudandobeLrrytouihw

从密文的前七个字母中可以猜测T为首字母,然后大致猜出第一个字母应该是Today,最后在所有的爆破得到的明文中得到如下代码,并且将结果存入到txt文件中去。
暴力破解的JAVA代码如下:
Post.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Post{
    
    
    /** @list存储全排列的所有可能性
     */
    public static List<String> list;
    public static void permutation(int[] array, int index) {
    
    
        if (index > array.length) {
    
    
            return;
        }
        if (index == array.length) {
    
    
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < array.length; i++) {
    
    
                b.append("" + array[i]);
            }
            list.add(b.toString());
        }
        for (int i = index; i < array.length; i++) {
    
    
            swap(array, index, i);
            permutation(array, index + 1);
            swap(array, index, i); // 再次交换,保持原状
        }
    }
    private static void swap(int[] array, int index, int target) {
    
    
        int tmp = array[index];
        array[index] = array[target];
        array[target] = tmp;
    }
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
    
    
            int n = scan.nextInt();
            int array[] = new int[n];
            for (int i = 0; i < array.length; i++) {
    
    
                array[i] = scan.nextInt();
            }
            list = new ArrayList<String>();
            permutation(array, 0);
            Collections.sort(list);
            String str1="ydonoaT4ethh1tpStefeo1e30brmKuinor7saighsgndareanwsaretr-n-aatrsityangdaalunopelaTmltwhnsirexptecoonuaoYibteceorrtedpuaet13nhoOocthft1e30broaBck(l7aiy)rdFodnvncadtasceitriceehrobeyusinngogllniqadssdseiauncfisoteadTctsyraleohbaerhmcesntasheadlelsrtstteealdmalotiransiovffietweitcsichhatehoeopgtretmonhennotadnrfeteobrmnieohtohegfnOtct3h11e30brotnosad7tilyrctolwtolfsitrenhotnsciuaninotctthedoeIrfyodwifltuaoyrouamoveesslreewihrtabnulelcredpendtwaneeaorrefv-t-assektecdanfeyoroalsvereufnaiadstoountleestdrneaoumtileorwlrznedaigclefoipTchereoaplsoePlinowlokayobctlhipPiuaVsdIhpoepevleoiwnhsdtregtasiellhwyhtePutpnedepuosrsuperwHileerehattrlstplnoeaFnctriucrcsheinnsadhtgeaiateossrcpetgrnhosnofudotriceehhneeitstnhaetvPhoptetlastfieciondsoenhiiutpuorspsyooufrtnloteyRethenhooothpfl-spur-erofayutnhthtiiawleonesnsaterymoudandobeLrrytouihw";
            System.out.println(str1.length());
            for (String str : list) {
    
    
                int order[]=new int [7];
                for(int i1=0;i1<7;i1++)
                {
    
    
                    order[i1]=str.charAt(i1)-49;
                }
                StringBuilder str3=new StringBuilder();
                int len=0;
                for(int i1=0;i1<str1.length()-7;i1=i1+7)
                {
    
    
                    String str2=str1.substring(i1, i1+7);
                    for(int j=0;j<7;j++)
                    {
    
    
                        str3.append(str2.charAt(order[j]));
                    }
                    len=i1;
                }
                FileWriter fw = null;
                try {
    
    
                    //如果文件存在,则追加内容;如果文件不存在,则创建文件
                    File f=new File("D:/123/a.txt");
                    fw = new FileWriter(f, true);
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
                PrintWriter pw = new PrintWriter(fw);
                pw.println(str3);
                pw.println(str);
                pw.flush();
                try {
    
    
                    fw.flush();
                    pw.close();
                    fw.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
                System.out.println(str3);
                System.out.println(str);
            }
        }
    }
}

按照第一个字母为T开头,单词Today则找到如下所示的明文:
在这里插入图片描述

得到明文如下:

Todayonthe14thofSeptember1307ourKinghassignedanarrestwarrant–againstyouandallTemplarswithnoexceptionYouaretobecapturedonthe13thofOctober1307(BlackFriday)andconvictedashereticsyourbelongingsandliquidassetsconfiscatedTheroyalchamberhassentsealedletterstoalladministrativeofficeswiththechargetoopenthemonandnotbeforethemorningofthe13thOctober1307andtostrictlyfollowtheinstructionscontainedtothewordIfyoufailtoarmyourselvestherewillbeanunprecedentedwaveofarrests–andtakecareofyourselvesandfailnottounderestimateourwellorganizedpoliceforceThePopealsowillnotbackyouPhilippIVhasdevelopedhisownstrategyhewillputthePopeunderpressureHewillthreatentosplitFranceschurchandinstigateaprocessonthegroundsofhereticsintheeventthatthePopefailstodiscontinuehissupportforyouRelynotonthehelpofothers–putyourfaithintheownalertnessandmayourLordbewithyou

题目的问题是:In what should the knights put all their faith?
To solve the challenge, please enter this noun. Use only capital
letters.最终得到题解的答案是 ALERTNESS 。

猜你喜欢

转载自blog.csdn.net/Onlyone_1314/article/details/109318674