Title: panaimin@icst.pku.edu.cn
1????????????(?)
- ???,??????????
- panaimin_at_icst.pku.edu.cn
- http//www.icst.pku.edu.cn/InfoSecCourse
2??
- ????
- ????
- RSA
- Elliptic curve
- Diffie-Hellman????
- ????
- ????
- ????
- ???Crypto
3????????
- 76?Diffie?Hellman??????????,???????????
- ?????????????????
- ??????????
- ?????????????????
- 78?,RSA??
- PKI
4????????
5????????
6???????
- ????????????????
- ????????????????
- ???????
- ????????????
- ???????,??????????
- ??????????????????
- ?????,????????????????
- ???????,???????????
- (??)???????????
7??????????
- ?????????,????????????
- ?????????,??????????,???????????
- ???????????????
- ??????????
8??????????
- ????????????
- ?????????????
- ????????
- ????????????????????
- ?????????????????????,????????????????????????
- ???????
- ????????????
- ?????????????
- ?????????????????????????
- ???,????????
9??(knapsack)??
- 0-1????
- ???????S???????A(a1,,an),??ai????,?????S
?aixi ??????X(x1,,xn)? - ????NP????,?????????????n?????
- ???????????
- ???????X,S???
- ?????????,????????????,??????
- ??????????????????
- ?????????????
- ???????????
10????????????
- ?????????
- ai gt ?aj (j 1,,i-1)
- ?????????????
- ??
- ????ai??,??S?????,???ai, ?xi?1,???xi?0
- ????,?????ai
- ??????2, 3, 6, 13, 27, 52
- ??70???
- ???2, 3, 13, 52
- ??,??70??????110101
11????
- ????????
- ???????????
- ??
- ?????? m gt ?ai (i 1,,n)
- ???????m?????w,?? ai wai (mod m) (i 1,,n)
- ???ai???????
- ??????????????
12????????????? MH????
- ??
- ????????n??X(x1,,xn)
- ?????A (a1, , an),???????S S E(X)
?aixi - ??
- ???S w-1S mod m
- ?????????
- S ?aixi
13?????????
- ??????????
- ????????
- ??????,?????????????,????????
14RSA??
- 1977??Ron Rivest?Adi Shamir?Len Adleman??,1978???
- ?????????
- ??????0n-1??,n??????
- ????????????
- ????????,???2000?9???
15RSA????
- ?????k, 2k lt n ? 2k1
- ?? C Me mod n
- ?? M Cd mod n Med mod n
- ?? KUe,n, ?? KRd,n
- ????????????
- ????e,d,n,??Med mod n M, ???Mltn
- ??Me?Cd????
- ?e?n??d?????????
16RSA??????
- ?npq, p?q????,?(n)(p-1)(q-1)?n?Euler?
- Euler????
- ?npq, p?q????, k?????,?
- mk(p-1)(q-1)1 ? m mod n, ???0?m?n
- ????e,d, ??edk?(n)1,? ed ? 1 mod ?(n) ? d ?
e-1 mod ?(n) - ?? KUe,n, ?? KRd,n
17????
- Fermat?? p??,a???????p??,? ap-1 ? 1 mod
p(???) - Euler??(n)?????n??n????????
- p???,?(p)p-1
- ?n??????n?Piai, aigt0,Pi????,? ?(n)
?Piai?(1-1/Pi) - ?gcd(m,n)1,??(mn)?(m)?(n),???,?p?q?????,
?(pq)(p-1)(q-1)
18????(?)
- Euler?? ?a?n???????,? a?(n) ? 1 mod n
- ?? ?npq, p?q????, k?????,? mk(p-1)(q-1)1 ? m
mod n, ???0?m?n
19RSA???????
- ?????
- ???????p,q, p?q
- ??npq,?(n)(p-1)(q-1)
- ????e,??gcd(e,?(n))1
- ??d ? e-1 mod ?(n)
- ?? KUe,n, ?? KRd,n
- ??
- ?? C Me mod n
- ?? M Cd mod n
20????
- ??dam, mbkbk-1b0(?????)
- d?1
- for i?k downto 0
- do d?(d?d) mod n
- if bi 1
- then d?(d?a) mod n
- return d
- ??
- M ((((((bk2bk-1)2bk-2)2bk-3)2)2b0
21RSA????
- ??????
- ??n pq????,??,?????????n??p?q,??????????p?q
- ???????
- ??e??d,???????
22?????
- ??????
- ?????????n(??????????)
- ?????a, ?altn
- ???????(???Miller-Rabin??),?n??????,??n,???
- ?????????????,??n???,?????.
- ???? ?N??,?ln(N)?????????
23????
- Miller-Rabin???????????????
- WITNESS(a,n)
- ? ?bkbk-1b0?(n-1)??????
- ? d?1
- ? for i?k downto 0
- ? do x?d
- ? d?(d?d) mod n
- ? if (d1 x?1 x?n-1) return TRUE
- ? if (bi1) then d?(d?a) mod n
- ? if (d?1) return TRUE
- ? else return FALSE
- ??n????,???FALSE???(?????)??0.5
- ???TRUE,??n?????
- ???an-1a mod n
- ??x21 mod n,??1?n-1??,?????
24??Euclid??
- ExtEclid(d,f)?????????, ??d?f
- ? (X1,X2,X3)?(1,0,f), (Y1,Y2,Y3)?(0,1,d)
- ? if Y3 0 then return X3, no inverse
- ? if Y3 1 then return 1, inverse is Y2
- ? Q ?(X3/Y3)?????
- ? (X1,X2,X3)?(X1-QY1, X2-QY2, X3-QY3)
- ? (X1,X2,X3) ? (Y1,Y2,Y3)
- ? goto ?
- fX1dX2X3, fY1dY2Y3
- ?Y31,? fY1dY21 ?dY2(-Y1)f1
- ? dY2 ? 1 mod f ?Y2 d-1 mod f
25RSA???
- ???????
- ?? KUe,n, ?? KRd,n, npq
- ??npq ? ?(n)(p-1)(q-1) ? de-1 mod ?(n)
- ???p,q,????(n) ? de-1 mod ?(n)
- ????(n),????d
- ??
- ????????????????
- ????????????????
- ????????????????
- ?? ??????n(1024???),????e,d???????,????
26RSA????
- ????
- ??????n??
- ????
- ??CPU???????????????????????
27??(primitive root)
- Euler????,????????a,n,
- a?(n) ? 1 mod n
- ?????????m??(n) (m?(n)),??
- am ? 1 mod n
- ????a,m?(n),??a?n?????
- ????p,?a?p?????,?
- a,a2, ,ap-1??p?????,?????p??0???,??1,2,,(p-1
)???p??.
28????
- ?a???p?????,??????b,b?0 mod p,???????i,
1?i?(p-1),?? b?ai mod pi??b?a???p???(????),??
inda,p(b).???? inda,p(xy) inda,p(x)inda,p(y)
mod ?(p) inda,p(xr) r?inda,p(x) mod ?(p) - ??????? y?gx mod p
- ??g,x,p,??y????
- ??y,g,p,??x????
29Diffie-Hellman????
- ???????????????????,?????????
- ??????????????????
- ??
- ??????q??q?????r
- A??Xltq,??XArXmod p, A?B XA
- B??Yltq,??YBrYmod p, B?A YB
- A?? (YB)X?(rY)X?rXYmod p
- B?? (XA)Y?(rX)Y?rXYmod p
- ??????????(rXYmod p)
- ??q??q???r???????????
30Diffie-Hellman???????
31Diffie-Hellman???????
- ?????
- 1 ??????q??q?????r(??E??)
- 2 A??Xltq,??XArX mod p, A?B XA
- 3 E??XA,?Z,??ZErZ mod p,??A?BZE
- 4 B??Yltq,??YBrY mod p, B?A YB
- 5 E??YB,??B?A ZE
- 6 A?? (ZE)X?(rZ)X?rZX mod p
- 7 B?? (ZE)Y?(rZ)Y?rYZ mod p
- 8 E?? (XA)Z?rXZ mod p, (YB)Z?rYZ mod p
- E?????rXY mod p
- E?????????????,??????
32????????
- 1985?Miller,Koblitz ????
- y2axybyx3cx2dxe
- ???????????O???
- ????
- ???????????,????O
- O???????O -O PO P
- ??????X???P1?P2,?P1P2OO,??P1 -P2
- ?????Q?R?X???,?????,?????P1,?QRP1O,?QR-P1
- 2?,???Q????,???????????????S,??QQ2Q-S
33?????????
34????????
- ????????
- y2?x3axb mod p
- p????,?4a327b2?0 mod p
- ?????0lt x ltp,???????y,???????(x,y),??x,y lt
p???Ep(a,b) - Ep(a,b)????O
- ????
- POP
- ??P(x,y),?P(x,-y)O,(x,-y)??P???,??-P???(x,-y)??
Ep(a,b)? - ??P(x1,y1),Q(x2,y2),? PQ(x3,y3)?x3?2-x1-x2
(mod p)y3?(x1-x3)-y1 (mod p)??,??P?Q,? ?
(y2-y1)/(x2-x1) ??PQ,? ? (3x12a)/(2y1)
35????????
- ??????
- ????QkP,??Q?P??Ep(a,b),kltp
- ??k?P,??Q,????
- ??Q?P,??k,????
- ??Ep(a,b)???G,??G??n??????
- G??????nGO???n?
- ??????r,??PrG,??
- ??(p,a,b,G,P),P???
- ??r
- ??M????M????Ep(a,b)????Pm
- ??,?????k,????CmkG,PmkP)
- ??k??kG??kP?O,??????k.
- ??Cm (PmkP)-r(kG)PmkrG-rkGPm
- ???????
36??????????
- ?? ?P?kP??k
- ???????????
- ???????????,???
- ?? ECC RSAPollard
rho????
512 3?104
768 2?108
1024 3?1011
1280 1?1014
1536 3?1016
2048 3?1020
Key size MIPS-Yrs
150 3.8?1010
205 7.1?1018
234 1.6?1028
37????
- ??????,??????????????
- ????
- ??????
- ??????
- ????????
- ??????????????,?????????????,?????????
- ???????????????,??????????????
38?????????
- Message encryption??????????????
- ???????????
- MAC??????,??????????????????????
- Hash function???????????????????????????,??????
39Message Authentication Code
- ??????????????????????????,???????,?MAC,??????(cry
ptographic checksum) - ??A???B,????K,????M, MACCK(M)
- ????????MAC????MAC??,?
- ?????????M????
- ??????????????????
- ??????????,????????????
- MAC?????????,???????????????????????????
40MAC????
M
M
C
C
Compare
Ck(M)
K
K
41??MAC??
- MAC???????
- ?????????????
- MAC??????
- MAC??????,??,???????????????
- M(X1,X2,,Xt)
- ?M??????MX1?X2??Xt
- MAC EK(?M)
- ?????M?(Y1,Y2,,Yt-1,Yt),??Yt??
- Yt Y1?Y2??Yt-1??M
- ???M?M??EK(?M)EK(?M?) ? CK(M)CK(M?)
- ??,????????K,????????M?
42MAC?????
- ??
- ?????MAC????????K
- ??
- ??M?CK(M),????M???CK(M)CK(M?)???????(??????)
- CK(M)????????M?M?, PrCK(M) CK(M?)2-MAC
- f?M?????(????????),??, PrCK(M) CK(f(M))2-MAC
43MAC based on DES
- ??
- M(X1,X2,,Xt)
- ?M1EK(X1)
- ?Mj1EK(Xj1??Mj), 1?jltt
- MAC ?Mt
- ANSI??(X9.17)
- ??CBC????,?????0
- ????????????
44Hash Function
- MAC???????????
- MAC???
- Hash?????????????
- Hash?? hH(x), ??
- ??????????????????
- H(x)??????
- ??? ??h,??x?hH(x)???????
- Weak Collision Resistence(WCR)??x,??y?x?H(x)H(y
)??????? - Strong Collision Resistence(SCR)
??y?x?H(x)H(y)???????
45????????
- ????
- ?k?1.18?2m/2?2m/2, ?k??1,2m?????????????????0.5
- ?k?0.83?n1/2,???1,n?k???????????????0.5
- ??,?Hash????N??Hash??,????(??kN/2)??????????Hash?
?????0.5
46??????
This Letter is I am writing
you to to you
Mr. --
to introduce
P. --
. . . . . .
Alfred
Barton,
. . . . . .
Letter2?Letter1?????????????2k???,???????????hash
?????!
??Hash????,64-gt128-gt160-gt256
47hash??????
- ?Merkle?1989???
- ?????hash????
- ????
- ?????M??????????Yi
- ????padding???????M???
- ?????CV0
- ????f, CVif(CVi-1,Yi-1)
- ????CVi?hash?
48hash?????
Y0
Y1
YL-1
b
b
b
n
n
n
f
n
f
n
f
CVL
IV CV0
CVL-1
CV1
IV initial value ??? CV chaining value
??? Yi ith input block (?i ??????) f
compression algorithm (????) n length of
hash code (??????) b length of input
block(??????)
49MD5 ??
- ??Ron Rivest
- ??
- ?????????
- ??128?????
- ???512?????????
50MD5 ???
51MD5??
- ???padding
- ???512???
- ??64????????64?
- ?????(64164512),???1000
- ???
- ??????512???Y0,Y1,YL-1
- ???
- ???MD buffer,128???(4??),??????,?L?
- ??????128?,?????512?,????128?,???????
- ???
- ?????????????128?
52MD5?????????
53????16?????????
A
B
C
D
Function g g(b,c,d) 1 F(b,c,d)
(b?c)?(b?d) 2 G(b,c,d) (b?d)?(c?d) 3 H(b,c,d)
b?c?d 4 I(b,c,d) c?(b?d)
g
Xk
Ti
CLSs
A
B
C
D
54??MD5
- MD5??little-endian
- ??????64???? ? 128?hash???
- MD5???????
- Dobbertin?1996?????????512-bit?,???MD5????????hash
- ????????????????,???MD5?hash??
55Secure Hash Algorithm??
- 1992?NIST???SHA(128?)
- 1993?SHA????
- 1994?????SHA-1(160?)
- 1995?SHA-1??????
- SHA-1????????lt264
- SHA-1??????160?
- ???MD4
56SHA-1??
- ???MD5??
- ???pading
- ?MD5??,???512???
- ???
- ??
- ???
- ???MD buffer,160???(5??)
- ????,160??512??-gt160??
- ???
- ??????SHA-1???
57????
58????20?????????
??????
??
59SHA-1????
- SHA-1??big-endian
- ?????? 160?hash?
- ?????????512-bit?,???SHA-1????????hash
- ????MD5
- ?????MD5
60RIPEMD-160??
- ??RIPE?????
- RIPEMD?128?
- ?????RIPEMD-160
- ???MD5
- ??
- ?????????
- ?????160??????
- ???512???????
61RIPEMD-160?????
62HMAC??
- MAC?????????
- MAC?????
- ?????????
- hash???????MAC HMAC?????
63HMAC???
64HMAC??????
- ???K???0?????hash?K
- K?????ipad(00110110)?XOR???Si
- ?(SiM)??hash
- K?????opad(01011010)?XOR???S0
- HMACfIV,S0f(IV,SiM)
- HMAC??
- ???????hash??
- ????????????????hash??
- ????hash?????
- ???????
- ?hash?????????
65????
- ?????????
- ???????????????
- ????????????
- ???????
- ?????
- ?????????????,????
- ????????
- ????????????
- ???????
- ?????
66??????
- ????M?????H(M)
- ????????????H(M)????,????EKRa(H(M))
- ????M??,????
- B?????????,???????
- B?A??????????H
- B??????????H(M)
- ??HH(M),??????A???
- ??
- ?????,??????????
- ??????,?????
67????????
68???????
- ??????
- ??????,????????
- ?????????
- ???????(big-endian?little-endian)
- ?????
- ???????????????
- ?????
- ???????,?????
- ??C/C???????
- ??????????????
- ???????
- ????
69Crypto
- Open-source??????
- ?????4.2
- ???????
- ????????????
- ???????????????
- ??????
- ???????
- ???????
- ???????
- ??,??
70(No Transcript)
71???????
- char EncryptString(const char instr, const char
passPhrase) -
- unsigned int lenstrlen(instr)
- char outstr
- DefaultEncryptorWithMAC encryptor(passPhrase,
new HexEncoder()) - encryptor.Put((byte )instr, len)
- encryptor.Close()
- unsigned int outputLength encryptor.MaxRetrieve
able() - outstr new charoutputLength1
- encryptor.Get((byte )outstr, outputLength)
- outstroutputLength 0
- return outstr
72???????
- void EncryptFile(const char in, const char out,
const char passPhrase) -
- FileSource f(in, true, new
DefaultEncryptorWithMAC(passPhrase, new
FileSink(out))) -
- void DecryptFile(const char in, const char out,
const char passPhrase) -
- DefaultDecryptorWithMAC p
- FileSource file(in, false, p new
DefaultDecryptorWithMAC(passPhrase)) - file.Pump(256)
- if (p-gtCurrentState() ! DefaultDecryptorWithMAC
KEY_GOOD) -
- cerr ltlt "Incorrect passphrase.\n"
- return
-
- file.Attach(new FileSink(out))
- file.PumpAll()
- file.Close()
- if (p-gtCurrentState() ! DefaultDecryptorWithMAC
MAC_GOOD)
73????
- void DigestFile(const char filename)
-
- MD5 md5
- SHA shs
- RIPEMD160 ripemd
- BufferedTransformation outputsnew
HashFilter(md5), new HashFilter(shs), new
HashFilter(ripemd) - FileSource file(filename, true, new Fork(3,
outputs)) - cout ltlt "MD5 "
- outputs0-gtAttach(new HexEncoder(new
FileSink(cout))) - cout ltlt endl
- cout ltlt "SHA "
- outputs1-gtAttach(new HexEncoder(new
FileSink(cout))) - cout ltlt endl
- cout ltlt "RIPEMD-160 "
- outputs2-gtAttach(new HexEncoder(new
FileSink(cout))) - cout ltlt endl
74??RSA???
- void GenerateRSAKey(unsigned int keyLength, const
char privFilename, const char pubFilename,
const char seed) -
- RandomPool randPool
- randPool.Put((byte )seed, strlen(seed))
- RSAES_OAEP_SHA_Decryptor priv(randPool,
keyLength) - HexEncoder privFile(new FileSink(privFilename))
- priv.DEREncode(privFile)
- privFile.Close()
- RSAES_OAEP_SHA_Encryptor pub(priv)
- HexEncoder pubFile(new FileSink(pubFilename))
- pub.DEREncode(pubFile)
- pubFile.Close()
75??RSA??
- char RSAEncryptString(const char pubFilename,
const char seed, const char message) -
- FileSource pubFile(pubFilename, true, new
HexDecoder) - RSAES_OAEP_SHA_Encryptor pub(pubFile)
- if (strlen(message) gt pub.MaxPlainTextLength())
- cerr ltlt "message too long for this key\n"
abort() -
- RandomPool randPool
- randPool.Put((byte )seed, strlen(seed))
- char outstr new char2pub.CipherTextLength()
1 - pub.Encrypt(randPool, (byte )message,
strlen(message), (byte )outstr) - HexEncoder hexEncoder
- hexEncoder.Put((byte )outstr,
pub.CipherTextLength()) - hexEncoder.Close()
- hexEncoder.Get((byte )outstr,
2pub.CipherTextLength()) - outstr2pub.CipherTextLength() 0
76??????
- void GzipFile(const char in, const char out,
int deflate_level) -
- FileSource(in, true, new Gzip(deflate_level, new
FileSink(out))) -
- void GunzipFile(const char in, const char out)
-
- FileSource(in, true, new Gunzip(new
FileSink(out))) -
77????
- ?
- William Stallings, Cryptography and network
security principles and practice, Second
Edition. - Bruce Shneier, Applied cryptography protocols,
algorithms, and sourcecode in C, Second Edition. - ??
- ???????
- Web??
- http//www.eskimo.com/weidai/cryptlib.html