学习课程:斯坦福大学密码学公开课
课件和作业(附答案)链接:https://pan.baidu.com/s/1vogTE2Flpzi9DmoclLPGQw
提取码:1mrp
Week 1 - Problem Set
Q1
先压缩再加密。
1、密文要随机性好,如果后加密的话,会导致随机性不好。同时也会泄漏压缩算法的信息。
2、压缩后数据量变小,此时加密的话会比较快。
Q2
D、把两个相同的序列连接起来作为一个新序列,这样新的序列就是可预测的了,不安全。
Q3
因为新的序列是通过AND运算得到的,那么检查最后一位时,最后一位是0的概率是0.75。
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
因此优势是0.25。
Q4
读懂题意就行了。
Q5
1、明文和密码的长度相同。
2、给定明文和对应的明文,能解出唯一的密钥。
Q6
选的时候不要只考虑随机性好坏,如果只考虑随机性,很有可能会错选最后一个选项。。。
Q7
计算就行。
Q8
主要是理解题意。选的节点向下不能出现坏节点。
Q9
即树的高度。当一个叶节点坏掉时,从根节点往树节点方向,每一层都需要做一个二选一选择,树的高度是多少,就要进行多少次选择。
Q10
前两题做对了这题必不会做错。
Week 1 - Programming Assignment
首先把target ciphertext和每个ciphertext异或。
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
public class Main {
//给定的10个ciphertext以及target ciphertext
private final static String string_cipher_text01 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b1" +
"6349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba50";
private final static String string_cipher_text02 = "234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df" +
"44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb741";
private final static String string_cipher_text03 = "32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df" +
"44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de812";
private final static String string_cipher_text04 = "32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a0290" +
"56f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee41";
private final static String string_cipher_text05 = "3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c2" +
"13f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de812";
private final static String string_cipher_text06 = "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d8" +
"4aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d";
private final static String string_cipher_text07 = "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c9" +
"09ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af513";
private final static String string_cipher_text08 = "315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d9" +
"43ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e941";
private final static String string_cipher_text09 = "271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d5" +
"13e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f404";
private final static String string_cipher_text10 = "466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed395980" +
"05b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d";
private final static String target_text = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e90" +
"52ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";
public static void main(String[] args) throws UnsupportedEncodingException {
//将每一个ciphertext转换成为BigInteger
BigInteger[] bigIntegerText = {
new BigInteger(string_cipher_text01,16),
new BigInteger(string_cipher_text02,16),
new BigInteger(string_cipher_text03,16),
new BigInteger(string_cipher_text04,16),
new BigInteger(string_cipher_text05,16),
new BigInteger(string_cipher_text06,16),
new BigInteger(string_cipher_text07,16),
new BigInteger(string_cipher_text08,16),
new BigInteger(string_cipher_text09,16),
new BigInteger(string_cipher_text10,16)};
BigInteger bigIntgerTargetText = new BigInteger(target_text,16);
//将targettext与每个ciphertext异或
BigInteger[] bigIntegerXOR = {
bigIntgerTargetText.xor(bigIntegerText[0]),
bigIntgerTargetText.xor(bigIntegerText[1]),
bigIntgerTargetText.xor(bigIntegerText[2]),
bigIntgerTargetText.xor(bigIntegerText[3]),
bigIntgerTargetText.xor(bigIntegerText[4]),
bigIntgerTargetText.xor(bigIntegerText[5]),
bigIntgerTargetText.xor(bigIntegerText[6]),
bigIntgerTargetText.xor(bigIntegerText[7]),
bigIntgerTargetText.xor(bigIntegerText[8]),
bigIntgerTargetText.xor(bigIntegerText[9])};
//思路是每个字符逐个计算
for(int i=0;i<10;i++) {
String XORString=bigIntegerXOR[i].toString(16);
for(int j=0;j+2<=XORString.length();j=j+2) {
int tmp=Integer.parseInt(XORString.subSequence(j, j+2).toString(), 16);
//只输出可显示字符,不然输出会很乱
if(tmp>31&&tmp<127) {
System.out.print((char)tmp);
}
else {
System.out.print("?");
}
}
System.out.println();
}
}
}
把输出的结果排一下版,对齐后进行猜测。猜测思路主要来自于题目中的提示:
Hint: XOR the ciphertexts together, and consider what happens when a space is XORed with a character in [a-zA-Z].
总结一下就是:
空格异或小写字母=对应的大写字母;
空格异或大写字母=对应的小写字母。
如果该列字符多,很可能target ciphertext此处是空格。
如果该列问号多,那么优先选取该列中的大写字母,因为英文文本中间大多是小写字母。
于是有:
0?T1 ?1@Au@!u0p?$T????RT?E??!??du?D???p??????Q??????!4Q???0D?ptT`?`????`?U@a@4 ??5
???E?E????DM??????L?S_N=??NT???N?O?????E??E????E?IC????RAU??R???????N?O???????T?NNE
????E?H???S???U?SqE2???QU??N?O????R???P??????DE??V??NU??I??EAK??TM??EF?????????
??????T???S???D??_Dw??NAU??????N??????O?I????^I???E?O??????EG??????R?I????T???E
?q??p??Q?Ep??u1P?T!????q?????T??e$??`??Pq?????? uP????P?????????Q?Q????`??????????
R?E???TT??S????SI?\?4???T?????H??^T??????????R[I??V??E?S?E???T?E?A??R?R??A?O??C?
R?E???TT??S????SI?\?4???O?????Y[????E??A?I????[SN???Rg???R???N?E?OM????????EO???
0?T1 ?0??????e1?????4??!???A?d???????u!??T?1????@??? @??uQ??E@?`???uD?????pp??P ??
?HMP??????????ZAG?N??CP#?????????EAS?????M?C?????ET???IRN???L?H?????C????ET????????
t<?ES&????S?E<????D-?YT>???R?SA\W?W?S?????N??P???\T?E??RT??EA??EO?EYW????N?H?NRO???
th? se??et mes?ag? is? ???n us?n? a strepm cipher? never uqe ehe ?e? more than once
the secret message is? when using a stream cipher? never use the key more than once
The secret message is: when using a stream cipher, never use the key more than once
因此密文是:
The secret message is: when using a stream cipher, never use the key more than once
密文异或明文得出key为:
66396e89c9dbd8cc9874352acd6395102eafce78aa7fed28a07f6bc98d29c50b69b0339a19f8aa401a9c6d708f80c066c763fef0123148cdd8e802d05ba98777335daefcecd59c433a6b268b60bf4eF03C9A61
可以用该key解密其他的密文来验证。
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
public class Main {
//给定的10个ciphertext以及target ciphertext
private final static String string_cipher_text01 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b1" +
"6349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba50";
private final static String string_cipher_text02 = "234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df" +
"44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb741";
private final static String string_cipher_text03 = "32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df" +
"44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de812";
private final static String string_cipher_text04 = "32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a0290" +
"56f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee41";
private final static String string_cipher_text05 = "3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c2" +
"13f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de812";
private final static String string_cipher_text06 = "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d8" +
"4aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d";
private final static String string_cipher_text07 = "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c9" +
"09ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af513";
private final static String string_cipher_text08 = "315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d9" +
"43ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e941";
private final static String string_cipher_text09 = "271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d5" +
"13e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f404";
private final static String string_cipher_text10 = "466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed395980" +
"05b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d";
private final static String target_text = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e90" +
"52ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";
private final static String guest_key = "66396e89c9dbd8cc9874352acd6395102eafce78aa7fed28a07f6bc98d29c50b69b0" +
"339a19f8aa401a9c6d708f80c066c763fef0123148cdd8e802d05ba98777335daefcecd59c433a6b268b60bf4eF03C9A61";
public static void main(String[] args) throws UnsupportedEncodingException {
//将每一个ciphertext转换成为BigInteger
BigInteger[] bigIntegerText = {
new BigInteger(string_cipher_text01,16),
new BigInteger(string_cipher_text02,16),
new BigInteger(string_cipher_text03,16),
new BigInteger(string_cipher_text04,16),
new BigInteger(string_cipher_text05,16),
new BigInteger(string_cipher_text06,16),
new BigInteger(string_cipher_text07,16),
new BigInteger(string_cipher_text08,16),
new BigInteger(string_cipher_text09,16),
new BigInteger(string_cipher_text10,16)};
BigInteger bigIntgerTargetText = new BigInteger(target_text,16);
BigInteger bigIntegerGuestKey = new BigInteger(guest_key,16);
for(int i=0;i<10;i++) {
System.out.println(i+1+":\n" + new String(bigIntegerGuestKey.xor(bigIntegerText[i]).toByteArray(), "GBK"));
}
}
}
output:
1:
We can factor the number 15 with quantum computers. We can also factor the number 1
2:
Euler would probably enjoy that now his theorem becomes a corner stone of crypto -
3:
The nice thing about Keeyloq is now we cryptographers can drive a lot of fancy cars
4:
The ciphertext produced by a weak encryption algorithm looks as good as ciphertext
5:
You don’t want to buy a set of car keys from a guy who specializes in stealing cars
6:
There are two types of cryptography - that which will keep secrets safe from your l
7:
There are two types of cyptography: one that allows the Government to use brute for
8:
We can see the point where the chip is unhappy if a wrong bit is sent and consumes
9:
A (private-key) encryption scheme states 3 algorithms, namely a procedure for gene
10:
The Concise OxfordDictionary (2006) de铿乶es crypto as the art of writing o r sol
不知道第十个为什么会出来奇奇怪怪的东西 : (