Jump to content

How can you encrypt to MD5?


Recommended Posts

I did a search, but the thread about it is really old, and the scripts no longer work.

How can you encrypt a string to MD5?

From: http://www.autoitscript.com/forum/index.ph...1010&hl=MD5

The function fails:

#AutoIt3Wrapper_plugin_funcs = MD5Hash

$plH = PluginOpen("..\Bin\MD5\MD5Hash.dll")

MsgBox(0, "Test MD5Hash.DLL", "String 'Test String':" & @TAB & MD5Hash("Test String", 2, True) & @CRLF & _

"File 'MD5Hash.dll':" & @TAB & MD5Hash("..\Bin\MD5\MD5Hash.dll", 1, True))

PluginClose($plH)

And this..don't remember where the thread is but credit is given:

;===============================================================================
;
; Function Name:    _StringMD5($string)
; Description:      Calculates the MD5 hash of a string argument
; Parameter:        $string is the string you want to hash.
; Requirement:      AutoIT version 3.2.5.0 (beta)
; Return Value:     Returns the MD5 hash string of the string argument.
; Author:           Arses
;
;===============================================================================
MsgBox(0, "", "Test = " & _StringMD5("Test"))
Func _StringMD5($string)
    $string = String($string)
    Local $bitLen, $bytes, $bytesLen, $blockN, $i
    Local $A, $B, $C, $D, $X[16], $Am, $Bm, $Cm, $Dm, $T[65]
    $bytes = StringToBinary($string)
    $bitLen = 8 * BinaryLen($bytes)
    $bytes += BinaryMid(Binary(0x80), 1, 1)
    $bytesLen = BinaryLen($bytes)
    While Mod($bytesLen + 8, 64) <> 0
        $bytes += BinaryMid(Binary(0x00), 1, 1)
        $bytesLen = BinaryLen($bytes)
    WEnd
    $bytes += BinaryMid(Binary($bitLen) + Binary(0), 1, 8)
    $bytesLen = BinaryLen($bytes)
    $A = 0x67452301
    $B = 0xEFCDAB89
    $C = 0x98BADCFE
    $D = 0x10325476
    For $i = 0 To 64 Step 1
        $T[$i] = 4294967296 * Abs(Sin($i))
    Next
    $blockN = 0
    Do
        For $i = 0 To 15 Step 1
            $X[$i] = BinaryMid($bytes, $blockN * 64 + 4 + $i * 4, 1) + BinaryMid($bytes, $blockN * 64 + 3 + $i * 4, 1) + _
                    BinaryMid($bytes, $blockN * 64 + 2 + $i * 4, 1) + BinaryMid($bytes, $blockN * 64 + 1 + $i * 4, 1)
        Next
        $Am = $A
        $Bm = $B
        $Cm = $C
        $Dm = $D
        ;Stage 1
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[0])) + $T[1])) + BitOR(BitAND($B, $C), BitAND(BitNOT($B), $D)))) + $A)), 7, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[1])) + $T[2])) + BitOR(BitAND($A, $B), BitAND(BitNOT($A), $C)))) + $D)), 12, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[2])) + $T[3])) + BitOR(BitAND($D, $A), BitAND(BitNOT($D), $B)))) + $C)), 17, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[3])) + $T[4])) + BitOR(BitAND($C, $D), BitAND(BitNOT($C), $A)))) + $B)), 22, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[4])) + $T[5])) + BitOR(BitAND($B, $C), BitAND(BitNOT($B), $D)))) + $A)), 7, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[5])) + $T[6])) + BitOR(BitAND($A, $B), BitAND(BitNOT($A), $C)))) + $D)), 12, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[6])) + $T[7])) + BitOR(BitAND($D, $A), BitAND(BitNOT($D), $B)))) + $C)), 17, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[7])) + $T[8])) + BitOR(BitAND($C, $D), BitAND(BitNOT($C), $A)))) + $B)), 22, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[8])) + $T[9])) + BitOR(BitAND($B, $C), BitAND(BitNOT($B), $D)))) + $A)), 7, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[9])) + $T[10])) + BitOR(BitAND($A, $B), BitAND(BitNOT($A), $C)))) + $D)), 12, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[10])) + $T[11])) + BitOR(BitAND($D, $A), BitAND(BitNOT($D), $B)))) + $C)), 17, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[11])) + $T[12])) + BitOR(BitAND($C, $D), BitAND(BitNOT($C), $A)))) + $B)), 22, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[12])) + $T[13])) + BitOR(BitAND($B, $C), BitAND(BitNOT($B), $D)))) + $A)), 7, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[13])) + $T[14])) + BitOR(BitAND($A, $B), BitAND(BitNOT($A), $C)))) + $D)), 12, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[14])) + $T[15])) + BitOR(BitAND($D, $A), BitAND(BitNOT($D), $B)))) + $C)), 17, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[15])) + $T[16])) + BitOR(BitAND($C, $D), BitAND(BitNOT($C), $A)))) + $B)), 22, "D") + $C))
        ;Stage 2
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[1])) + $T[17])) + BitOR(BitAND($B, $D), BitAND($C, BitNOT($D))))) + $A)), 5, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[6])) + $T[18])) + BitOR(BitAND($A, $C), BitAND($B, BitNOT($C))))) + $D)), 9, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[11])) + $T[19])) + BitOR(BitAND($D, $B), BitAND($A, BitNOT($B))))) + $C)), 14, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[0])) + $T[20])) + BitOR(BitAND($C, $A), BitAND($D, BitNOT($A))))) + $B)), 20, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[5])) + $T[21])) + BitOR(BitAND($B, $D), BitAND($C, BitNOT($D))))) + $A)), 5, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[10])) + $T[22])) + BitOR(BitAND($A, $C), BitAND($B, BitNOT($C))))) + $D)), 9, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[15])) + $T[23])) + BitOR(BitAND($D, $B), BitAND($A, BitNOT($B))))) + $C)), 14, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[4])) + $T[24])) + BitOR(BitAND($C, $A), BitAND($D, BitNOT($A))))) + $B)), 20, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[9])) + $T[25])) + BitOR(BitAND($B, $D), BitAND($C, BitNOT($D))))) + $A)), 5, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[14])) + $T[26])) + BitOR(BitAND($A, $C), BitAND($B, BitNOT($C))))) + $D)), 9, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[3])) + $T[27])) + BitOR(BitAND($D, $B), BitAND($A, BitNOT($B))))) + $C)), 14, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[8])) + $T[28])) + BitOR(BitAND($C, $A), BitAND($D, BitNOT($A))))) + $B)), 20, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[13])) + $T[29])) + BitOR(BitAND($B, $D), BitAND($C, BitNOT($D))))) + $A)), 5, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[2])) + $T[30])) + BitOR(BitAND($A, $C), BitAND($B, BitNOT($C))))) + $D)), 9, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[7])) + $T[31])) + BitOR(BitAND($D, $B), BitAND($A, BitNOT($B))))) + $C)), 14, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[12])) + $T[32])) + BitOR(BitAND($C, $A), BitAND($D, BitNOT($A))))) + $B)), 20, "D") + $C))
        ;Stage 3
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[5])) + $T[33])) + BitXOR($B, $C, $D))) + $A)), 4, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[8])) + $T[34])) + BitXOR($A, $B, $C))) + $D)), 11, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[11])) + $T[35])) + BitXOR($D, $A, $B))) + $C)), 16, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[14])) + $T[36])) + BitXOR($C, $D, $A))) + $B)), 23, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[1])) + $T[37])) + BitXOR($B, $C, $D))) + $A)), 4, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[4])) + $T[38])) + BitXOR($A, $B, $C))) + $D)), 11, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[7])) + $T[39])) + BitXOR($D, $A, $B))) + $C)), 16, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[10])) + $T[40])) + BitXOR($C, $D, $A))) + $B)), 23, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[13])) + $T[41])) + BitXOR($B, $C, $D))) + $A)), 4, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[0])) + $T[42])) + BitXOR($A, $B, $C))) + $D)), 11, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[3])) + $T[43])) + BitXOR($D, $A, $B))) + $C)), 16, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[6])) + $T[44])) + BitXOR($C, $D, $A))) + $B)), 23, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[9])) + $T[45])) + BitXOR($B, $C, $D))) + $A)), 4, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[12])) + $T[46])) + BitXOR($A, $B, $C))) + $D)), 11, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[15])) + $T[47])) + BitXOR($D, $A, $B))) + $C)), 16, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[2])) + $T[48])) + BitXOR($C, $D, $A))) + $B)), 23, "D") + $C))
        ;Stage 4
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[0])) + $T[49])) + BitXOR($C, BitOR($B, BitNOT($D))))) + $A)), 6, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[7])) + $T[50])) + BitXOR($B, BitOR($A, BitNOT($C))))) + $D)), 10, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[14])) + $T[51])) + BitXOR($A, BitOR($D, BitNOT($B))))) + $C)), 15, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[5])) + $T[52])) + BitXOR($D, BitOR($C, BitNOT($A))))) + $B)), 21, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[12])) + $T[53])) + BitXOR($C, BitOR($B, BitNOT($D))))) + $A)), 6, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[3])) + $T[54])) + BitXOR($B, BitOR($A, BitNOT($C))))) + $D)), 10, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[10])) + $T[55])) + BitXOR($A, BitOR($D, BitNOT($B))))) + $C)), 15, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[1])) + $T[56])) + BitXOR($D, BitOR($C, BitNOT($A))))) + $B)), 21, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[8])) + $T[57])) + BitXOR($C, BitOR($B, BitNOT($D))))) + $A)), 6, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[15])) + $T[58])) + BitXOR($B, BitOR($A, BitNOT($C))))) + $D)), 10, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[6])) + $T[59])) + BitXOR($A, BitOR($D, BitNOT($B))))) + $C)), 15, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[13])) + $T[60])) + BitXOR($D, BitOR($C, BitNOT($A))))) + $B)), 21, "D") + $C))
        $A = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[4])) + $T[61])) + BitXOR($C, BitOR($B, BitNOT($D))))) + $A)), 6, "D") + $B))
        $D = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[11])) + $T[62])) + BitXOR($B, BitOR($A, BitNOT($C))))) + $D)), 10, "D") + $A))
        $C = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[2])) + $T[63])) + BitXOR($A, BitOR($D, BitNOT($B))))) + $C)), 15, "D") + $D))
        $B = Dec(Hex(BitRotate(Dec(Hex(Dec(Hex(Dec(Hex(Dec(Hex($X[9])) + $T[64])) + BitXOR($D, BitOR($C, BitNOT($A))))) + $B)), 21, "D") + $C))
        $A = Dec(Hex($A + $Am))
        $B = Dec(Hex($B + $Bm))
        $C = Dec(Hex($C + $Cm))
        $D = Dec(Hex($D + $Dm))
        $blockN += 1
    Until $blockN * 64 >= $bytesLen
    Return Hex(Binary($A) + Binary($B) + Binary($C) + Binary($D))
EndFunc   ;==>_StringMD5

It says "Test" = 0F6CBA0ADC28..then more blah blah blah

And this site says "Test" = 0cbc6611f5540bd0809a388dc95a615b

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Okay...on that first link, I got the object downloaded..

$oMD5 = ObjCreate("XStandard.MD5");"ActiveX MD5 Hash/CheckSum
$oMD5.GetCheckSumFromString("test")

: ==> Variable must be of type "Object".:

$oMD5.GetCheckSumFromString("test")

$oMD5^ ERROR

Looks like an object var to me... Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Oh, uh..no. Never had to do that before.

Not sure if I did this right...

Posted Image

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Ah ha! Registered it! Everything works now. Correctly.

But, if I compile, will people need to register it aswell?

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

But, if I compile, will people need to register it aswell?

Yes, but you could FileInstall() and then register it for them if you want to make it easy.
Link to comment
Share on other sites

MsgBox(0,'','Test = ' & _Crypt_HashData("Test"))

;===============================================================================
; Function Name:    _Crypt_HashData()
; Description:          Calculate hash from data
; Syntax:
; Parameter(s):     $vData - data to hash, can be binary or a string
;                   $iAlgID - hash algorithm identifier, can be one of the following:
;                       0x8001 = MD2
;                       0x8002 = MD4
;                       0x8003 = MD5 (default)
;                       0x8004 = SHA1
;                       also see http://msdn.microsoft.com/en-us/library/aa375549(VS.85).aspx
; Requirement(s):
; Return Value(s):  Success = Returns hash string
;                   Failure = Returns empty string and sets error:
;                       @error -1 = error opening advapi32.dll
;                       @error 1 = failed CryptAcquireContext
;                       @error 2 = failed CryptCreateHash
;                       @error 3 = failed CryptHashData
; Author(s):   Siao
; Modification(s):
;===============================================================================
Func _Crypt_HashData($vData, $iAlgID = 0x8003)
    Local $hDll = DllOpen('advapi32.dll'), $iLen = BinaryLen($vData), $hContext, $hHash, $aRet, $sRet = "", $iErr = 0, $tDat = DllStructCreate("byte[" & $iLen+1 & "]"), $tBuf
    DllStructSetData($tDat, 1, $vData)
    If $hDll = -1 Then Return SetError($hDll,0,$sRet)
    $aRet = DllCall($hDll,'int','CryptAcquireContext', 'ptr*',0, 'ptr',0, 'ptr',0, 'dword',1, 'dword',0xF0000000) ;PROV_RSA_FULL = 1; CRYPT_VERIFYCONTEXT = 0xF0000000
    If Not @error And $aRet[0] Then
        $hContext = $aRet[1]
        $aRet = DllCall($hDll,'int','CryptCreateHash', 'ptr',$hContext, 'dword',$iAlgID, 'ptr',0, 'dword',0, 'ptr*',0)
        If $aRet[0] Then
            $hHash = $aRet[5]
            $aRet = DllCall($hDll,'int','CryptHashData', 'ptr',$hHash, 'ptr',DllStructGetPtr($tDat), 'dword',$iLen, 'dword',0)
            If $aRet[0] Then
                $aRet = DllCall($hDll,'int','CryptGetHashParam', 'ptr',$hHash, 'dword',2, 'ptr',0, 'int*',0, 'dword',0) ;HP_HASHVAL = 2
                $tBuf = DllStructCreate("byte[" & $aRet[4] & "]")
                DllCall($hDll,'int','CryptGetHashParam', 'ptr',$hHash, 'dword',2, 'ptr',DllStructGetPtr($tBuf), 'int*',$aRet[4], 'dword',0)
                $sRet = Hex(DllStructGetData($tBuf, 1))
            Else
                $iErr = 3
            EndIf
            DllCall($hDll,'int','CryptDestroyHash', 'ptr',$hHash)
        Else
            $iErr = 2
        EndIf
        DllCall($hDll,'int','CryptReleaseContext', 'ptr',$hContext, 'dword',0)
    Else
        $iErr = 1
    EndIf
    DllClose($hDll)
    Return SetError($iErr,0,$sRet)
EndFunc

"be smart, drink your wine"

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...