And MD5.asp calculated the same "correct" Delphi encryption code

zhaozj2021-02-16  91

In fact, it is defective, but it is necessary for this defect (no way) I have mentioned MD5.asp this morning because of design defects, when encrypting the double-character character (Chinese), will be encrypted and standard The MD5 algorithm (.NET / INDYHSHMESSAGEST5 / MD5.PAS, the three calculation results are the same as the result of calculating the 岀 in bytes). The reason is that the MD5.asp uses the MID function, which is removed " Character ", and the correct approach should be that the extracted byte can be the data in the forum database (for example, DVBBS7) is already the result of MD5.asp, so we can only write wrong, to rewrite a summ of MD5.asp. Encryption process >>>> The extensive use of MD5.asp seems to have defects I have written out with Delphi, although I don't know if the result of the ASC function is correct code - my goal is the same as MD5.asp Isn't this defective program now?

Unit AMD5;

Interface

// File Name: AMD5.PAS / / / / Function: The same as the MD5.asp calculation is the same // / / Author: Adapted from Scriptbaby from MD5.asp // // Completion time: 2004-07-21 //

/ / Please send the person to keep the above information, thank you //

Uses sysutils;

TYPE ARLONGWORD = array of longword;

type sMD5 = class class function RotateLeft (const lValue, Bits: longword): longword; class function MD5 (const sMessage: string; const sType: boolean = false): string; class function ConvToWord (const sMessage: string): arrlongword; overload Class Function ConvToWord: arrlongword; overload; Class Function WordTohex (Const Lvalue: longword): string;

IMPLEMentation

Const bits_to_a_byte = 8; BYTES_TO_A_WORD = 4; bits_to_a_word = 32;

CAA = $ 67452301; CBB = $ EFCDAB89; CCC = $ 98BADCFE; CDD = $ 10325476;

MODULUS_BITS = 512; CONGRUENT_BITS = 448;

{SMD5}

Class Function SMD5.ConvToWord (Const SMESSAGE: WIDESTRING): Arrlongword; Var LomersageLength, Lnumberofwords, LbytePosition, Lbytecount, LWORDAVOUNT: Longword; LWordArray: arlongword;

{Function} Function ASC (const t: widechar): smallint; var s: string; A: smallint; begin s: = t;

A: = Smallint (s [1]); Case Length (s) of 2: Begin A: = A SHL 8; A: = A Smallint (S [2]); ELSE; END; Result: = A; END;

Begin LMessageLength: = Length (SMESSAGE);

Lnumberofwords: = (((((((((MODULUS_BITS - CONGRUENT_BITS) DIV BIV (MODUS_BITS DIV BITS_TO_A_BYTE)) 1) * (MODULUS_BITS DIV BITS_TO_A_WORD);

SETLENGTH (LWORDARRAY, LNUMBEROFWORDS);

lByteCount: = 0; While lByteCount

LWordCount: = lbytecount div BYTES_TO_A_WORD; lbyteposition: = (lbytecount mode_to_a_word) * bits_to_a_byte;

LWordArray [LWORDCOUNT]: = LWORDARRAY [LWORDCOUNT] OR ($ 80 shl lbyteposition);

LWordArray [lnumberofwords - 2]: = LMESSAGALENGTH SHL 3; LWORDARRAY [lnumberofwords - 1]: = LMESSAGELENGTH SHR 29;

Result: = lwordArray;

END;

Class Function SMD5.ConvToWord (const SMESSAGE: STRING): Arrlongword: = convtoword (WideString (SMESSAGE));

Class Function SMD5.MD5 (const stype: boolean = false): string; const S11 = 7; S12 = 12; S13 = 17; S14 = 22; S21 = 5; S22 = 9; S23 = 14; S24 = 20; S31 = 4; S32 = 11; S33 = 16; S34 = 23; S41 = 6; S42 = 10; S43 = 15; S44 = 21;

VAR K: Integer; AA, BB, CC, DD, A, B, C, D: longword; x: arrlongword; s: string; {functions} function md5_f (const x, y, z: longword): longword; begin Result: = (x and y) OR ((NOT X) and z); END;

Function MD5_G (Const x, y, z: longword): longword; begin result: = (x and z) or (Y AND (not z));

Function MD5_H (Const x, y, z: longword): longword; beg Result: = (x xor y xor z);

Function MD5_I (Const x, y, z: longword): longword; begin result: = (Y XOR (x or (not z));

Procedure MD5_FF (var A: Longword; Const B, C, D, X, S, Ac: longword); Begin A: = A MD5_F (B, C, D) X AC; A: = RotateLeft (A, s); A: = a b; end;

Procedure MD5_GG (VAR A: Longword; Const B, C, D, X, S, AC: longword); Begin A: = A MD5_G (B, C, D) X AC; A: = Rotateleft (A, s); A: = a b; end;

Procedure MD5_HH (var A: Longword; Const B, C, D, X, S, AC: longword); Begin A: = A MD5_H (B, C, D) X AC; A: = Rotateleft (A, s); A: = a b; end;

Procedure MD5_II (VAR A: Longword; Const B, C, D, X, S, Ac: longword); Begin A: = A MD5_I (B, C, D) X AC; A: = Rotateleft (A, s); A: = a b; end;

Begin

{MD5} x: = convToword (SMESSAGE);

A: = Caa; B: = CBB; C: = CCC; D: = CDD;

K: = 0; Repeat aa: = a; bb: = b; cc: = C; DD: = D;

MD5_FF (A, B, C, D, X [K 0], S11, $ D76AA478); MD5_FF (D, A, B, C, X [K 1], S12, $ E8C7B756); MD5_FF (C, D, A, B, X [K 2], S13, $ 242070dB); MD5_FF (B, C, D, A, X [K 3], S14, $ C1BDCEEE); MD5_FF (A, B, C, D, X [K 4], S11, $ F57C0FAF); MD5_FF (D, A, B, C, X [K 5], S12, $ 4787C62A); MD5_FF (C, D, A, B, X [ K 6], S13, $ A8304613); MD5_FF (B, C, D, A, X [K 7], S14, $ FD469501); MD5_FF (A, B, C, D, X [K 8] , S11, $ 698098d8); MD5_FF (D, A, B, C, X [K 9], S12, $ 8B44F7AF); MD5_FF (C, D, A, B, X [K 10], S13, $ FFFF5BB1); MD5_FF (B, C, D, A, X [K 11], S14, $ 895CD7BE); MD5_FF (A, B, C, D, X [K 12], S11, $ 6B901122); MD5_FF (D, A, B, C, X [K 13], S12, $ FD987193); MD5_FF (C, D, A, B, X [K 14], S13, $ A679438E); MD5_FF (B, C , D, A, X [K 15], S14, $ 49B40821); MD5_GG (A, B, C, D, X [K 1], S21, $ F61E2562); MD5_GG (D, A, B, C X [K 6], S22, $ C040B340); MD5_GG (C, D, A, B, X [K 11], S23, $ 265E5A51); MD5_GG (B, C, D, A, X [K 0], S24, $ E9B6C7AA); MD5_GG ( A, B, C, D, X [K 5], S21, $ D62F105D); MD5_GG (D, A, B, C, X [K 10], S22, $ 2441453); MD5_GG (C, D, A , B, X [K 15], S23, $ D8A1E681); MD5_GG (B, C, D, A, X [K 4], S24, $ E7D3FBC8); MD5_GG (A, B, C, D, X [k 9], S21, $ 21e1cde6); MD5_GG (D, A, B, C, X [K 14], S22, $ C33707D6); MD5_GG (C, D, A, B, X [K 3 ], S23, $ F4D50D87); MD5_GG (B, C, D, A, X [K 8], S24, $ 455A14ED); MD5_GG (A, B, C, D, X [K 13], S21, $ A9E3E905); MD5_GG (D, A, B, C, X [K 2], S22, $ FCEFA3F8); MD5_GG (C, D, A, B, X [K 7], S23, $ 676F02D9); MD5_GG (B, C, D, A, X [K 12], S24, $ 8D2A4C8A);

MD5_HH (A, B, C, D, X [K 5], S31, $ FFFA3942); MD5_HH (D, A, B, C, X [K 8], S32, $ 8771F681); MD5_HH (C, D, A, B, X [K 11], S33, $ 6d9d6122); MD5_HH (B, C, D, A, X [K 14], S34, $ FDE5380C); MD5_HH (A, B, C, D, X [K 1], S31, $ A4BEEA44); MD5_HH (D, A, B, C, X [K 4], S32, $ 4BDECFA9); MD5_HH (C, D, A, B, X [ K 7], S33, $ F6BB4B60); MD5_HH (B, C, D, A, X [K 10], S34, $ BEBFBC70); MD5_HH (A, B, C, D, X [K 13] , S31, $ 289B7EC6); MD5_HH (D, A, B, C, X [K 0], S32, $ EAA127FA); MD5_HH (C, D, A, B, X [K 3], S33, $ D4EF3085); MD5_HH (B, C, D, A, X [K 6], S34, $ 4881D05); MD5_HH (A, B, C, D, X [K 9], S31, $ D9D4D039); MD5_HH (D, A, B, C, X [K 12], S32, $ E6DB99E5); MD5_HH (C, D, A, B, X [K 15], S33, $ 1FA27CF8); MD5_HH (B, C , D, A, X [K 2], S34, $ C4AC5665); MD5_II (A, B, C, D, X [K 0], S41, $ F4292244); MD5_II (D, A, B, C X [K 7], S42, $ 432AFF97); MD5_II (C, D, A, B, X [K 14], S43, $ AB9423A7); MD5_II (B, C, D, A, X [K 5], S44, $ FC93A039); MD5_II (A, B, C, D, X [K 12], S41, $ 655B59C3); MD5_II (D, A, B, C, X [K 3], S42, $ 8F0CC92); MD5_II (C, D , A, B, X [K 10], S43, $ FFEFF47D); MD5_II (B, C, D, A, X [K 1], S44, $ 85845DD1); MD5_II (A, B, C, D , X [K 8], S41, $ 6FA87E4F); MD5_II (D, A, B, C, X [K 15], S42, $ Fe2CE6E0); MD5_II (C, D, A, B, X [K 6], S43, $ A3014314); MD5_II (B, C, D, A, X [K 13], S44, $ 4E0811A1); MD5_II (A, B, C, D, X [K 4], S41, $ F7537E82); MD5_II (D, A, B, C, X [K 11], S42, $ BD3AF235); MD5_II (C, D, A, B, X [K 2], S43, $ 2AD7D2BB ); MD5_II (B, C, D, A, X [K 9], S44, $ EB86D391);

A: = A AA; B: = B BB; C: = C CC; D: = D DD; K: = K 16; Until K> HIGH (X); if Stype Then Begin S: = Wordtohex (a) WordTohex (B) WordToHex (C) WordTohex (D) end else begin s: = WordTohex (B) WordTohex (C); END;

Result: = strlower (Pansichar (s));

Class Function SMD5.RotateLeft (Const Lvalue, Bits: Longword): Longword; Begin Result: = (Lvalue SHR (32 - Bits)); END;

Class Function SMD5.WordToHex (Const Lvalue: longword): string; var s: string; begin s: = INTTOHEX (LVALUE, 8); Result: = S [7] S [8] S [5] S [ 6] S [3] S [4] S [1] S [2];

End.

Maybe some improper, please advise

转载请注明原文地址:https://www.9cbs.com/read-14377.html

New Post(0)