锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产综合18久久久久久,一区二区三区.www,麻豆精品视频在线观看http://www.shnenglu.com/bilicon/category/4783.html緇х畫瀛︿範涓?/description>zh-cnWed, 02 Jul 2008 20:12:20 GMTWed, 02 Jul 2008 20:12:20 GMT60Rijndael鍔犲瘑綆楁硶鐨勪粙緇?http://www.shnenglu.com/bilicon/articles/29454.htmlbiliconbiliconMon, 06 Aug 2007 14:45:00 GMThttp://www.shnenglu.com/bilicon/articles/29454.htmlhttp://www.shnenglu.com/bilicon/comments/29454.htmlhttp://www.shnenglu.com/bilicon/articles/29454.html#Feedback0http://www.shnenglu.com/bilicon/comments/commentRss/29454.htmlhttp://www.shnenglu.com/bilicon/services/trackbacks/29454.html 浣滆?鏋楃鍏?鍙朵箟闆?鏉ㄥ浗楦?/span>

鏈枃閽堝Rijndael鍔犲瘑綆楁硶鐨勬暟瀛︾悊璁鴻儗鏅紝綆楁硶鐨勬灦鏋勶紝鍥炲悎鐨勮漿鎹紝閲戦挜鐨勪駭鐢燂紝浠ュ強鍚勭鏀誨嚮鐮村瘑娉曠瓑絳夛紝鍋氫簡涓浜涚畝鍗曠殑浠嬬粛銆?/span>

涓銆佺畝浠?/span>

鍦?/span>AES ( Advanced Encryption Standard ) 鐨勯夋嫈涓紝浠庢渶鍒濈殑鍗佷簲涓畻娉曪紝鍒板崄涓佷簲涓紝閫愭絳涢夊嚭閫傚悎鐢ㄦ潵浣滀負涓嬩竴浠e姞瀵嗙畻娉曠殑鏍囧噯銆?/span>Rijndael鍦ㄧ粡榪囦簡涓鐣椂鏃ョ殑鑰冮獙涔嬪悗錛屼篃涓鐩村悕鍒楀墠鐭涖傜洿鑷?st1:chsdate w:st="on" IsROCDate="False" IsLunarDate="False" Day="2" Month="10" Year="2005">鍗佹湀浜屾棩錛?/span>Rijndael鎵嶈劚棰栬屽嚭錛岃繖綃囨枃绔犱究鏄拡瀵?/span>Rijndael浣滅畝瑕佺殑浠嬬粛銆?/span>

Rijndael鏄竴涓弽澶嶈繍綆楃殑鍔犲瘑綆楁硶錛屽畠鍏佽鍙彉鍔ㄧ殑鏁版嵁鍖哄潡鍙婇噾閽ョ殑闀垮害銆傛暟鎹尯鍧椾笌閲戦挜闀垮害鐨勫彉鍔ㄦ槸鍚勮嚜鐙珛鐨勩?/span>

鍦?/span>Rijndael綆楁硶涓畾涔変簡鍑犱釜鍚嶈瘝錛屽垎榪板涓嬶細

State錛氬湪榪愮畻榪囩▼涓墍浜х敓鐨勪腑闂村鹼紝鏄竴涓?/span>4×Nb鐨勭煩闃碉紝Nb鍙敱鏁版嵁闀垮害闄や互32浣嶆眰寰楋紝涔熷氨鏄妸鏁版嵁鍒嗗壊鎴?/span>Nb涓尯鍧椼?/span>

Cipher Key錛氱敤鏉ュ仛鍔犲瘑榪愮畻鐨勯噾閽ワ紝褰㈠紡鏄竴涓?/span>4×Nk鐨勭煩闃碉紝Nk鍙敱閲戦挜闀垮害闄や互32浣嶆眰寰楋紝涔熷氨鏄妸閲戦挜鍒嗗壊鎴?/span>Nk涓?/span>32浣嶇殑瀛愰噾閽ャ?/span>

鍦?/span>Rijndael綆楁硶涓紝榪愮畻鐨勫洖鍚堟暟(Nr)鏄敱Nb鍙?/span>Nk鎵鍐沖畾鐨勶紝鍥炲悎鏁扮殑鍙樺姩瀹氫箟濡備笅琛ㄣ?/span>

 

Nr

Nb=4

Nb=6

Nb=8

Nk=4

10

12

14

Nk=6

12

12

14

Nk=8

14

14

14

浜屻?/span>Rijndael鐨勬暟瀛﹁儗鏅?/span>

鍦?/span>Rijndael涓嬌鐢ㄤ簡璁稿瀛楄妭灞傜駭鐨勮繍綆楋紝鑰岃繖浜涜繍綆楁槸浠?/span>GF(28)涓哄熀紜鏋舵瀯銆備篃鏈変竴浜涢噰鐢ㄤ簡4-byte鐨勫瓧緇勮繍綆椼傚湪榪欓儴鍒嗭紝鎴戜滑灝嗕粙緇嶈繖浜涘熀鏈殑鏁板鍘熺悊銆?/span>

(1)       GF(28)鐨勫畾涔?/span>

鍋囪涓涓瓧鑺?/span>b鐢?/span>b7b6b5b4b3b2b1b0緇勬垚錛屾垜浠彲浠ユ妸榪欎簺bi鎯寵薄鎴愪竴涓?/span>7嬈″欏瑰紡鐨勭郴鏁幫紝鑰岃繖浜涚郴鏁頒笉鏄?/span>0灝辨槸1錛?/span>

b7 x7+ b6 x6+ b5 x5+ b4 x4+ b3 x3+ b2 x2+ b1 x + b0錛?/span>

渚嬪錛?/span>(57)16鐨勪簩榪涘埗琛ㄧず娉曚負(0101,0111)2琛ㄧず鎴愬欏瑰紡錛屽垯涓猴細

x6+ x4+ x2+ x + 1 .

(2)       鍔犳硶

涓や釜澶氶」寮忕殑鍔犳硶錛屽垯鏄畾涔変負鐩稿悓鎸囨暟欏圭殑緋繪暟鍜屽啀妯′綑2錛岀畝鍗曠殑璇村氨鏄綔EXOR榪愮畻(i.e., 1+1=0)銆備緥濡傦細

(57)16+(83)16=(01010111)2+(10000011)2 = (11010100)2 = (D4)16

       鎴栨槸(x6+x4+x2+x+1) + (x7+x+1) = x7+x6+x4+x2

(3)       涔樻硶

鍦ㄤ箻娉曢噷闈紝澶氶」寮忕浉涔樹箣鍚庣殑緇撴灉寰堝鏄撻犳垚婧綅鐨勯棶棰橈紝瑙e喅婧綅鐨勬柟寮忔槸鎶婄浉涔樼殑緇撴灉錛屽啀妯′綑涓涓笉鍙垎瑙g殑澶氶」寮?/span>m(x)銆傚湪Rijndael涓紝瀹氫箟涓涓繖鏍峰瓙鐨勫欏瑰紡涓?/span>

m(x)=x8+x4+x3+x+1鎴栨槸(11B)16

渚嬪錛?/span>

(57)16鈥?/span>(83)16 = ( x6+ x4+ x2+ x + 1)鈥?/span> ( x7+ x + 1) = x13+ x11+ x9+ x8+ x7+x7+ x5+ x3+ x2+x+x6+ x4+ x2+ x + 1

= (x13+ x11+ x9+ x8+ x6+ x5+ x4+ x3+ 1+x13+ x11+ x9+ x8+ x6+ x5+ x4+ x3+ 1) modulo (x8+ x4+ x3+ x + 1)

= x7+ x6+ 1=(C1)16

(4)       涔樹互x

鑻ユ妸b(x)涔樹笂x錛屽緱鍒?/span>b7 x8+ b6 x7+ b5 x6+ b4 x5+ b3 x4+ b2 x3+ b1 x2 + b0x銆傝嫢b7=0錛屼笉浼氬彂鐢熸孩浣嶉棶棰橈紝絳旀鍗蟲槸姝g‘鐨勶紱鑻?/span>b7=1錛屽彂鐢熸孩浣嶉棶棰橈紝蹇呴』鍑忓幓m(x)銆傛垜浠彲浠ユ妸榪欑榪愮畻琛ㄧず涓?/span>xtime(x)錛屽叾榪愮畻鏂瑰紡涓?/span>left shift(鑻ユ孩浣嶅垯鍜?/span>(1B)16鍋?/span>EXOR榪愮畻)錛屼緥濡傦細‘57’ · ‘13’ = ‘FE’

57’ · ‘02’ = xtime(57) = ‘AE’

57’ · ‘04’ = xtime(AE) = ‘47’

57’ · ‘08’ = xtime(47) = ‘8E’

57’ · ‘10’ = xtime(8E) = ‘07’

57’ · ‘13’ = ‘57’ · (‘01’02’10’) = ‘57’ ‘AE’ 07’ = ‘FE’

 

涓夈?/span>Rijndael鐨勫姞瀵嗘灦鏋?/span>

Rijndael鍔犲瘑綆楁硶鏄敱涓涓?/span>initial Round Key addition錛?/span>Nr-1涓洖鍚堣繍綆楋紝鍙婁竴涓?/span>final round鎵緇勬垚銆傚姞瀵嗚繃紼嬩互C璇█浼爜鍙欒堪濡備笅錛?/span>

Rijndael(State, CipherKey)

//state琛ㄧず杈撳叆鐨勬暟鎹槑鏂囷紝

//CipherKey琛ㄧず浣跨敤鐨勫姞瀵嗛噾閽ワ紝

//ExpandedKey琛ㄧず姣忎釜Round浣跨敤鐨勫瓙閲戦挜銆?/span>

{

KeyExpansion(CipherKey, ExpandedKey);

AddRoundKey(State, ExpandedKey);

For ( i=1; i<Nr; i++)

Round(State, ExpandedKey+Nb×i);

FinalRound(State, ExpandedKey+Nb×Nr);

}

涓婅堪綆楁硶涓殑Key Expansion錛屽彲浠ュ厛琛岃綆楀嚭鏉ワ紝鎵浠ュ姞瀵嗚繃紼嬪彲浠ョ畝鍖栦負錛?/span>

Rijndael(State,ExpandedKey)

//State琛ㄧず杈撳叆鐨勬暟鎹槑鏂囷紝

//ExpandedKey琛ㄧず姣忎釜Round浣跨敤鐨勫瓙閲戦挜銆?/span>

{

AddRoundKey(State,ExpandedKey);

For( i=1 ; i<Nr ; i++ )

{

Round(State,ExpandedKey + Nb×i) ;

}

FinalRound (State, ExpandedKey + Nb×Nr);

}

鍚勪釜瀛愯繍綆椾粙緇嶅涓嬨?/span>

鍥炲悎杞崲(Round transformation)錛?/span>

鍥炲悎杞崲鍖呭惈鍥涗釜涓嶅悓鐨勫伐浣滐紝鍏剁畻娉曞涓嬶細

Round(State,RoundKey)

//State琛ㄧず杈撳叆鐨勬暟鎹槑鏂囷紝

//RoundKey琛ㄧず姣忎釜Round浣跨敤鐨勫瓙閲戦挜銆?/span>

{

ByteSub(State);

ShiftRow(State);

MixColumn(State);

AddRoundKey(State,RoundKey);

}

 

 

綆楁硶涓殑緇堟鍥炲悎(Final round)鍖呭惈涓嬪垪宸ヤ綔欏圭洰錛?/span>

FinalRound(State,RoundKey)

//State琛ㄧず杈撳叆鐨勬暟鎹槑鏂囷紝

//RoundKey琛ㄧず姣忎釜Round浣跨敤鐨勫瓙閲戦挜銆?/span>

{

ByteSub(State) ;

ShiftRow(State) ;

AddRoundKey(State,RoundKey);

}

浠ヤ笅閽堝姣忎釜鍥炲悎杞崲鐨勮繍綆楄繃紼嬶紝浣滀竴涓繁鍏ョ殑浠嬬粛錛屽彲浠ユ洿娓呮綆楁硶鐨勮繃紼嬨?/span>

1.            瀛楄妭鍙栦唬杞崲(ByteSub transformation)錛?/span>

瀛楄妭杞崲鏄竴涓互瀛楄妭涓哄崟浣嶇殑闈炵嚎鎬у彇浠h繍綆楋紝鍙栦唬琛?/span>(S-Box)鏄粡榪囦袱涓繍綆楄繃紼嬭屽緩绔嬶紝騫朵笖鏄彲閫嗙殑銆?/span>

棣栧厛鎵懼嚭姣忎釜瀛楄妭鍦?/span>GF(28)涓殑涔樻硶鍙嶅厓绱狅紱

鎺ョ潃緇忚繃涓涓豢灝?/span>(Affine)杞崲榪愮畻錛屽畾涔夊涓嬶細

 

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

瀛楄妭鍙栦唬(ByteSub)榪愮畻瀵?/span>State鐨勫獎鍝嶏紝濡備笅鍥炬墍紺猴細

 

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

瀛楄妭鍙栦唬(ByteSub)杞崲鐨勫弽榪愮畻錛?/span>

璁$畻浠垮皠瀵瑰簲涔嬪悗鐨勭浉鍙嶈繍綆楀彲寰楀埌S-1-Box錛屼互姝?/span>S-1-Box鍋氬瓧鑺傚彇浠?/span>(ByteSub)鍗沖彲銆?/span>

2.            縐誨垪杞崲( ShiftRow transformation )錛?/span>

鍦ㄨ繖涓漿鎹腑錛?/span>State鐨勬瘡涓鍒椾互涓嶅悓鐨勫亸縐婚噺鍋氱幆鐘朵綅縐伙紝絎?/span>0鍒椾笉鍔紝絎竴鍒椾綅縐?/span>C1涓瓧鑺傦紝絎簩鍒椾綅縐?/span>C2涓瓧鑺傦紝絎笁鍒椾綅縐?/span>C3涓瓧鑺傘備綅縐葷殑鍋忕Щ閲?/span>C1,C2,C3璺熷尯鍧楃殑鏁扮洰(Nb)鏈夊叧錛屽畾涔夊涓嬭〃錛?/span>

Nb

C1

C2

C3

4

1

2

3

6

1

2

3

8

1

3

4

縐誨垪杞崲(ShiftRow)榪愮畻瀵逛簬State鐨勫獎鍝嶏紝鍥劇ず濡備笅錛?/span>

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

縐誨垪杞崲(ShiftRow)鐨勫弽榪愮畻錛?/span>

瀵圭浜岀涓夊強絎洓鍒楀仛Nb-C1,Nb-C2,Nb-C3涓瓧鑺傜殑鐜姸浣嶇Щ鍗沖彲銆?/span>

3.            娣瘋杞崲(MixColumn transformation)錛?/span>

鍦ㄨ繖涓漿鎹腑錛屾妸State褰撲綔涓涓瓨鍦?/span>GF(28)涓殑澶氶」寮忋傚茍涓斿涓涓浐瀹氱殑澶氶」寮?/span>c(x)浣滀箻娉曪紝濡傛灉鍙戠敓婧綅錛屽垯鍐嶆ā浣?/span>x4+1銆傝〃紺哄涓嬶細

c(x) = ‘03’ x3 + ‘01’ x2 + ‘01’ x + ‘02’ .

c(x)涓?/span>x4+1浜掕川錛屼護b(x) = c(x) Ä a(x)錛屼互鐭╅樀涔樻硶琛ㄧず濡備笅錛?/span>

 

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

State緇忚繃娣瘋(MixColumn)榪愮畻涔嬪悗鐨勫彉鍖栧涓嬶細

 

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

娣瘋(MixColumn)杞崲鐨勫弽榪愮畻錛屽垯鏄箻涓婁竴涓壒孌婄殑澶氶」寮?/span>d(x)錛?/span>

(‘03’x3 + ‘01’x2 + ‘01’x + ‘02’ ) Ä d(x) = ‘01’,

d(x) = ‘0B’x3 + ‘0D’x2 + ‘09’x + ‘0E’ .

4.            The Round Key Addition錛?/span>

榪欎釜榪愮畻涓昏鏄妸姣忎竴涓洖鍚堥噾閽?/span>(Round Key)閫忚繃綆鍗曠殑bitwise EXOR鍔犲叆鍒版瘡涓涓?/span>State涓紝浠ュ浘紺哄涓嬶細

 

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

鍥涖侀噾閽ョ殑鎺掔▼(Key Schedule)

鍥炲悎閲戦挜(Round Key)鏄粠鍔犲瘑閲戦挜(Cipher Key)緇忚繃榪愮畻浜х敓鍑烘潵鐨勩傞噾閽ユ帓紼?/span>(Key Schedule)鏄敱閲戦挜鎵╁厖(Key Expansion)鍙婂洖鍚堥噾閽ョ殑閫夋嫨(Round Key Selection)緇勬垚鐨勶紝鍩烘湰鐨勭悊璁哄涓嬶細

       鎵鏈夊洖鍚堥噾閽ョ殑鎬諱綅鏁版槸鎶婂尯鍧楅暱搴?/span>(block length)涔樹笂鍥炲悎鏁板姞1錛?/span>(鏈?/span>Nr-1涓洖鍚堬紝鍔犱笂涓涓粓姝㈠洖鍚?/span>(final round))錛屼緥濡傦紝128涓綅鐨勫尯鍧楅暱搴︾粡榪?/span>10涓洖鍚堣繍綆楋紝鎵闇瑕佺敤鍒扮殑鎵鏈夊洖鍚堥噾閽ョ殑鎬諱綅鏁頒負1408涓綅銆?/span>

       鍔犲瘑閲戦挜(Cipher Key)蹇呴』鎵╁厖涓烘墿鍏呴噾閽?/span>(Expanded Key)銆?/span>

       鍥炲悎閲戦挜鏄粠鎵╁厖閲戦挜涓夊嚭鏉ョ殑錛岄夋嫨鐨勬柟寮忓涓嬶細

       絎竴涓洖鍚堥噾閽ョ敱鍓?/span>Nb涓瓧緇勭粍鎴愶紝絎簩涓洖鍚堥噾閽ョ敱鎺ヤ笅鏉ョ殑Nb涓瓧緇勭粍鎴愶紝浣欐綾繪帹銆?/span>

(1)       閲戦挜鐨勬墿鍏?/span>( Key Expansion )錛?/span>

鎵╁厖鍚庣殑閲戦挜鏄竴涓?/span>4-byte鐨勭嚎鎬ф暟緇勶紝琛ㄧず涓?/span>W[Nb×(Nr+1)]銆傚墠Nk涓瓧緇勫寘鍚簡鍔犲瘑閲戦挜(Cipher Key)銆?/span>

       閲戦挜鎵╁厖鍑藉紡鍜?/span>Nk鏄伅鎭浉鍏崇殑錛屽垎涓轟袱縐嶆儏鍐佃繍浣滐紝涓鏄綋Nk灝忎簬鎴栫瓑浜?/span>6錛屽彟澶栧垯鏄綋Nk澶т簬6錛屼互浼爜鍙欒堪濡備笅錛?/span>

褰?/span>Nk鈮?/span>6鏃訛紝

KeyExpansion(byte Key[4×Nk] word W[Nb×(Nr+1)])

{

for(i = 0; i < Nk; i++)

W[i] = (Key[4×i], Key[4×i+1], Key[4×i+2], Key[4×i+3] );

for(i = Nk; i < Nb×(Nr + 1); i++)

{

temp = W[i - 1];

if (i % Nk == 0)

temp = SubByte(RotByte(temp)) ^ Rcon[i / Nk];

W[i] = W[i - Nk] ^ temp;

}

}

鍦ㄤ笂闈㈢殑瀛愮▼搴忎腑錛?/span>SubByte(W)浼犲洖涓涓?/span>4-byte鐨勫瓧緇勶紝榪欎簺瀛楃粍鏄緭鍏ョ殑瀛楃粍緇忚繃S-box鐨勮漿鎹㈡墍浜х敓鐨勭浉瀵瑰瓧緇勩?/span>RotByte(W)鍒欐槸浼犲洖緇忚繃鏃嬭漿鐨勫瓧緇勩?/span>

褰?/span>Nk錛?/span>6鏃訛紝

KeyExpansion(byte Key[4×Nk] word W[Nb×(Nr+1)])

{

for(i = 0; i < Nk; i++)

W[i] =  (key[4×i],key[4×i+1], key[4×i+2], key[4×i+3] );

for(i = Nk; i < Nb×(Nr + 1); i++)

{

temp = W[i - 1];

if (i % Nk == 0)

temp = SubByte(RotByte(temp)) ^ Rcon[i / Nk];

else if (i % Nk == 4)

temp = SubByte(temp);

W[i] = W[i - Nk] ^ temp;

}

}

浠ヤ笂涓ょ鎯呭喌鐨勭浉寮傚鍦ㄤ簬褰?/span>Nk鈮?/span>6鏃訛紝(i-4)鏄?/span>Nk鐨勫嶆暟鏃訛紝瀵逛簬W[i-1]鍏堟墽琛?/span>SubByte錛屽啀鎵цEXOR銆?/span>

涓婅堪鍥炲悎甯告暟瀹氫箟濡備笅錛?/span>

Rcon[i] = (RC[i],‘00’,‘00’,‘00’)錛屽叾涓?/span>RC[0]=’01’錛?/span>RC[i]=xtime(Rcon[i-1])銆?/span>

(2)       閫夋嫨鍥炲悎閲戦挜(Round Key Selection)

絎?/span>i涓洖鍚堥噾閽ユ槸鎸囧湪瀛樺湪鍥炲悎閲戦挜緙撳啿鍖虹殑瀛楃粍W[Nb*i]鍒?/span>W[Nb*(i+1)]錛屽浘紺哄涓嬶細

 

(鏈浘鎽樺綍鑷弬鑰冩枃鐚?/span>[1])

浜斻佸畨鍏ㄦу垎鏋?/span>

       鎴戜滑閽堝浠ヤ笅宸茬煡鐨勬敾鍑繪硶瀵?/span>Rijndael鐨勫畨鍏ㄦу垎鏋愪綔涓綆瑕佸彊榪幫紝鍖呮嫭宸垎鏀誨嚮娉?/span>(Differential Cryptanalysis)錛岀嚎鎬ф敾鍑繪硶(Linear Cryptanalysis)錛屽鉤鏂規敾鍑繪硶(The Square Attack)錛屽唴鎻掓敾鍑繪硶(Interpolation attacks)絳夋敾鍑繪柟寮忋?/span>

(1)       宸垎鏀誨嚮娉?/span>( Differential Cryptanalysis )

       姝ゆ敾鍑繪硶鏄竴縐?/span>Chosen-plaintext attack錛屽埄鐢ㄥぇ閲忓凡鐭ョ殑鏄庢枃/瀵嗘枃瀵逛箣闂寸殑宸紓錛屾嵁浠ユ帹嫻嬪嚭閲戦挜鐨勪綅鍊箋傚湪澶ч儴鍒嗙殑鍥炲悎榪愮畻涓?/span>(鍥炲悎鏁拌秴榪?/span>3)錛岃嫢瀛樺湪瓚呰繃21-n(n鎸囩殑鏄尯鍧楅暱搴?/span>)姣斾緥鐨勫彲棰勬祴鎬х殑宸紓錛岃繖涓敾鍑繪硶灝卞彲浠ユ帹嫻嬪嚭閲戦挜鐨勪綅鍊箋傚湪Rijndael涓紝宸茬粡璇佹槑鍦ㄧ粡榪?/span>Rijndael鍥涗釜鍥炲悎鐨勮繍綆楀悗錛屽瓨鍦ㄤ笉瓚呰繃2-150姣斾緥鐨勫彲棰勬祴鎬у樊寮傦紝鍦ㄥ叓涓洖鍚堣繍綆椾腑涓嶈秴榪?/span>2-300銆傝緇嗚瘉鏄庤繃紼嬶紝璇峰弬鐓у弬鑰冩枃鐚?/span>

(2)       綰挎ф敾鍑繪硶( Linear Cryptanalysis )

       榪欐槸涓縐?/span>Known-plaintext鏀誨嚮娉曪紝鍒╃敤澶ч噺鎼滈泦鍒扮殑鏄庢枃/瀵嗘枃瀵圭殑鐩稿叧鎬э紝瀵瑰姞瀵嗘硶榪涜鏀誨嚮銆傛槑鏂?/span>/瀵嗘枃瀵圭殑鐩稿叧鎬х敱綰挎ц建榪?/span>(Linear trails)鎵緇勬垚錛岀敱浜庣嚎鎬ц建榪圭殑鐩稿叧緋繪暟涓?/span>Round keys鐨勫兼湁瀵嗗垏鍏崇郴錛岄忚繃鐩稿叧緋繪暟鐨勬璐熷彿錛岀嚎鎬ф敾鍑繪硶灝卞彲浠ユ壘鍑洪噾閽ュ箋傝瀵規姉榪欑鏀誨嚮娉曪紝鏈変竴涓繀瑕佹潯浠跺氨鏄嬌榪欑鐩稿叧緋繪暟澶т簬2n/2鐨勭嚎鎬ц建榪逛笉瀛樺湪銆傚湪Rijndael涓紝宸茬粡璇佹槑鍑哄綋鎵ц鍥涗釜鍥炲悎鏃訛紝涓嶅瓨鍦ㄧ浉鍏崇郴鏁板ぇ浜?/span>2-75鐨勭嚎鎬ц建榪癸紱鍦ㄦ墽琛屽叓涓洖鍚堟椂錛屽叾鐩稿叧緋繪暟澶т簬2-150鐨勭浉鍏崇郴鏁頒害涓嶅瓨鍦ㄣ傝緇嗚瘉鏄庤繃紼嬭鍙傜収鍙傝冩枃鐚?/span>

(3)       騫蟲柟鏀誨嚮娉?/span>( The Square attack )

       榪欑鏀誨嚮娉曟槸涓縐?/span>chosen- plaintext attack錛岃屼笖鍜屽瓧鑺傚彇浠?/span>(ByteSub)錛屾販琛?/span>(MixColumn)鏃剁殑澶氶」寮忎箻娉曪紝閲戦挜鐨勬帓紼?/span>(Key Schedule)絳夎繍綆楁棤鍏熾傚綋Rijndael鎵ц6涓洖鍚堜互涓婃椂錛屾縐嶆柟寮忔瘮瀹屽叏鐨勯噾閽ユ悳瀵?/span>(exhaustive key search)鏉ョ殑鏇存湁鏁堢巼銆傚叧浜庢縐嶆敾鍑繪柟寮忕殑璇﹀敖鎻忚堪鍙?/span>Rijndael濡備綍寤朵幾姝ょ鏀誨嚮鏂瑰紡錛岃鍙傜収鍙傝冩枃鐚?/span>

(4)       鍐呮彃鏀誨嚮娉?/span>( Interpolation attacks )

       鍦ㄨ繖縐嶆敾鍑繪硶涓紝鏀誨嚮鑰呭埄鐢ㄥ姞瀵嗙殑杈撳叆鍙婅緭鍑洪厤瀵癸紝寤虹珛涓浜涘欏瑰紡銆傚鏋滃姞瀵嗙殑緇勪歡鏈変竴涓畝媧佺殑浠f暟灞曞紑寮忥紝騫朵笖鍜岀鐞嗙殑澶嶆潅搴︾粨鍚堝湪涓璧鋒椂錛岃繖縐嶆敾鍑繪硶渚挎槸鍙鐨勩傚熀鏈殑鏀誨嚮鏂瑰紡鏄鏋滄敾鍑昏呭緩绔嬬殑浠f暟灞曞紑寮忕殑闃跺害(degree)寰堝皬錛屽彧闇瑕佷竴浜涘姞瀵嗘硶鐨勮緭鍏ュ強杈撳嚭閰嶅灝卞彲浠ュ緱鍒頒唬鏁板睍寮寮忕殑鍚勯」緋繪暟銆傜劧鑰岋紝鍦?/span>GF(28)涓殑鍙栦唬鐭╅樀(S-box)錛屽畠鐨勫睍寮寮忎負錛?/span>63+8fx127+b5x191+01x223+f4x239+25x247+f9x251+09x253+05x254銆傚叾浣欎粙緇嶏紝璇峰弬鐓у弬鑰冩枃鐚?/span>

(5)銆佸急閲戦挜(Weak keys)

       鍏充簬寮遍噾閽ョ殑鍙戠敓錛屽熀鏈笂鏄洜涓哄姞瀵嗘硶鐨勯潪綰挎ц繍綆椾笌瀹為檯閲戦挜鍊兼湁瀵嗗垏鍏崇郴銆傝岃繖縐嶉棶棰樹笉瀛樺湪浜?/span>Rijndael涔嬩腑錛屽洜涓哄湪Rijndael涓紝閲戦挜鏄互EXOR榪愮畻錛岃屾墍鏈夌殑闈炵嚎鎬ц繍綆楅兘瀹氫箟鍦ㄥ彇浠g煩闃?/span>(S-box)涓傚湪Rijndael涓紝瀵歸噾閽ョ殑閫夋嫨錛屾槸娌℃湁闄愬埗鐨勩?/span>

鍏佺粨璁猴細

       浠ヤ笂瀵?/span>Rijndael浣滀竴綆瑕佷粙緇嶄箣鍚庯紝鎴戜滑浠?/span>Rijndael鐨勪紭鐐逛笌闄愬埗浣滀負鎴戜滑鐨勭粨璁恒?/span>

(1)銆?/span>Rijndael鏈変互涓嬩紭鐐?/span>鈥?/span>

浠ュ疄浣滆鐐硅岃█

1.            Rijndael鍙互瀹炰綔鍦?/span>Pentium ( Pro ) 絳夎綆楁満涓婏紝騫跺凡鐩稿綋蹇殑閫熷害澶勭悊榪愮畻錛涜屽湪琛ㄦ牸澶у皬涓庢晥鐜囦箣闂存槸鍙互鍋氬彇鑸嶇殑銆?/span>

2.            Rijndael鍙互瀹炰綔鍦ㄦ櫤鑳藉崱(Smart Card)涓婏紝浣跨敤灝戦噺鐨?/span>RAM錛屽皯閲忕殑紼嬪簭浠g爜錛涘湪ROM涓庢晥鐜囦箣闂翠篃鏄彲浠ュ仛鍙栬垗鐨勩?/span>

3.            鍦ㄨ璁′笂錛屽洖鍚堢殑杞崲鏄彲騫寵澶勭悊鐨勩?/span>

4.            鍔犲瘑娉曚笉閲囩敤綆楁湳榪愮畻錛屼笉浼氬洜涓轟笉鍚屽鐞嗗櫒鏋舵瀯鑰屾湁鎵鍋忓樊銆?/span>

璁捐綆鍗曞寲錛?/span>

1.            璁捐涓婁笉寮曠敤鍏跺畠鍔犲瘑緇勪歡錛屽S-box銆?/span>

2.            瀹夊叏搴︿笉寤虹珛鍦ㄤ竴浜涘垎鏋愪笉澶熸槑紜殑綆楁湳榪愮畻涔嬩笂銆?/span>

3.            鍔犲瘑娉曠揣鍑戯紝涓嶆槗钘忓叆鏆楅棬絳夌▼搴忎唬鐮併?/span>

闄ゆ涔嬪錛?/span>Rijndael鏇村厑璁稿彲鍙樺姩鐨勫尯鍧楅暱搴﹀強閲戦挜闀垮害錛屽叾闀垮害鍙敱128浣嶅埌256浣嶄箣闂達紱鎵浠ュ洖鍚堟暟涔熸槸鍙彉鍔ㄧ殑銆?/span>

(2)Rijndael鐨勯檺鍒訛細

鍦ㄨВ瀵嗚繃紼嬩腑鏈変互涓嬮檺鍒?/span>

1.            瀹炰綔鍦ㄦ櫤鎱у崱鏃訛紝瑙e瘑涓嶅鍔犲瘑鏉ョ殑鏈夋晥鐜囷紝瑙e瘑闇瑕佹洿澶氱殑紼嬪簭浠g爜鍙?/span>cycles錛屼絾鏄窡鍏跺畠綆楁硶姣旇搗鏉ワ紝浠嶇劧鏄揩閫熺殑銆?/span>

2.            浠ヨ蔣浠惰岃█錛屽姞瀵嗗拰瑙e瘑浣跨敤涓嶅悓鐨勭▼搴忓拰琛ㄦ牸銆?/span>

3.            浠ョ‖浠惰岃█錛岃В瀵嗗彧鑳介噸鐢ㄩ儴鍒嗗姞瀵嗙殑鐢佃礬銆?/span>



bilicon 2007-08-06 22:45 鍙戣〃璇勮
]]>
MD5~RFC 1321http://www.shnenglu.com/bilicon/articles/29263.htmlbiliconbiliconFri, 03 Aug 2007 04:35:00 GMThttp://www.shnenglu.com/bilicon/articles/29263.htmlhttp://www.shnenglu.com/bilicon/comments/29263.htmlhttp://www.shnenglu.com/bilicon/articles/29263.html#Feedback0http://www.shnenglu.com/bilicon/comments/commentRss/29263.htmlhttp://www.shnenglu.com/bilicon/services/trackbacks/29263.html Network Working Group R. Rivest Request for Comments: 1321 MIT Laboratory for Computer Science and RSA Data Security, Inc. April 1992 The MD5 Message-Digest Algorithm Status of this Memo This memo provides information for the Internet community. It does not specify an Internet standard. Distribution of this memo is unlimited. Acknowlegements We would like to thank Don Coppersmith, Burt Kaliski, Ralph Merkle, David Chaum, and Noam Nisan for numerous helpful comments and suggestions. Table of Contents 1. Executive Summary 1 2. Terminology and Notation 2 3. MD5 Algorithm Description 3 4. Summary 6 5. Differences Between MD4 and MD5 6 References 7 APPENDIX A - Reference Implementation 7 Security Considerations 21 Author's Address 21 1. Executive Summary This document describes the MD5 message-digest algorithm. The algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA. Rivest [Page 1] RFC 1321 MD5 Message-Digest Algorithm April 1992 The MD5 algorithm is designed to be quite fast on 32-bit machines. In addition, the MD5 algorithm does not require any large substitution tables; the algorithm can be coded quite compactly. The MD5 algorithm is an extension of the MD4 message-digest algorithm 1,2]. MD5 is slightly slower than MD4, but is more "conservative" in design. MD5 was designed because it was felt that MD4 was perhaps being adopted for use more quickly than justified by the existing critical review; because MD4 was designed to be exceptionally fast, it is "at the edge" in terms of risking successful cryptanalytic attack. MD5 backs off a bit, giving up a little in speed for a much greater likelihood of ultimate security. It incorporates some suggestions made by various reviewers, and contains additional optimizations. The MD5 algorithm is being placed in the public domain for review and possible adoption as a standard. For OSI-based applications, MD5's object identifier is md5 OBJECT IDENTIFIER ::= iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) 5} In the X.509 type AlgorithmIdentifier [3], the parameters for MD5 should have type NULL. 2. Terminology and Notation In this document a "word" is a 32-bit quantity and a "byte" is an eight-bit quantity. A sequence of bits can be interpreted in a natural manner as a sequence of bytes, where each consecutive group of eight bits is interpreted as a byte with the high-order (most significant) bit of each byte listed first. Similarly, a sequence of bytes can be interpreted as a sequence of 32-bit words, where each consecutive group of four bytes is interpreted as a word with the low-order (least significant) byte given first. Let x_i denote "x sub i". If the subscript is an expression, we surround it in braces, as in x_{i+1}. Similarly, we use ^ for superscripts (exponentiation), so that x^i denotes x to the i-th power. Let the symbol "+" denote addition of words (i.e., modulo-2^32 addition). Let X <<< s denote the 32-bit value obtained by circularly shifting (rotating) X left by s bit positions. Let not(X) denote the bit-wise complement of X, and let X v Y denote the bit-wise OR of X and Y. Let X xor Y denote the bit-wise XOR of X and Y, and let XY denote the bit-wise AND of X and Y. Rivest [Page 2] RFC 1321 MD5 Message-Digest Algorithm April 1992 3. MD5 Algorithm Description We begin by supposing that we have a b-bit message as input, and that we wish to find its message digest. Here b is an arbitrary nonnegative integer; b may be zero, it need not be a multiple of eight, and it may be arbitrarily large. We imagine the bits of the message written down as follows: m_0 m_1 ... m_{b-1} The following five steps are performed to compute the message digest of the message. 3.1 Step 1. Append Padding Bits The message is "padded" (extended) so that its length (in bits) is congruent to 448, modulo 512. That is, the message is extended so that it is just 64 bits shy of being a multiple of 512 bits long. Padding is always performed, even if the length of the message is already congruent to 448, modulo 512. Padding is performed as follows: a single "1" bit is appended to the message, and then "0" bits are appended so that the length in bits of the padded message becomes congruent to 448, modulo 512. In all, at least one bit and at most 512 bits are appended. 3.2 Step 2. Append Length A 64-bit representation of b (the length of the message before the padding bits were added) is appended to the result of the previous step. In the unlikely event that b is greater than 2^64, then only the low-order 64 bits of b are used. (These bits are appended as two 32-bit words and appended low-order word first in accordance with the previous conventions.) At this point the resulting message (after padding with bits and with b) has a length that is an exact multiple of 512 bits. Equivalently, this message has a length that is an exact multiple of 16 (32-bit) words. Let M[0 ... N-1] denote the words of the resulting message, where N is a multiple of 16. 3.3 Step 3. Initialize MD Buffer A four-word buffer (A,B,C,D) is used to compute the message digest. Here each of A, B, C, D is a 32-bit register. These registers are initialized to the following values in hexadecimal, low-order bytes first): Rivest [Page 3] RFC 1321 MD5 Message-Digest Algorithm April 1992 word A: 01 23 45 67 word B: 89 ab cd ef word C: fe dc ba 98 word D: 76 54 32 10 3.4 Step 4. Process Message in 16-Word Blocks We first define four auxiliary functions that each take as input three 32-bit words and produce as output one 32-bit word. F(X,Y,Z) = XY v not(X) Z G(X,Y,Z) = XZ v Y not(Z) H(X,Y,Z) = X xor Y xor Z I(X,Y,Z) = Y xor (X v not(Z)) In each bit position F acts as a conditional: if X then Y else Z. The function F could have been defined using + instead of v since XY and not(X)Z will never have 1's in the same bit position.) It is interesting to note that if the bits of X, Y, and Z are independent and unbiased, the each bit of F(X,Y,Z) will be independent and unbiased. The functions G, H, and I are similar to the function F, in that they act in "bitwise parallel" to produce their output from the bits of X, Y, and Z, in such a manner that if the corresponding bits of X, Y, and Z are independent and unbiased, then each bit of G(X,Y,Z), H(X,Y,Z), and I(X,Y,Z) will be independent and unbiased. Note that the function H is the bit-wise "xor" or "parity" function of its inputs. This step uses a 64-element table T[1 ... 64] constructed from the sine function. Let T[i] denote the i-th element of the table, which is equal to the integer part of 4294967296 times abs(sin(i)), where i is in radians. The elements of the table are given in the appendix. Do the following: /* Process each 16-word block. */ For i = 0 to N/16-1 do /* Copy block i into X. */ For j = 0 to 15 do Set X[j] to M[i*16+j]. end /* of loop on j */ /* Save A as AA, B as BB, C as CC, and D as DD. */ AA = A BB = B Rivest [Page 4] RFC 1321 MD5 Message-Digest Algorithm April 1992 CC = C DD = D /* Round 1. */ /* Let [abcd k s i] denote the operation a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ /* Do the following 16 operations. */ [ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4] [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8] [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12] [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16] /* Round 2. */ /* Let [abcd k s i] denote the operation a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */ /* Do the following 16 operations. */ [ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20] [ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24] [ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28] [ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32] /* Round 3. */ /* Let [abcd k s t] denote the operation a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */ /* Do the following 16 operations. */ [ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36] [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40] [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44] [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48] /* Round 4. */ /* Let [abcd k s t] denote the operation a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */ /* Do the following 16 operations. */ [ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52] [ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56] [ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60] [ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64] /* Then perform the following additions. (That is increment each of the four registers by the value it had before this block was started.) */ A = A + AA B = B + BB C = C + CC D = D + DD end /* of loop on i */ Rivest [Page 5] RFC 1321 MD5 Message-Digest Algorithm April 1992 3.5 Step 5. Output The message digest produced as output is A, B, C, D. That is, we begin with the low-order byte of A, and end with the high-order byte of D. This completes the description of MD5. A reference implementation in C is given in the appendix. 4. Summary The MD5 message-digest algorithm is simple to implement, and provides a "fingerprint" or message digest of a message of arbitrary length. It is conjectured that the difficulty of coming up with two messages having the same message digest is on the order of 2^64 operations, and that the difficulty of coming up with any message having a given message digest is on the order of 2^128 operations. The MD5 algorithm has been carefully scrutinized for weaknesses. It is, however, a relatively new algorithm and further security analysis is of course justified, as is the case with any new proposal of this sort. 5. Differences Between MD4 and MD5 The following are the differences between MD4 and MD5: 1. A fourth round has been added. 2. Each step now has a unique additive constant. 3. The function g in round 2 was changed from (XY v XZ v YZ) to (XZ v Y not(Z)) to make g less symmetric. 4. Each step now adds in the result of the previous step. This promotes a faster "avalanche effect". 5. The order in which input words are accessed in rounds 2 and 3 is changed, to make these patterns less like each other. 6. The shift amounts in each round have been approximately optimized, to yield a faster "avalanche effect." The shifts in different rounds are distinct. Rivest [Page 6] RFC 1321 MD5 Message-Digest Algorithm April 1992 References [1] Rivest, R., "The MD4 Message Digest Algorithm", RFC 1320, MIT and RSA Data Security, Inc., April 1992. [2] Rivest, R., "The MD4 message digest algorithm", in A.J. Menezes and S.A. Vanstone, editors, Advances in Cryptology - CRYPTO '90 Proceedings, pages 303-311, Springer-Verlag, 1991. [3] CCITT Recommendation X.509 (1988), "The Directory - Authentication Framework." APPENDIX A - Reference Implementation This appendix contains the following files taken from RSAREF: A Cryptographic Toolkit for Privacy-Enhanced Mail: global.h -- global header file md5.h -- header file for MD5 md5c.c -- source code for MD5 For more information on RSAREF, send email to <rsaref@rsa.com>. The appendix also includes the following file: mddriver.c -- test driver for MD2, MD4 and MD5 The driver compiles for MD5 by default but can compile for MD2 or MD4 if the symbol MD is defined on the C compiler command line as 2 or 4. The implementation is portable and should work on many different plaforms. However, it is not difficult to optimize the implementation on particular platforms, an exercise left to the reader. For example, on "little-endian" platforms where the lowest-addressed byte in a 32- bit word is the least significant and there are no alignment restrictions, the call to Decode in MD5Transform can be replaced with a typecast. A.1 global.h /* GLOBAL.H - RSAREF types and constants */ /* PROTOTYPES should be set to one if and only if the compiler supports function argument prototyping. The following makes PROTOTYPES default to 0 if it has not already Rivest [Page 7] RFC 1321 MD5 Message-Digest Algorithm April 1992 been defined with C compiler flags. */ #ifndef PROTOTYPES #define PROTOTYPES 0 #endif /* POINTER defines a generic pointer type */ typedef unsigned char *POINTER; /* UINT2 defines a two byte word */ typedef unsigned short int UINT2; /* UINT4 defines a four byte word */ typedef unsigned long int UINT4; /* PROTO_LIST is defined depending on how PROTOTYPES is defined above. If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it returns an empty list. */ #if PROTOTYPES #define PROTO_LIST(list) list #else #define PROTO_LIST(list) () #endif A.2 md5.h /* MD5.H - header file for MD5C.C */ /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. License to copy and use this software is granted provided that it is identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing this software or this function. License is also granted to make and use derivative works provided that such works are identified as "derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" without express or implied warranty of any kind. Rivest [Page 8] RFC 1321 MD5 Message-Digest Algorithm April 1992 These notices must be retained in any copies of any part of this documentation and/or software. */ /* MD5 context. */ typedef struct { UINT4 state[4]; /* state (ABCD) */ UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ unsigned char buffer[64]; /* input buffer */ } MD5_CTX; void MD5Init PROTO_LIST ((MD5_CTX *)); void MD5Update PROTO_LIST ((MD5_CTX *, unsigned char *, unsigned int)); void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *)); A.3 md5c.c /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm */ /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. License to copy and use this software is granted provided that it is identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing this software or this function. License is also granted to make and use derivative works provided that such works are identified as "derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" without express or implied warranty of any kind. These notices must be retained in any copies of any part of this documentation and/or software. */ #include "global.h" #include "md5.h" /* Constants for MD5Transform routine. */ Rivest [Page 9] RFC 1321 MD5 Message-Digest Algorithm April 1992 #define S11 7 #define S12 12 #define S13 17 #define S14 22 #define S21 5 #define S22 9 #define S23 14 #define S24 20 #define S31 4 #define S32 11 #define S33 16 #define S34 23 #define S41 6 #define S42 10 #define S43 15 #define S44 21 static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64])); static void Encode PROTO_LIST ((unsigned char *, UINT4 *, unsigned int)); static void Decode PROTO_LIST ((UINT4 *, unsigned char *, unsigned int)); static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int)); static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int)); static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* F, G, H and I are basic MD5 functions. */ #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~z))) /* ROTATE_LEFT rotates x left n bits. */ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */ #define FF(a, b, c, d, x, s, ac) { \ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ Rivest [Page 10] RFC 1321 MD5 Message-Digest Algorithm April 1992 (a) += (b); \ } #define GG(a, b, c, d, x, s, ac) { \ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define HH(a, b, c, d, x, s, ac) { \ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define II(a, b, c, d, x, s, ac) { \ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } /* MD5 initialization. Begins an MD5 operation, writing a new context. */ void MD5Init (context) MD5_CTX *context; /* context */ { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. */ context->state[0] = 0x67452301; context->state[1] = 0xefcdab89; context->state[2] = 0x98badcfe; context->state[3] = 0x10325476; } /* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */ void MD5Update (context, input, inputLen) MD5_CTX *context; /* context */ unsigned char *input; /* input block */ unsigned int inputLen; /* length of input block */ { unsigned int i, index, partLen; /* Compute number of bytes mod 64 */ index = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((context->count[0] += ((UINT4)inputLen << 3)) Rivest [Page 11] RFC 1321 MD5 Message-Digest Algorithm April 1992 < ((UINT4)inputLen << 3)) context->count[1]++; context->count[1] += ((UINT4)inputLen >> 29); partLen = 64 - index; /* Transform as many times as possible. */ if (inputLen >= partLen) { MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen); MD5Transform (context->state, context->buffer); for (i = partLen; i + 63 < inputLen; i += 64) MD5Transform (context->state, &input[i]); index = 0; } else i = 0; /* Buffer remaining input */ MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i); } /* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */ void MD5Final (digest, context) unsigned char digest[16]; /* message digest */ MD5_CTX *context; /* context */ { unsigned char bits[8]; unsigned int index, padLen; /* Save number of bits */ Encode (bits, context->count, 8); /* Pad out to 56 mod 64. */ index = (unsigned int)((context->count[0] >> 3) & 0x3f); padLen = (index < 56) ? (56 - index) : (120 - index); MD5Update (context, PADDING, padLen); /* Append length (before padding) */ MD5Update (context, bits, 8); Rivest [Page 12] RFC 1321 MD5 Message-Digest Algorithm April 1992 /* Store state in digest */ Encode (digest, context->state, 16); /* Zeroize sensitive information. */ MD5_memset ((POINTER)context, 0, sizeof (*context)); } /* MD5 basic transformation. Transforms state based on block. */ static void MD5Transform (state, block) UINT4 state[4]; unsigned char block[64]; { UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; Decode (x, block, 64); /* Round 1 */ FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ /* Round 2 */ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ Rivest [Page 13] RFC 1321 MD5 Message-Digest Algorithm April 1992 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ /* Round 3 */ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ /* Round 4 */ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; /* Zeroize sensitive information. Rivest [Page 14] RFC 1321 MD5 Message-Digest Algorithm April 1992 */ MD5_memset ((POINTER)x, 0, sizeof (x)); } /* Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4. */ static void Encode (output, input, len) unsigned char *output; UINT4 *input; unsigned int len; { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) { output[j] = (unsigned char)(input[i] & 0xff); output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); } } /* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */ static void Decode (output, input, len) UINT4 *output; unsigned char *input; unsigned int len; { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); } /* Note: Replace "for loop" with standard memcpy if possible. */ static void MD5_memcpy (output, input, len) POINTER output; POINTER input; unsigned int len; { unsigned int i; for (i = 0; i < len; i++) Rivest [Page 15] RFC 1321 MD5 Message-Digest Algorithm April 1992 output[i] = input[i]; } /* Note: Replace "for loop" with standard memset if possible. */ static void MD5_memset (output, value, len) POINTER output; int value; unsigned int len; { unsigned int i; for (i = 0; i < len; i++) ((char *)output)[i] = (char)value; } A.4 mddriver.c /* MDDRIVER.C - test driver for MD2, MD4 and MD5 */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All rights reserved. RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" without express or implied warranty of any kind. These notices must be retained in any copies of any part of this documentation and/or software. */ /* The following makes MD default to MD5 if it has not already been defined with C compiler flags. */ #ifndef MD #define MD MD5 #endif #include <stdio.h> #include <time.h> #include <string.h> #include "global.h" #if MD == 2 #include "md2.h" #endif #if MD == 4 Rivest [Page 16] RFC 1321 MD5 Message-Digest Algorithm April 1992 #include "md4.h" #endif #if MD == 5 #include "md5.h" #endif /* Length of test block, number of test blocks. */ #define TEST_BLOCK_LEN 1000 #define TEST_BLOCK_COUNT 1000 static void MDString PROTO_LIST ((char *)); static void MDTimeTrial PROTO_LIST ((void)); static void MDTestSuite PROTO_LIST ((void)); static void MDFile PROTO_LIST ((char *)); static void MDFilter PROTO_LIST ((void)); static void MDPrint PROTO_LIST ((unsigned char [16])); #if MD == 2 #define MD_CTX MD2_CTX #define MDInit MD2Init #define MDUpdate MD2Update #define MDFinal MD2Final #endif #if MD == 4 #define MD_CTX MD4_CTX #define MDInit MD4Init #define MDUpdate MD4Update #define MDFinal MD4Final #endif #if MD == 5 #define MD_CTX MD5_CTX #define MDInit MD5Init #define MDUpdate MD5Update #define MDFinal MD5Final #endif /* Main driver. Arguments (may be any combination): -sstring - digests string -t - runs time trial -x - runs test script filename - digests file (none) - digests standard input */ int main (argc, argv) int argc; Rivest [Page 17] RFC 1321 MD5 Message-Digest Algorithm April 1992 char *argv[]; { int i; if (argc > 1) for (i = 1; i < argc; i++) if (argv[i][0] == '-' && argv[i][1] == 's') MDString (argv[i] + 2); else if (strcmp (argv[i], "-t") == 0) MDTimeTrial (); else if (strcmp (argv[i], "-x") == 0) MDTestSuite (); else MDFile (argv[i]); else MDFilter (); return (0); } /* Digests a string and prints the result. */ static void MDString (string) char *string; { MD_CTX context; unsigned char digest[16]; unsigned int len = strlen (string); MDInit (&context); MDUpdate (&context, string, len); MDFinal (digest, &context); printf ("MD%d (\"%s\") = ", MD, string); MDPrint (digest); printf ("\n"); } /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. */ static void MDTimeTrial () { MD_CTX context; time_t endTime, startTime; unsigned char block[TEST_BLOCK_LEN], digest[16]; unsigned int i; Rivest [Page 18] RFC 1321 MD5 Message-Digest Algorithm April 1992 printf ("MD%d time trial. Digesting %d %d-byte blocks ...", MD, TEST_BLOCK_LEN, TEST_BLOCK_COUNT); /* Initialize block */ for (i = 0; i < TEST_BLOCK_LEN; i++) block[i] = (unsigned char)(i & 0xff); /* Start timer */ time (&startTime); /* Digest blocks */ MDInit (&context); for (i = 0; i < TEST_BLOCK_COUNT; i++) MDUpdate (&context, block, TEST_BLOCK_LEN); MDFinal (digest, &context); /* Stop timer */ time (&endTime); printf (" done\n"); printf ("Digest = "); MDPrint (digest); printf ("\nTime = %ld seconds\n", (long)(endTime-startTime)); printf ("Speed = %ld bytes/second\n", (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime)); } /* Digests a reference suite of strings and prints the results. */ static void MDTestSuite () { printf ("MD%d test suite:\n", MD); MDString (""); MDString ("a"); MDString ("abc"); MDString ("message digest"); MDString ("abcdefghijklmnopqrstuvwxyz"); MDString ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); MDString ("1234567890123456789012345678901234567890\ 1234567890123456789012345678901234567890"); } /* Digests a file and prints the result. Rivest [Page 19] RFC 1321 MD5 Message-Digest Algorithm April 1992 */ static void MDFile (filename) char *filename; { FILE *file; MD_CTX context; int len; unsigned char buffer[1024], digest[16]; if ((file = fopen (filename, "rb")) == NULL) printf ("%s can't be opened\n", filename); else { MDInit (&context); while (len = fread (buffer, 1, 1024, file)) MDUpdate (&context, buffer, len); MDFinal (digest, &context); fclose (file); printf ("MD%d (%s) = ", MD, filename); MDPrint (digest); printf ("\n"); } } /* Digests the standard input and prints the result. */ static void MDFilter () { MD_CTX context; int len; unsigned char buffer[16], digest[16]; MDInit (&context); while (len = fread (buffer, 1, 16, stdin)) MDUpdate (&context, buffer, len); MDFinal (digest, &context); MDPrint (digest); printf ("\n"); } /* Prints a message digest in hexadecimal. */ static void MDPrint (digest) unsigned char digest[16]; { Rivest [Page 20] RFC 1321 MD5 Message-Digest Algorithm April 1992 unsigned int i; for (i = 0; i < 16; i++) printf ("%02x", digest[i]); } A.5 Test suite The MD5 test suite (driver option "-x") should print the following results: MD5 test suite: MD5 ("") = d41d8cd98f00b204e9800998ecf8427e MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661 MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72 MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0 MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = d174ab98d277d9f5a5611c2c9f419d9f MD5 ("123456789012345678901234567890123456789012345678901234567890123456 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a Security Considerations The level of security discussed in this memo is considered to be sufficient for implementing very high security hybrid digital- signature schemes based on MD5 and a public-key cryptosystem. Author's Address Ronald L. Rivest Massachusetts Institute of Technology Laboratory for Computer Science NE43-324 545 Technology Square Cambridge, MA 02139-1986 Phone: (617) 253-5880 EMail: rivest@theory.lcs.mit.edu Rivest [Page 21]

bilicon 2007-08-03 12:35 鍙戣〃璇勮
]]>
[zt]MD5綆楁硶鍘熺悊 http://www.shnenglu.com/bilicon/articles/29262.htmlbiliconbiliconFri, 03 Aug 2007 04:34:00 GMThttp://www.shnenglu.com/bilicon/articles/29262.htmlhttp://www.shnenglu.com/bilicon/comments/29262.htmlhttp://www.shnenglu.com/bilicon/articles/29262.html#Feedback0http://www.shnenglu.com/bilicon/comments/commentRss/29262.htmlhttp://www.shnenglu.com/bilicon/services/trackbacks/29262.html緇艱堪
    
    md5鐨勫叏縐版槸message-digest algorithm 5錛堜俊鎭?鎽樿綆楁硶錛夛紝鍦?0騫翠唬鍒濈敱mit laboratory for computer science鍜宺sa data security inc鐨剅onald l. rivest寮鍙戝嚭鏉ワ紝緇弇d2銆乵d3鍜宮d4鍙戝睍鑰屾潵銆傚畠鐨勪綔鐢ㄦ槸璁╁ぇ瀹歸噺淇℃伅鍦ㄧ敤鏁板瓧絳懼悕杞歡絳劇講縐佷漢瀵嗗寵鍓嶈"鍘嬬緝"鎴愪竴縐嶄繚瀵嗙殑鏍煎紡錛堝氨鏄妸涓涓換鎰忛暱搴︾殑瀛楄妭涓插彉鎹㈡垚涓瀹氶暱鐨勫ぇ鏁存暟錛夈備笉綆℃槸md2銆乵d4榪樻槸md5錛屽畠浠兘闇瑕佽幏寰椾竴涓殢鏈洪暱搴︾殑淇℃伅騫朵駭鐢熶竴涓?28浣嶇殑淇℃伅鎽樿銆傝櫧鐒惰繖浜涚畻娉曠殑緇撴瀯鎴栧鎴栧皯鏈変簺鐩鎬技錛屼絾md2鐨勮璁′笌md4鍜宮d5瀹屽叏涓嶅悓錛岄偅鏄洜涓簃d2鏄負8浣嶆満鍣ㄥ仛榪囪璁′紭鍖栫殑錛岃宮d4鍜宮d5鍗存槸闈㈠悜32浣嶇殑鐢佃剳銆傝繖涓変釜綆楁硶鐨勬弿榪板拰c璇█婧愪唬鐮佸湪internet rfcs 1321涓湁璇︾粏鐨勬弿榪幫紙http://www.ietf.org/rfc/rfc1321.txt錛夛紝榪欐槸涓浠芥渶鏉冨▉鐨勬枃妗o紝鐢眗onald l. rivest鍦?992騫?鏈堝悜ieft鎻愪氦銆?
    
    rivest鍦?989騫村紑鍙戝嚭md2綆楁硶銆傚湪榪欎釜綆楁硶涓紝棣栧厛瀵逛俊鎭繘琛屾暟鎹ˉ浣嶏紝浣夸俊鎭殑瀛楄妭闀垮害鏄?6鐨勫嶆暟銆傜劧鍚庯紝浠ヤ竴涓?6浣嶇殑媯楠屽拰榪藉姞鍒頒俊鎭湯灝俱傚茍涓旀牴鎹繖涓柊浜х敓鐨勪俊鎭綆楀嚭鏁e垪鍊箋傚悗鏉ワ紝rogier鍜宑hauvaud鍙戠幇濡傛灉蹇界暐浜嗘楠屽拰灝嗕駭鐢焟d2鍐茬獊銆俶d2綆楁硶鐨勫姞瀵嗗悗緇撴灉鏄敮涓鐨?-鏃㈡病鏈夐噸澶嶃?
    
    涓轟簡鍔犲己綆楁硶鐨勫畨鍏ㄦэ紝rivest鍦?990騫村張寮鍙戝嚭md4綆楁硶銆俶d4綆楁硶鍚屾牱闇瑕佸~琛ヤ俊鎭互紜繚淇℃伅鐨勫瓧鑺傞暱搴﹀姞涓?48鍚庤兘琚?12鏁撮櫎錛堜俊鎭瓧鑺傞暱搴od 512 = 448錛夈傜劧鍚庯紝涓涓互64浣嶄簩榪涘埗琛ㄧず鐨勪俊鎭殑鏈鍒濋暱搴﹁娣誨姞榪涙潵銆備俊鎭澶勭悊鎴?12浣峝amg?rd/merkle榪唬緇撴瀯鐨勫尯鍧楋紝鑰屼笖姣忎釜鍖哄潡瑕侀氳繃涓変釜涓嶅悓姝ラ鐨勫鐞嗐俤en boer鍜宐osselaers浠ュ強鍏朵粬浜哄緢蹇殑鍙戠幇浜嗘敾鍑籱d4鐗堟湰涓涓姝ュ拰絎笁姝ョ殑婕忔礊銆俤obbertin鍚戝ぇ瀹舵紨紺轟簡濡備綍鍒╃敤涓閮ㄦ櫘閫氱殑涓漢鐢佃剳鍦ㄥ嚑鍒嗛挓鍐呮壘鍒癿d4瀹屾暣鐗堟湰涓殑鍐茬獊錛堣繖涓啿紿佸疄闄呬笂鏄竴縐嶆紡媧烇紝瀹冨皢瀵艱嚧瀵逛笉鍚岀殑鍐呭榪涜鍔犲瘑鍗村彲鑳藉緱鍒扮浉鍚岀殑鍔犲瘑鍚庣粨鏋滐級銆傛鏃犵枒闂紝md4灝辨琚窐姹版帀浜嗐?
    
    灝界md4綆楁硶鍦ㄥ畨鍏ㄤ笂鏈変釜榪欎箞澶х殑婕忔礊錛屼絾瀹冨鍦ㄥ叾鍚庢墠琚紑鍙戝嚭鏉ョ殑濂藉嚑縐嶄俊鎭畨鍏ㄥ姞瀵嗙畻娉曠殑鍑虹幇鍗存湁鐫涓嶅彲蹇借鐨勫紩瀵間綔鐢ㄣ傞櫎浜唌d5浠ュ錛屽叾涓瘮杈冩湁鍚嶇殑榪樻湁sha-1銆乺ipe-md浠ュ強haval絳夈?
    
    涓騫翠互鍚庯紝鍗?991騫達紝rivest寮鍙戝嚭鎶鏈笂鏇翠負瓚嬭繎鎴愮啛鐨刴d5綆楁硶銆傚畠鍦╩d4鐨勫熀紜涓婂鍔犱簡"瀹夊叏-甯﹀瓙"錛坰afety-belts錛夌殑姒傚康銆傝櫧鐒秏d5姣攎d4紼嶅井鎱竴浜涳紝浣嗗嵈鏇翠負瀹夊叏銆傝繖涓畻娉曞緢鏄庢樉鐨勭敱鍥涗釜鍜宮d4璁捐鏈夊皯璁鎬笉鍚岀殑姝ラ緇勬垚銆傚湪md5綆楁硶涓紝淇℃伅-鎽樿鐨勫ぇ灝忓拰濉厖鐨勫繀瑕佹潯浠朵笌md4瀹屽叏鐩稿悓銆俤en boer鍜宐osselaers鏇懼彂鐜癿d5綆楁硶涓殑鍋囧啿紿侊紙pseudo-collisions錛夛紝浣嗛櫎姝や箣澶栧氨娌℃湁鍏朵粬琚彂鐜扮殑鍔犲瘑鍚庣粨鏋滀簡銆?
    
    van oorschot鍜寃iener鏇劇粡鑰冭檻榪囦竴涓湪鏁e垪涓毚鍔涙悳瀵誨啿紿佺殑鍑芥暟錛坆rute-force hash function錛夛紝鑰屼笖浠栦滑鐚滄祴涓涓璁捐涓撻棬鐢ㄦ潵鎼滅儲md5鍐茬獊鐨勬満鍣紙榪欏彴鏈哄櫒鍦?994騫寸殑鍒墮犳垚鏈ぇ綰︽槸涓鐧句竾緹庡厓錛夊彲浠ュ鉤鍧囨瘡24澶╁氨鎵懼埌涓涓啿紿併備絾鍗曚粠1991騫村埌2001騫磋繖10騫撮棿錛岀珶娌℃湁鍑虹幇鏇夸唬md5綆楁硶鐨刴d6鎴栬鍙仛鍏朵粬浠涔堝悕瀛楃殑鏂扮畻娉曡繖涓鐐癸紝鎴戜滑灝卞彲浠ョ湅鍑鴻繖涓憰鐤靛茍娌℃湁澶鐨勫獎鍝峬d5鐨勫畨鍏ㄦс備笂闈㈡墍鏈夎繖浜涢兘涓嶈凍浠ユ垚涓簃d5鐨勫湪瀹為檯搴旂敤涓殑闂銆傚茍涓旓紝鐢變簬md5綆楁硶鐨勪嬌鐢ㄤ笉闇瑕佹敮浠樹換浣曠増鏉冭垂鐢ㄧ殑錛屾墍浠ュ湪涓鑸殑鎯呭喌涓嬶紙闈炵粷瀵嗗簲鐢ㄩ鍩熴備絾鍗充究鏄簲鐢ㄥ湪緇濆瘑棰嗗煙鍐咃紝md5涔熶笉澶變負涓縐嶉潪甯鎬紭縐鐨勪腑闂存妧鏈級錛宮d5鎬庝箞閮藉簲璇ョ畻寰椾笂鏄潪甯稿畨鍏ㄧ殑浜嗐?nbsp;
    
綆楁硶鐨勫簲鐢?/strong>
    
    md5鐨勫吀鍨嬪簲鐢ㄦ槸瀵逛竴孌典俊鎭紙message錛変駭鐢熶俊鎭憳瑕侊紙message-digest錛夛紝浠ラ槻姝㈣綃℃敼銆傛瘮濡傦紝鍦╱nix涓嬫湁寰堝杞歡鍦ㄤ笅杞界殑鏃跺欓兘鏈変竴涓枃浠跺悕鐩稿悓錛屾枃浠舵墿灞曞悕涓?md5鐨勬枃浠訛紝鍦ㄨ繖涓枃浠朵腑閫氬父鍙湁涓琛屾枃鏈紝澶ц嚧緇撴瀯濡傦細
    
    md5 (tanajiya.tar.gz) = 0ca175b9c0f726a831d895e269332461
    
    榪欏氨鏄痶anajiya.tar.gz鏂囦歡鐨勬暟瀛楃鍚嶃俶d5灝嗘暣涓枃浠跺綋浣滀竴涓ぇ鏂囨湰淇℃伅錛岄氳繃鍏朵笉鍙嗙殑瀛楃涓插彉鎹㈢畻娉曪紝浜х敓浜嗚繖涓敮涓鐨刴d5淇℃伅鎽樿銆傚鏋滃湪浠ュ悗浼犳挱榪欎釜鏂囦歡鐨勮繃紼嬩腑錛屾棤璁烘枃浠剁殑鍐呭鍙戠敓浜嗕換浣曞艦寮忕殑鏀瑰彉錛堝寘鎷漢涓轟慨鏀規垨鑰呬笅杞借繃紼嬩腑綰胯礬涓嶇ǔ瀹氬紩璧風殑浼犺緭閿欒絳夛級錛屽彧瑕佷綘瀵硅繖涓枃浠墮噸鏂拌綆梞d5鏃跺氨浼氬彂鐜頒俊鎭憳瑕佷笉鐩稿悓錛岀敱姝ゅ彲浠ョ‘瀹氫綘寰楀埌鐨勫彧鏄竴涓笉姝g‘鐨勬枃浠躲傚鏋滃啀鏈変竴涓涓夋柟鐨勮璇佹満鏋勶紝鐢╩d5榪樺彲浠ラ槻姝㈡枃浠朵綔鑰呯殑"鎶佃禆"錛岃繖灝辨槸鎵璋撶殑鏁板瓧絳懼悕搴旂敤銆?
    
    md5榪樺箍娉涚敤浜庡姞瀵嗗拰瑙e瘑鎶鏈笂銆傛瘮濡傚湪unix緋葷粺涓敤鎴風殑瀵嗙爜灝辨槸浠d5錛堟垨鍏跺畠綾諱技鐨勭畻娉曪級緇忓姞瀵嗗悗瀛樺偍鍦ㄦ枃浠剁郴緇熶腑銆傚綋鐢ㄦ埛鐧誨綍鐨勬椂鍊欙紝緋葷粺鎶婄敤鎴瘋緭鍏ョ殑瀵嗙爜璁$畻鎴恗d5鍊鹼紝鐒跺悗鍐嶅幓鍜屼繚瀛樺湪鏂囦歡緋葷粺涓殑md5鍊艱繘琛屾瘮杈冿紝榪涜岀‘瀹氳緭鍏ョ殑瀵嗙爜鏄惁姝g‘銆傞氳繃榪欐牱鐨勬楠わ紝緋葷粺鍦ㄥ茍涓嶇煡閬撶敤鎴峰瘑鐮佺殑鏄庣爜鐨勬儏鍐典笅灝卞彲浠ョ‘瀹氱敤鎴風櫥褰曠郴緇熺殑鍚堟硶鎬с傝繖涓嶄絾鍙互閬垮厤鐢ㄦ埛鐨勫瘑鐮佽鍏鋒湁緋葷粺綆$悊鍛樻潈闄愮殑鐢ㄦ埛鐭ラ亾錛岃屼笖榪樺湪涓瀹氱▼搴︿笂澧炲姞浜嗗瘑鐮佽鐮磋В鐨勯毦搴︺?
    
    姝f槸鍥犱負榪欎釜鍘熷洜錛岀幇鍦ㄨ榛戝浣跨敤鏈澶氱殑涓縐嶇牬璇戝瘑鐮佺殑鏂規硶灝辨槸涓縐嶈縐頒負"璺戝瓧鍏?鐨勬柟娉曘傛湁涓ょ鏂規硶寰楀埌瀛楀吀錛屼竴縐嶆槸鏃ュ父鎼滈泦鐨勭敤鍋氬瘑鐮佺殑瀛楃涓茶〃錛屽彟涓縐嶆槸鐢ㄦ帓鍒楃粍鍚堟柟娉曠敓鎴愮殑錛屽厛鐢╩d5紼嬪簭璁$畻鍑鴻繖浜涘瓧鍏擱」鐨刴d5鍊鹼紝鐒跺悗鍐嶇敤鐩爣鐨刴d5鍊煎湪榪欎釜瀛楀吀涓绱€傛垜浠亣璁懼瘑鐮佺殑鏈澶ч暱搴︿負8浣嶅瓧鑺傦紙8 bytes錛夛紝鍚屾椂瀵嗙爜鍙兘鏄瓧姣嶅拰鏁板瓧錛屽叡26+26+10=62涓瓧絎︼紝鎺掑垪緇勫悎鍑虹殑瀛楀吀鐨勯」鏁板垯鏄痯(62,1)+p(62,2)….+p(62,8)錛岄偅涔熷凡緇忔槸涓涓緢澶╂枃鐨勬暟瀛椾簡錛屽瓨鍌ㄨ繖涓瓧鍏稿氨闇瑕乼b綰х殑紓佺洏闃靛垪錛岃屼笖榪欑鏂規硶榪樻湁涓涓墠鎻愶紝灝辨槸鑳借幏寰楃洰鏍囪處鎴風殑瀵嗙爜md5鍊肩殑鎯呭喌涓嬫墠鍙互銆傝繖縐嶅姞瀵嗘妧鏈騫挎硾鐨勫簲鐢ㄤ簬unix緋葷粺涓紝榪欎篃鏄負浠涔坲nix緋葷粺姣斾竴鑸搷浣滅郴緇熸洿涓哄潥鍥轟竴涓噸瑕佸師鍥犮?nbsp;
    
綆楁硶鎻忚堪
    
    瀵筸d5綆楁硶綆瑕佺殑鍙欒堪鍙互涓猴細md5浠?12浣嶅垎緇勬潵澶勭悊杈撳叆鐨勪俊鎭紝涓旀瘡涓鍒嗙粍鍙堣鍒掑垎涓?6涓?2浣嶅瓙鍒嗙粍錛岀粡榪囦簡涓緋誨垪鐨勫鐞嗗悗錛岀畻娉曠殑杈撳嚭鐢卞洓涓?2浣嶅垎緇勭粍鎴愶紝灝嗚繖鍥涗釜32浣嶅垎緇勭駭鑱斿悗灝嗙敓鎴愪竴涓?28浣嶆暎鍒楀箋?
    
    鍦╩d5綆楁硶涓紝棣栧厛闇瑕佸淇℃伅榪涜濉厖錛屼嬌鍏跺瓧鑺傞暱搴﹀512姹備綑鐨勭粨鏋滅瓑浜?48銆傚洜姝わ紝淇℃伅鐨勫瓧鑺傞暱搴︼紙bits length錛夊皢琚墿灞曡嚦n*512+448錛屽嵆n*64+56涓瓧鑺傦紙bytes錛夛紝n涓轟竴涓鏁存暟銆傚~鍏呯殑鏂規硶濡備笅錛屽湪淇℃伅鐨勫悗闈㈠~鍏呬竴涓?鍜屾棤鏁頒釜0錛岀洿鍒版弧瓚充笂闈㈢殑鏉′歡鏃舵墠鍋滄鐢?瀵逛俊鎭殑濉厖銆傜劧鍚庯紝鍦ㄥ湪榪欎釜緇撴灉鍚庨潰闄勫姞涓涓互64浣嶄簩榪涘埗琛ㄧず鐨勫~鍏呭墠淇℃伅闀垮害銆傜粡榪囪繖涓ゆ鐨勫鐞嗭紝鐜板湪鐨勪俊鎭瓧鑺傞暱搴?n*512+448+64=(n+1)*512錛屽嵆闀垮害鎭板ソ鏄?12鐨勬暣鏁板嶃傝繖鏍峰仛鐨勫師鍥犳槸涓烘弧瓚沖悗闈㈠鐞嗕腑瀵逛俊鎭暱搴︾殑瑕佹眰銆?
    
    md5涓湁鍥涗釜32浣嶈縐頒綔閾炬帴鍙橀噺錛坈haining variable錛夌殑鏁存暟鍙傛暟錛屼粬浠垎鍒負錛歛=0x01234567錛宐=0x89abcdef錛宑=0xfedcba98錛宒=0x76543210銆?
    
    褰撹緗ソ榪欏洓涓摼鎺ュ彉閲忓悗錛屽氨寮濮嬭繘鍏ョ畻娉曠殑鍥涜疆寰幆榪愮畻銆傚驚鐜殑嬈℃暟鏄俊鎭腑512浣嶄俊鎭垎緇勭殑鏁扮洰銆?
    
    灝嗕笂闈㈠洓涓摼鎺ュ彉閲忓鍒跺埌鍙﹀鍥涗釜鍙橀噺涓細a鍒癮錛宐鍒癰錛宑鍒癱錛宒鍒癲銆?
    
    涓誨驚鐜湁鍥涜疆錛坢d4鍙湁涓夎疆錛夛紝姣忚疆寰幆閮藉緢鐩鎬技銆傜涓杞繘琛?6嬈℃搷浣溿傛瘡嬈℃搷浣滃a銆乥銆乧鍜宒涓殑鍏朵腑涓変釜浣滀竴嬈¢潪綰挎у嚱鏁拌繍綆楋紝鐒跺悗灝嗘墍寰楃粨鏋滃姞涓婄鍥涗釜鍙橀噺錛屾枃鏈殑涓涓瓙鍒嗙粍鍜屼竴涓父鏁般傚啀灝嗘墍寰楃粨鏋滃悜鍙崇幆縐諱竴涓笉瀹氱殑鏁幫紝騫跺姞涓奱銆乥銆乧鎴杁涓箣涓銆傛渶鍚庣敤璇ョ粨鏋滃彇浠銆乥銆乧鎴杁涓箣涓銆?
    浠ヤ竴涓嬫槸姣忔鎿嶄綔涓敤鍒扮殑鍥涗釜闈炵嚎鎬у嚱鏁幫紙姣忚疆涓涓級銆?
    
    f(x,y,z) =(x&y)|((~x)&z)
    g(x,y,z) =(x&z)|(y&(~z))
    h(x,y,z) =x^y^z
    i(x,y,z)=y^(x|(~z))
    錛?amp;鏄笌錛寍鏄垨錛寏鏄潪錛宆鏄紓鎴栵級
    
    榪欏洓涓嚱鏁扮殑璇存槑錛氬鏋渪銆亂鍜寊鐨勫搴斾綅鏄嫭绔嬪拰鍧囧寑鐨勶紝閭d箞緇撴灉鐨勬瘡涓浣嶄篃搴旀槸鐙珛鍜屽潎鍖鐨勩?
    f鏄竴涓愪綅榪愮畻鐨勫嚱鏁般傚嵆錛屽鏋渪錛岄偅涔坹錛屽惁鍒檢銆傚嚱鏁癶鏄愪綅濂囧伓鎿嶄綔絎︺?
    
    鍋囪mj琛ㄧず娑堟伅鐨勭j涓瓙鍒嗙粍錛堜粠0鍒?5錛夛紝<<
    ff(a,b,c,d,mj,s,ti)琛ㄧずa=b+((a+(f(b,c,d)+mj+ti)<< gg(a,b,c,d,mj,s,ti)琛ㄧずa=b+((a+(g(b,c,d)+mj+ti)<< hh(a,b,c,d,mj,s,ti)琛ㄧずa=b+((a+(h(b,c,d)+mj+ti)<< ii(a,b,c,d,mj,s,ti)琛ㄧずa=b+((a+(i(b,c,d)+mj+ti)<<
    榪欏洓杞紙64姝ワ級鏄細
    
    絎竴杞?
    
    ff(a,b,c,d,m0,7,0xd76aa478)
    ff(d,a,b,c,m1,12,0xe8c7b756)
    ff(c,d,a,b,m2,17,0x242070db)
    ff(b,c,d,a,m3,22,0xc1bdceee)
    ff(a,b,c,d,m4,7,0xf57c0faf)
    ff(d,a,b,c,m5,12,0x4787c62a)
    ff(c,d,a,b,m6,17,0xa8304613)
    ff(b,c,d,a,m7,22,0xfd469501)
    ff(a,b,c,d,m8,7,0x698098d8)
    ff(d,a,b,c,m9,12,0x8b44f7af)
    ff(c,d,a,b,m10,17,0xffff5bb1)
    ff(b,c,d,a,m11,22,0x895cd7be)
    ff(a,b,c,d,m12,7,0x6b901122)
    ff(d,a,b,c,m13,12,0xfd987193)
    ff(c,d,a,b,m14,17,0xa679438e)
    ff(b,c,d,a,m15,22,0x49b40821)
    
    絎簩杞?
    
    gg(a,b,c,d,m1,5,0xf61e2562)
    gg(d,a,b,c,m6,9,0xc040b340)
    gg(c,d,a,b,m11,14,0x265e5a51)
    gg(b,c,d,a,m0,20,0xe9b6c7aa)
    gg(a,b,c,d,m5,5,0xd62f105d)
    gg(d,a,b,c,m10,9,0x02441453)
    gg(c,d,a,b,m15,14,0xd8a1e681)
    gg(b,c,d,a,m4,20,0xe7d3fbc8)
    gg(a,b,c,d,m9,5,0x21e1cde6)
    gg(d,a,b,c,m14,9,0xc33707d6)
    gg(c,d,a,b,m3,14,0xf4d50d87)
    gg(b,c,d,a,m8,20,0x455a14ed)
    gg(a,b,c,d,m13,5,0xa9e3e905)
    gg(d,a,b,c,m2,9,0xfcefa3f8)
    gg(c,d,a,b,m7,14,0x676f02d9)
    gg(b,c,d,a,m12,20,0x8d2a4c8a)
    
    絎笁杞?
    
    hh(a,b,c,d,m5,4,0xfffa3942)
    hh(d,a,b,c,m8,11,0x8771f681)
    hh(c,d,a,b,m11,16,0x6d9d6122)
    hh(b,c,d,a,m14,23,0xfde5380c)
    hh(a,b,c,d,m1,4,0xa4beea44)
    hh(d,a,b,c,m4,11,0x4bdecfa9)
    hh(c,d,a,b,m7,16,0xf6bb4b60)
    hh(b,c,d,a,m10,23,0xbebfbc70)
    hh(a,b,c,d,m13,4,0x289b7ec6)
    hh(d,a,b,c,m0,11,0xeaa127fa)
    hh(c,d,a,b,m3,16,0xd4ef3085)
    hh(b,c,d,a,m6,23,0x04881d05)
    hh(a,b,c,d,m9,4,0xd9d4d039)
    hh(d,a,b,c,m12,11,0xe6db99e5)
    hh(c,d,a,b,m15,16,0x1fa27cf8)
    hh(b,c,d,a,m2,23,0xc4ac5665)
    
    絎洓杞?
    
    ii(a,b,c,d,m0,6,0xf4292244)
    ii(d,a,b,c,m7,10,0x432aff97)
    ii(c,d,a,b,m14,15,0xab9423a7)
    ii(b,c,d,a,m5,21,0xfc93a039)
    ii(a,b,c,d,m12,6,0x655b59c3)
    ii(d,a,b,c,m3,10,0x8f0ccc92)
    ii(c,d,a,b,m10,15,0xffeff47d)
    ii(b,c,d,a,m1,21,0x85845dd1)
    ii(a,b,c,d,m8,6,0x6fa87e4f)
    ii(d,a,b,c,m15,10,0xfe2ce6e0)
    ii(c,d,a,b,m6,15,0xa3014314)
    ii(b,c,d,a,m13,21,0x4e0811a1)
    ii(a,b,c,d,m4,6,0xf7537e82)
    ii(d,a,b,c,m11,10,0xbd3af235)
    ii(c,d,a,b,m2,15,0x2ad7d2bb)
    ii(b,c,d,a,m9,21,0xeb86d391)
    
    甯告暟ti鍙互濡備笅閫夋嫨錛?
    
    鍦ㄧi姝ヤ腑錛宼i鏄?294967296*abs(sin(i))鐨勬暣鏁伴儴鍒嗭紝i鐨勫崟浣嶆槸寮у害銆?4294967296絳変簬2鐨?2嬈℃柟)
    鎵鏈夎繖浜涘畬鎴愪箣鍚庯紝灝哸銆乥銆乧銆乨鍒嗗埆鍔犱笂a銆乥銆乧銆乨銆傜劧鍚庣敤涓嬩竴鍒嗙粍鏁版嵁緇х畫榪愯綆楁硶錛屾渶鍚庣殑杈撳嚭鏄痑銆乥銆乧鍜宒鐨勭駭鑱斻?
    
    褰撲綘鎸夌収鎴戜笂闈㈡墍璇寸殑鏂規硶瀹炵幇md5綆楁硶浠ュ悗錛屼綘鍙互鐢ㄤ互涓嬪嚑涓俊鎭浣犲仛鍑烘潵鐨勭▼搴忎綔涓涓畝鍗曠殑嫻嬭瘯錛岀湅鐪嬬▼搴忔湁娌℃湁閿欒銆?
    
    md5 ("") = d41d8cd98f00b204e9800998ecf8427e
    md5 ("a") = 0cc175b9c0f1b6a831c399e269772661
    md5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
    md5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
    md5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
    md5 ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789") =
    d174ab98d277d9f5a5611c2c9f419d9f
    md5 ("123456789012345678901234567890123456789012345678901234567890123456789
    01234567890") = 57edf4a22be3c955ac49da2e2107b67a
    
    濡傛灉浣犵敤涓婇潰鐨勪俊鎭垎鍒浣犲仛鐨刴d5綆楁硶瀹炰緥鍋氭祴璇曪紝鏈鍚庡緱鍑虹殑緇撹鍜屾爣鍑嗙瓟妗堝畬鍏ㄤ竴鏍鳳紝閭f垜灝辮鍦ㄨ繖閲岃薄浣犻亾涓澹扮璐轟簡銆傝鐭ラ亾錛屾垜鐨勭▼搴忓湪絎竴嬈$紪璇戞垚鍔熺殑鏃跺欐槸娌℃湁寰楀嚭鍜屼笂闈㈢浉鍚岀殑緇撴灉鐨勩?nbsp;
    
    
md5鐨勫畨鍏ㄦ?/strong>
    
    md5鐩稿md4鎵浣滅殑鏀硅繘錛?nbsp;    
    1. 澧炲姞浜嗙鍥涜疆錛?nbsp;    
    2. 姣忎竴姝ュ潎鏈夊敮涓鐨勫姞娉曞父鏁幫紱    
    3. 涓哄噺寮辯浜岃疆涓嚱鏁癵鐨勫縐版т粠(x&y)|(x&z)|(y&z)鍙樹負(x&z)|(y&(~z))錛?nbsp;   
    4. 絎竴姝ュ姞涓婁簡涓婁竴姝ョ殑緇撴灉錛岃繖灝嗗紩璧鋒洿蹇殑闆穿鏁堝簲錛?nbsp;    
    5. 鏀瑰彉浜嗙浜岃疆鍜岀涓夎疆涓闂秷鎭瓙鍒嗙粍鐨勬搴忥紝浣垮叾鏇翠笉鐩鎬技錛?nbsp;    
    6. 榪戜技浼樺寲浜嗘瘡涓杞腑鐨勫驚鐜乏縐諱綅縐婚噺浠ュ疄鐜版洿蹇殑闆穿鏁堝簲銆傚悇杞殑浣嶇Щ閲忎簰涓嶇浉鍚屻?nbsp;


bilicon 2007-08-03 12:34 鍙戣〃璇勮
]]>
[杞琞浣跨敤CPU鏃墮棿鎴寵繘琛岄珮綺懼害璁℃椂http://www.shnenglu.com/bilicon/articles/28807.htmlbiliconbiliconThu, 26 Jul 2007 02:57:00 GMThttp://www.shnenglu.com/bilicon/articles/28807.htmlhttp://www.shnenglu.com/bilicon/comments/28807.htmlhttp://www.shnenglu.com/bilicon/articles/28807.html#Feedback0http://www.shnenglu.com/bilicon/comments/commentRss/28807.htmlhttp://www.shnenglu.com/bilicon/services/trackbacks/28807.html

瀵瑰叧娉ㄦц兘鐨勭▼搴忓紑鍙戜漢鍛樿岃█錛屼竴涓ソ鐨勮鏃墮儴浠舵棦鏄泭鍙嬶紝涔熸槸鑹笀銆傝鏃跺櫒鏃㈠彲浠ヤ綔涓虹▼搴忕粍浠跺府鍔╃▼搴忓憳綺劇‘鐨勬帶鍒剁▼搴忚繘紼嬶紝鍙堟槸涓浠舵湁鍔涚殑璋冭瘯姝﹀櫒錛屽湪鏈夌粡楠岀殑紼嬪簭鍛樻墜閲屽彲浠ュ敖蹇殑紜畾紼嬪簭鐨勬ц兘鐡墮錛屾垨鑰呭涓嶅悓鐨勭畻娉曚綔鍑烘湁璇存湇鍔涚殑鎬ц兘姣旇緝銆?/span>
鍦?/span>Windows騫沖彴涓嬶紝甯哥敤鐨勮鏃跺櫒鏈変袱縐嶏紝涓縐嶆槸timeGetTime澶氬獟浣撹鏃跺櫒錛屽畠鍙互鎻愪緵姣綰х殑璁℃椂銆備絾榪欎釜綺懼害瀵瑰緢澶氬簲鐢ㄥ満鍚堣岃█榪樻槸澶矖緋欎簡銆傚彟涓縐嶆槸QueryPerformanceCount璁℃暟鍣紝闅忕郴緇熺殑涓嶅悓鍙互鎻愪緵寰綰х殑璁℃暟銆傚浜庡疄鏃跺浘褰㈠鐞嗐佸濯掍綋鏁版嵁嫻佸鐞嗐佹垨鑰呭疄鏃剁郴緇熸瀯閫犵殑紼嬪簭鍛橈紝鍠勭敤QueryPerformanceCount/QueryPerformanceFrequency鏄竴欏瑰熀鏈姛銆?/span>
鏈枃瑕佷粙緇嶇殑錛屾槸鍙︿竴縐嶇洿鎺ュ埄鐢?/span>Pentium CPU鍐呴儴鏃墮棿鎴寵繘琛岃鏃剁殑楂樼簿搴﹁鏃舵墜孌點備互涓嬭璁轟富瑕佸緱鐩婁簬銆?/span>Windows鍥懼艦緙栫▼銆嬩竴涔︼紝絎?/span>15欏碉紞17欏碉紝鏈夊叴瓚g殑璇昏呭彲浠ョ洿鎺ュ弬鑰冭涔︺傚叧浜?/span>RDTSC鎸囦護鐨勮緇嗚璁猴紝鍙互鍙傝?/span>Intel浜у搧鎵嬪唽銆傛湰鏂囦粎浠呬綔鎶涚爾涔嬬敤銆?/span>

鍦?/span>Intel Pentium浠ヤ笂綰у埆鐨?/span>CPU涓紝鏈変竴涓О涓?/span>鏃墮棿鎴籌紙Time Stamp錛?/span>鐨勯儴浠訛紝瀹冧互64浣嶆棤絎﹀彿鏁村瀷鏁扮殑鏍煎紡錛岃褰曚簡鑷?/span>CPU涓婄數浠ユ潵鎵緇忚繃鐨勬椂閽熷懆鏈熸暟銆傜敱浜庣洰鍓嶇殑CPU涓婚閮介潪甯擱珮錛屽洜姝よ繖涓儴浠跺彲浠ヨ揪鍒扮撼縐掔駭鐨勮鏃剁簿搴︺傝繖涓簿紜ф槸涓婅堪涓ょ鏂規硶鎵鏃犳硶姣旀嫙鐨勩?/span>
鍦?/span>Pentium浠ヤ笂鐨?/span>CPU涓紝鎻愪緵浜嗕竴鏉℃満鍣ㄦ寚浠?/span>RDTSC錛?/span>Read Time Stamp Counter錛夋潵璇誨彇榪欎釜鏃墮棿鎴崇殑鏁板瓧錛屽茍灝嗗叾淇濆瓨鍦?/span>EDX:EAX瀵勫瓨鍣ㄥ涓傜敱浜?/span>EDX:EAX瀵勫瓨鍣ㄥ鎭板ソ鏄?/span>Win32騫沖彴涓?/span>C++璇█淇濆瓨鍑芥暟榪斿洖鍊肩殑瀵勫瓨鍣紝鎵浠ユ垜浠彲浠ユ妸榪欐潯鎸囦護鐪嬫垚鏄竴涓櫘閫氱殑鍑芥暟璋冪敤銆傚儚榪欐牱錛?/span>

inline unsigned __int64 GetCycleCount()
{
__asm RDTSC
}

浣嗘槸涓嶈錛屽洜涓?/span>RDTSC涓嶈C++鐨勫唴宓屾眹緙栧櫒鐩存帴鏀寔錛屾墍浠ユ垜浠鐢?/span>_emit浼寚浠ょ洿鎺ュ祵鍏ヨ鎸囦護鐨勬満鍣ㄧ爜褰㈠紡0X0F銆?/span>0X31錛屽涓嬶細

inline unsigned __int64 GetCycleCount()
{
__asm _emit 0x0F
__asm _emit 0x31
}

浠ュ悗鍦ㄩ渶瑕佽鏁板櫒鐨勫満鍚堬紝鍙互鍍忎嬌鐢ㄦ櫘閫氱殑Win32 API涓鏍鳳紝璋冪敤涓ゆGetCycleCount鍑芥暟錛屾瘮杈冧袱涓繑鍥炲肩殑宸紝鍍忚繖鏍鳳細

unsigned long t;
t = (unsigned long)GetCycleCount();
//Do Something time-intensive ...
t -= (unsigned long)GetCycleCount();

銆?/span>Windows鍥懼艦緙栫▼銆嬬15欏電紪鍐欎簡涓涓被錛屾妸榪欎釜璁℃暟鍣ㄥ皝瑁呰搗鏉ャ傛湁鍏磋叮鐨勮鑰呭彲浠ュ幓鍙傝冮偅涓被鐨勪唬鐮併備綔鑰呬負浜嗘洿綺劇‘鐨勫畾鏃訛紝鍋氫簡涓鐐瑰皬灝忕殑鏀硅繘錛屾妸鎵цRDTSC鎸囦護鐨勬椂闂達紝閫氳繃榪炵畫涓ゆ璋冪敤GetCycleCount鍑芥暟璁$畻鍑烘潵騫朵繚瀛樹簡璧鋒潵錛屼互鍚庢瘡嬈¤鏃剁粨鏉熷悗錛岄兘浠庡疄闄呭緱鍒扮殑璁℃暟涓噺鎺夎繖涓灝忔鏃墮棿錛屼互寰楀埌鏇村噯紜殑璁℃椂鏁板瓧銆備絾鎴戜釜浜鴻寰楄繖涓鐐圭偣鏀硅繘鎰忎箟涓嶅ぇ銆傚湪鎴戠殑鏈哄櫒涓婂疄嫻嬶紝榪欐潯鎸囦護澶ф鑺辨帀浜嗗嚑鍗佸埌100澶氫釜鍛ㄦ湡錛屽湪Celeron 800MHz鐨勬満鍣ㄤ笂錛岃繖涓嶈繃鏄崄鍒嗕箣涓寰鐨勬椂闂淬傚澶у鏁板簲鐢ㄦ潵璇達紝榪欑偣鏃墮棿瀹屽叏鍙互蹇界暐涓嶈錛涜屽閭d簺紜疄瑕佺簿紜埌綰崇鏁伴噺綰х殑搴旂敤鏉ヨ錛岃繖涓ˉ鍋夸篃榪囦簬綺楃硻浜嗐?/span>

榪欎釜鏂規硶鐨勪紭鐐規槸錛?/span>
1.
楂樼簿搴︺傚彲浠ョ洿鎺ヨ揪鍒扮撼縐掔駭鐨勮鏃剁簿搴︼紙鍦?/span>1GHz鐨?/span>CPU涓婃瘡涓椂閽熷懆鏈熷氨鏄竴綰崇錛夛紝榪欐槸鍏朵粬璁℃椂鏂規硶鎵闅句互浼佸強鐨勩?/span>
2.
鎴愭湰浣庛?/span>timeGetTime 鍑芥暟闇瑕侀摼鎺ュ濯掍綋搴?/span>winmm.lib錛?/span>QueryPerformance* 鍑芥暟鏍規嵁MSDN鐨勮鏄庯紝闇瑕佺‖浠剁殑鏀寔錛堣櫧鐒舵垜榪樻病鏈夎榪囦笉鏀寔鐨勬満鍣級鍜?/span>KERNEL搴撶殑鏀寔錛屾墍浠ヤ簩鑰呴兘鍙兘鍦?/span>Windows騫沖彴涓嬩嬌鐢紙鍏充簬DOS騫沖彴涓嬬殑楂樼簿搴﹁鏃墮棶棰橈紝鍙互鍙傝冦婂浘褰㈢▼搴忓紑鍙戜漢鍛樻寚鍗椼嬶紝閲岄潰鏈夊叧浜庢帶鍒跺畾鏃跺櫒8253鐨勮緇嗚鏄庯級銆備絾RDTSC鎸囦護鏄竴鏉?/span>CPU鎸囦護錛屽嚒鏄?/span>i386騫沖彴涓?/span>Pentium浠ヤ笂鐨勬満鍣ㄥ潎鏀寔錛岀敋鑷蟲病鏈夊鉤鍙扮殑闄愬埗錛堟垜鐩鎬俊i386鐗堟湰UNIX鍜?/span>Linux涓嬭繖涓柟娉曞悓鏍烽傜敤錛屼絾娌℃湁鏉′歡璇曢獙錛夛紝鑰屼笖鍑芥暟璋冪敤鐨勫紑閿鏄渶灝忕殑銆?/span>
3.
鍏鋒湁鍜?/span>CPU涓婚鐩存帴瀵瑰簲鐨勯熺巼鍏崇郴銆備竴涓鏁扮浉褰撲簬1/(CPU涓婚Hz鏁?/span>)縐掞紝榪欐牱鍙鐭ラ亾浜?/span>CPU鐨勪富棰戯紝鍙互鐩存帴璁$畻鍑烘椂闂淬傝繖鍜?/span>QueryPerformanceCount涓嶅悓錛屽悗鑰呴渶瑕侀氳繃QueryPerformanceFrequency鑾峰彇褰撳墠璁℃暟鍣ㄦ瘡縐掔殑璁℃暟嬈℃暟鎵嶈兘鎹㈢畻鎴愭椂闂淬?/span>

榪欎釜鏂規硶鐨勭己鐐規槸錛?/span>
1.
鐜版湁鐨?/span>C/C++緙栬瘧鍣ㄥ鏁頒笉鐩存帴鏀寔浣跨敤RDTSC鎸囦護錛岄渶瑕佺敤鐩存帴宓屽叆鏈哄櫒鐮佺殑鏂瑰紡緙栫▼錛屾瘮杈冮夯鐑︺?/span>
2.
鏁版嵁鎶栧姩姣旇緝鍘夊銆傚叾瀹炲浠諱綍璁¢噺鎵嬫鑰岃█錛岀簿搴﹀拰紼沖畾鎬ф案榪滄槸涓瀵圭煕鐩俱傚鏋滅敤浣庣簿搴︾殑timeGetTime鏉ヨ鏃訛紝鍩烘湰涓婃瘡嬈¤鏃剁殑緇撴灉閮芥槸鐩稿悓鐨勶紱鑰?/span>RDTSC鎸囦護姣忔緇撴灉閮戒笉涓鏍鳳紝緇忓父鏈夊嚑鐧劇敋鑷充笂鍗冪殑宸窛銆傝繖鏄繖縐嶆柟娉曢珮綺懼害鏈韓鍥烘湁鐨勭煕鐩俱?/span>

鍏充簬榪欎釜鏂規硶璁℃椂鐨勬渶澶ч暱搴︼紝鎴戜滑鍙互綆鍗曠殑鐢ㄤ笅鍒楀叕寮忚綆楋細

鑷?/span>CPU涓婄數浠ユ潵鐨勭鏁?/span> = RDTSC璇誨嚭鐨勫懆鏈熸暟 / CPU涓婚閫熺巼錛?/span>Hz錛?/span>

64浣嶆棤絎﹀彿鏁存暟鎵鑳借〃杈劇殑鏈澶ф暟瀛楁槸1.8×10^19錛屽湪鎴戠殑Celeron 800涓婂彲浠ヨ鏃跺ぇ綰?/span>700騫達紙涔︿腑璇村彲浠ュ湪200MHz鐨?/span>Pentium涓婅鏃?/span>117騫達紝榪欎釜鏁板瓧涓嶇煡閬撴槸鎬庝箞寰楀嚭鏉ョ殑錛屼笌鎴戠殑璁$畻鏈夊嚭鍏ワ級銆傛棤璁哄浣曪紝鎴戜滑澶у彲涓嶅繀鍏沖績婧㈠嚭鐨勯棶棰樸?/span>

涓嬮潰鏄嚑涓皬渚嬪瓙錛岀畝瑕佹瘮杈冧簡涓夌璁℃椂鏂規硶鐨勭敤娉曚笌綺懼害
//Timer1.cpp
浣跨敤浜?/span>RDTSC鎸囦護鐨?/span>Timer綾?/span>//KTimer綾葷殑瀹氫箟鍙互鍙傝銆?/span>Windows鍥懼艦緙栫▼銆?/span>P15
//
緙栬瘧琛岋細CL Timer1.cpp /link USER32.lib
#include <stdio.h>
#include "KTimer.h"
main()
{
unsigned t;
KTimer timer;
timer.Start();
Sleep(1000);
t = timer.Stop();
printf("Lasting Time: %d\n",t);
}

//Timer2.cpp 浣跨敤浜?/span>timeGetTime鍑芥暟
//
闇鍖呭惈<mmsys.h>錛屼絾鐢變簬Windows澶存枃浠墮敊緇煎鏉傜殑鍏崇郴
//
綆鍗曞寘鍚?/span><windows.h>姣旇緝鍋鋒噿錛氾級
//
緙栬瘧琛岋細CL timer2.cpp /link winmm.lib
#include <windows.h>
#include <stdio.h>

main()
{
DWORD t1, t2;
t1 = timeGetTime();
Sleep(1000);
t2 = timeGetTime();
printf("Begin Time: %u\n", t1);
printf("End Time: %u\n", t2);
printf("Lasting Time: %u\n",(t2-t1));
}

//Timer3.cpp 浣跨敤浜?/span>QueryPerformanceCounter鍑芥暟
//
緙栬瘧琛岋細CL timer3.cpp /link KERNEl32.lib
#include <windows.h>
#include <stdio.h>

main()
{
LARGE_INTEGER t1, t2, tc;
QueryPerformanceFrequency(&tc);
printf("Frequency: %u\n", tc.QuadPart);
QueryPerformanceCounter(&t1);
Sleep(1000);
QueryPerformanceCounter(&t2);
printf("Begin Time: %u\n", t1.QuadPart);
printf("End Time: %u\n", t2.QuadPart);
printf("Lasting Time: %u\n",( t2.QuadPart- t1.QuadPart));
}

////////////////////////////////////////////////
//
浠ヤ笂涓変釜紺轟緥紼嬪簭閮芥槸嫻嬭瘯1縐掗挓浼戠湢鎵鑰楄垂鐨勬椂闂?/span>



bilicon 2007-07-26 10:57 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲永久在线| 亚洲婷婷综合色高清在线| 国产伦精品一区二区三区视频孕妇 | 国产一区日韩欧美| 欧美福利视频在线观看| 久久久久免费视频| 久久综合电影一区| 欧美激情一二区| 亚洲精品一级| 午夜视频久久久| 久久噜噜噜精品国产亚洲综合| 久久久免费观看视频| 久久一区二区三区四区| 女仆av观看一区| 欧美日韩专区| 国产在线精品一区二区中文| 狠久久av成人天堂| 亚洲乱码国产乱码精品精天堂| 夜夜嗨av一区二区三区四区| 欧美一区=区| 欧美午夜在线观看| 久久精品99| 欧美成人中文字幕| 国产精品成人免费视频| 韩国精品一区二区三区| 亚洲精品欧洲| 亚洲一区二区在线看| 久久激五月天综合精品| 亚洲国产一区视频| 亚洲视频日本| 日韩一区二区精品葵司在线| 欧美肥婆在线| 久久婷婷丁香| 久久不见久久见免费视频1| 亚洲精品一区二区三区四区高清| 中日韩在线视频| 亚洲欧美国产日韩天堂区| 欧美一区二区在线观看| 最新国产成人av网站网址麻豆| 亚洲美女精品成人在线视频| 毛片一区二区三区| 最近看过的日韩成人| 久久久久久久综合日本| 国产精品专区第二| 亚洲影院在线观看| 久久精品成人一区二区三区蜜臀| 亚洲第一久久影院| 欧美日韩一区在线| 午夜伦理片一区| 久久成人免费网| 亚洲国产高清一区| 亚洲破处大片| 欧美韩日一区| 久久一区二区三区国产精品| 精品999成人| 欧美高清在线| 欧美精品免费在线观看| 在线综合视频| 免费欧美视频| 国产精品美女一区二区| 午夜一区在线| 国产亚洲制服色| 欧美chengren| 欧美激情a∨在线视频播放| 亚洲无毛电影| 西瓜成人精品人成网站| 激情丁香综合| 亚洲视频第一页| 亚洲欧美中文日韩在线| 亚洲午夜一区二区三区| 米奇777在线欧美播放| 国产精品日本一区二区| 亚洲国产精品传媒在线观看| 欧美日韩人人澡狠狠躁视频| 亚洲香蕉网站| 亚洲一区影院| 欧美激情第1页| 亚洲一区二区视频| 久久国产免费看| 亚洲天堂黄色| 亚洲欧美日韩国产中文在线| 国户精品久久久久久久久久久不卡| 亚洲欧美bt| 亚洲自拍偷拍麻豆| 国产精品美女久久久久av超清 | 噜噜噜噜噜久久久久久91| 亚洲精品四区| 久久狠狠一本精品综合网| 在线一区亚洲| 免费不卡中文字幕视频| 夜夜嗨av一区二区三区四季av| 激情久久综合| 亚洲欧美日韩区| 久久精品一本久久99精品| 激情视频一区二区三区| 久久久久久国产精品mv| 亚洲国产精品国自产拍av秋霞| 午夜精品福利一区二区三区av| 亚洲第一免费播放区| 中文国产一区| 亚洲电影天堂av| 国产精品亚洲美女av网站| 久久久久国产精品www| 亚洲激情视频在线播放| 欧美在线高清视频| 一卡二卡3卡四卡高清精品视频| 国产免费观看久久| 欧美大片一区二区| 久久精品日韩欧美| 一区二区三区四区五区视频| 欧美a级片一区| 欧美在线亚洲在线| 一本色道久久| 日韩一级成人av| 亚洲电影在线看| 国产精品美女在线观看| 欧美四级伦理在线| 国产精品毛片高清在线完整版| 欧美日韩亚洲激情| 欧美日本一区二区视频在线观看| 欧美成人高清| 久久av一区二区三区漫画| 中文亚洲视频在线| 美脚丝袜一区二区三区在线观看| 欧美色大人视频| 亚洲国产精品美女| 国产一区99| 先锋影音国产一区| 亚洲你懂的在线视频| 欧美在线|欧美| 欧美福利视频一区| 国产一区91精品张津瑜| 亚洲免费观看在线观看| 欧美专区在线观看一区| 亚洲毛片在线观看| 欧美在线视频日韩| 午夜在线不卡| 亚洲国产日本| 久久精品理论片| 国产欧美成人| 亚洲欧美乱综合| 日韩午夜av| 欧美性猛交xxxx免费看久久久| 伊人一区二区三区久久精品| 午夜精品影院在线观看| 欧美一区在线看| 一级成人国产| 久久综合色8888| 亚洲永久字幕| 久久综合九色九九| av成人免费观看| 久久综合网hezyo| 午夜精品一区二区三区在线视| 久久久午夜电影| 久久激情视频免费观看| 欧美高清视频| 欧美xxxx在线观看| 国产日韩欧美在线观看| 日韩写真在线| 亚洲精品中文字幕有码专区| 亚洲欧美日韩另类| 亚洲欧美日韩精品| 欧美日韩国产小视频在线观看| 欧美电影免费观看高清完整版| 久久久久九九视频| 一道本一区二区| 久久精品一区二区三区不卡牛牛| 亚洲国产日韩在线一区模特| 一本大道久久a久久精品综合| 国产一区导航| 亚洲午夜精品视频| 亚洲高清在线播放| 亚洲一区尤物| 欧美风情在线观看| 麻豆91精品| 国产一区二区福利| 亚洲欧美日韩国产一区二区三区| 亚洲欧洲中文日韩久久av乱码| 亚洲老板91色精品久久| 亚洲视频在线一区观看| 亚洲天堂偷拍| 亚洲人在线视频| 欧美激情一区二区| 国产精品99久久久久久宅男| 亚洲尤物精选| 激情综合在线| 免费观看成人| 欧美a级大片| 欧美亚洲在线观看| 欧美日一区二区在线观看 | 影音先锋成人资源站| 亚洲久久在线| 亚洲人午夜精品| 蜜臀91精品一区二区三区| 久久国产精品高清| 国产欧美一区二区三区久久人妖| 亚洲麻豆视频| 亚洲专区免费| 国产一区av在线| 久久久久这里只有精品|