首页
社区
课程
招聘
未解决 [讨论][求助 - 已自行解决] 关于使用XOR加密的密文下无密钥的解密思路
发表于: 2019-10-3 12:11 3671

未解决 [讨论][求助 - 已自行解决] 关于使用XOR加密的密文下无密钥的解密思路

2019-10-3 12:11
3671
学校的课题。


课题的第一部分是解密加密算法为如下的加密字符

import base64

def encode(text):
    return base64.encodestring(text)

def decode(text):
    return base64.decodestring(text)

def encrypt(cleartext, key):
    to_return = bytearray(len(cleartext))
    for i in xrange(len(cleartext)):
        to_return[i] = ord(cleartext[i]) ^ key
    return to_return


这部分我直接暴力破解了。因为key的要求类型是一个byte ,并且key只有一个,也就是说取值范围肯定是 -128 至 127 不会变化的。


但是第二部分有些坑,我到现在也没想到该从哪儿切入解密


第二部分的代码如下

import base64
def encode(text):
    return base64.encodestring(text)

def decode(text):
    return base64.decodestring(text)

def encrypt(cleartext, key):
    to_return = bytearray(len(cleartext))
    for i in xrange(len(cleartext)):
        to_return[i] = ord(cleartext[i]) ^ ord(key[i % len(key)])
    return str(to_return)


主要问题是这次的key是一个String类型的内容,并且String的长度是未知的。

目前通过一些测试得知加密用的String的长度大概为5-7,但是感觉暴力破解不太可取。所以前来求助


顺便说一下,题目给的密文是base64加密过的,解密之后是乱码


(为了防止出现不必要的麻烦,我把密文编辑掉了,如果有兴趣的可以找我私聊要XD)


目前现在的想法是先过一遍base64的解码,然后使用重合因子(index of coincidence)法想办法去找规律。但是不太清楚该怎么具体操作好一些……

顺便这是给的解码算法的java代码:
import java.util.Base64;

public class Alan {

   public static String encode (byte[] text)
   {
	  return Base64.getEncoder().encodeToString(text);
   }

   public static byte[] decode (String text)
   {
	  return Base64.getDecoder().decode(text);
   }

   public static byte[] encrypt(byte[] cleartext, String key)
   {
	  byte[] toReturn = new byte[cleartext.length];
	  byte[] keyBytes = key.getBytes();
	  for (int i = 0; i < cleartext.length; i++)
	  {
		 toReturn[i] = (byte) (cleartext[i] ^ keyBytes[i % keyBytes.length]);
	  }
	  return toReturn;
   }

   public static void main(String[] args)
   {
	  System.out.println(encode(encrypt("this is a test".getBytes(), "12")));
	  System.out.println(new String((encrypt(decode("RVpYQRFbQhJQEkVXQkY="), "12"))));
   }
}

更新1:

从IC分析里,m=5的倍数的得数高于别的长度,所以目前猜测是这个密钥应该是个5的倍数。之后该怎么搞还是有点懵XD

更新2:

虽然不知道有没有用,目前最高的IC是来自单词“joint” (X


这里附上一个我求重合因子的代码(java)
public static double calculate(String s){

        int i;
        int N = 0;
        double sum = 0.0;
        double total = 0.0;


        //initialize array of values to count frequency of each letter
        int[] values = new int[256];
        for(i=0; i<256; i++){
            values[i] = 0;
        }

        //calculate frequency of each letter in s
        int ch;
        for(i=0; i<s.length(); i++){
            ch = s.charAt(i);
            if(ch < 256){
                values[ch]++;
                N++;
            }
        }

        //calculate the sum of each frequency
        for(i=0; i<256; i++){
            ch = values[i];
            sum = sum + (ch * (ch-1));
        }



        for (i = 0 ; i < values.length; i++){

        }

        //divide by N(N-1)
        total = sum/(N*(N-1));

        //System.out.println(Arrays.toString(values));
        //return the result
        return total;

    }

再更新!
现在只差临门一脚了!
目前使用key:“ (Epj”解码得到文字如下证实了解码的确是5位加密!

&ing:tNo, =t ta?es m1 eig<t.

A8an T!ringn Er.z. Ho#... ow d; youtmeantyou #ork 5long'ide 5 Ger9an?

elenn Wel8, ea7h oftus i:terc1pts 9essa3es f&om atspec=fic erma: rad=o to#er. o wethaveta co!nter$art ;n th1 oth1r si0e, w<o's  ip-t5ppin3 outtthe 9essa3es. very;ne t-pes 5 tou7h di2fere:tly,tso y;u ge  to ?now  he r<ythmtof y;ur c;unte&partz It'' str5ngel- int=matez I f1el a' if  kno# himtso w1ll. t's 5 pit- he <as atgirl2rien0, bu  tha 's w<y I 0isag&ee w=th y;u, M&. Al1xand1r, b1caus1 I'mtin l;ve w=th atcowo&ker ;f so&ts a:d wesve n1ver 1ven 9et.

ugh lexa:der:tWellx all;w metto b!y yo! ano her $int 5nd Isll t1ll y;u wh- yousre w&ong.^Hele:: Le 's.

ugh lexa:der:tExce8lentz

[Th1y ge  up 5nd h1ad t; thetbar.tAs t<ey o&der,tAlantsitstcomp8etel- sil1ntlyx in 5 daz1 - h5vingtcometto atreal=sati;n]

J;an C8arken In 7ase -ou w1re w;nder=ng,  hat'' wha  fli&tingtlook' lik1.

Al5n Tu&ing:t[lou0ly] elenu

Joa: Cla&ke: slig<tly 1mbar&asse0, asthis 'houtthas 0rawntatte:tiontto t<em] lan!^Hele:: Ye', Al5n?

A8an T!ringn [ge s uptand 2acesther]tWhy 0o yo! thi:k yo!r Ge&man 7ount1rpar  hasta gi&lfri1nd?

elenn It'' jus  a s upidtjokez Donst wo&ry a6out =t.

A8an T!ringn No,tno, :o, n;, nox tel8 me.^Hele:: We8l, e5ch o2 histmess5ges 6egin' wit< thetsametfivetlett1rs. -I-LyL-Y.tSo Itsusp1ct t<at C=lly 9ust 6e th1 nam1 of <is a9ore.^[By  his $ointx Joa:, Pe er H=ltontand ohn airn7rossthavetbegu: to 7atchton]

lan  urin3: Bu  tha 's i9poss=ble.tThe erma:s ar1 ins ruct1d totuse 2ive &ando9 let ers 5t th1 sta&t oftever- mes'age.^Hele:: We8l, t<is b8oke 0oesnst.

H!gh A8exan0er: ove #ill 9ake 5 mantdo s rang1 thi:gs,  sup$ose.^AlantTuri:g: I: thi' cas1....tLovetjusttlosttGerm5ny t<e wh;le b8oodytwar!^[He &ushe' offx bar3ing =nto ugh 5nd c5usin3 himtto s$ill  he p=nts <e ha' jus  bou3ht o: Hel1n. J;hn, etertand oan &ush ;ff a2ter <im a:d Hu3h, r1alis=ng w<at h5s ha$pene0, ap;logi'es a:d da'hes ;ff a2ter  hem]^HughtAlex5ndern My od, -ou d=d itz Youtjusttdefe5ted azis9 wit

之后我发现,大部分文字其实是可以找到词汇理解的,毕竟只是缺一两个字符。比如 gi&lfri1nd 肯定是 girlfriend, H!gh A8exan0er 这里的话有点难猜是什么,但是万能的谷歌告诉我是Hugh Alexander。

之后我把这些替换的字符和他应该是的字符进行XOR运算对比,得到了T这个字符为key的index=0的字符,所以我就把密钥 “ (Epj”改成了 “T(Epj”。输出结果也变成了:

Ring: No, It taKes mE eigHt.

ALan TUring Er.. HoW... how dO you mean you Work AlongSide A GerMan?

helen WelL, eaCh of us iNtercEpts MessaGes fRom a specIfic germaN radIo toWer. so we have a coUnterPart On thE othEr siDe, wHo's Tip-tAppinG out the MessaGes. everyOne tYpes A touCh diFfereNtly, so yOu geT to Know The rHythm of yOur cOunteRpart It'S strAngelY intImate I fEel aS if i knoW him so wEll. it's A pitY he Has a girlFrienD, buT thaT's wHy I DisagRee wIth yOu, MR. AlExandEr, bEcausE I'm in lOve wIth a cowoRker Of soRts aNd weve nEver Even Met.

hugh alexaNder: Well allOw me to bUy yoU anoTher Pint And Ill tEll yOu whY youre wRong.*HeleN: LeT's.

hugh alexaNder: ExceLlent

[ThEy geT up And hEad tO the bar. As tHey oRder, Alan sits compLetelY silEntly in A dazE - hAving come to a realIsatiOn]

JOan CLarke In Case You wEre wOnderIng, That'S whaT fliRting lookS likE.

AlAn TuRing: [louDly] helen

JoaN ClaRke: {sligHtly EmbarRasseD, as his Shout has Drawn atteNtion to tHem] alan!*HeleN: YeS, AlAn?

ALan TUring [geTs up and Faces her] Why Do yoU thiNk yoUr GeRman CountErparT has a giRlfriEnd?

helen It'S jusT a sTupid joke Dont woRry aBout It.

L-Y. So I suspEct tHat CIlly Must Be thE namE of His aMore.*[By This Point JoaN, PeTer HIlton and john cairnCross have beguN to Catch on]

alan turinG: BuT thaT's iMpossIble. The germaNs arE insTructEd to use Five RandoM letTers At thE staRt of everY mesSage.*HeleN: WeLl, tHis bLoke Doesnt.

HUgh ALexanDer: love Will Make A man do sTrangE thiNgs, i supPose.*Alan TuriNg: IN thiS casE.... Love just lost GermAny tHe whOle bLoody war!*[He RusheS off barGing Into hugh And cAusinG him to sPill The pInts He haS jusT bouGht oN HelEn. JOhn, peter and joan Rush Off aFter Him aNd HuGh, rEalisIng wHat hAs haPpeneD, apOlogiSes aNd daShes Off aFter Them]*Hugh AlexAnder My god, You dId it You just defeAted nazisM wit


之后发现,偶尔在某个5的倍数上,字符会有大小写不正确的原因。
另外已知XOR算法中如果让一个英文字母XOR 0x20,就会得到他的相对应的大写变成小写,小写变成大写的ASCII编码。所以我把最前面的T改成了t,之后解密用代码如下:
String resultFinal = new String((encrypt(decode(key), "t(Epj")));

之后,见证奇迹的时候到了!原文输出成功!

测试提交了一下作业,密钥正确!


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2019-10-5 09:11 被ScarletCN编辑 ,原因: 加密解密完成!!!
收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//