VB2008 : MD5 et RC4 : j'ai besoin d'aide pour vérifier mon script svp, je ne par
arcadoli
Messages postés1Date d'inscriptionlundi 15 septembre 2008StatutMembreDernière intervention 3 avril 2010
-
3 avril 2010 à 01:38
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 2021
-
3 avril 2010 à 08:06
Bonjour/bonsoir à vous,
J'hésite toujours à demander de l'aide, je fais ce que je peux pour chercher tout seul, mais là j'avoue que je suis un peu perdu...
Je voudrais qu'à partir d'un mot de passe, genre 'bonjour', le MD5 me génère une chaine de 32 caractères Hexa, soit 16 bits,
et qu'ensuite avec cette chaine, mon fichier soit encrypté avec RC4.
En entrée, j'ai un fichier binaire, je convertis les caractères en ANSI et je fais pareil à la sortie de mon script.
Le problème, c'est que si je repasse le même fichier, il ne se décrypte pas....
Voici mon code ci-dessous. Je ne comprends pas tout car j'ai biensur trouvé les sources sur intenet.
J'hésite d'ailleurs sur quelle valeur donner à Private Const ENCRYPT_BLOCK_SIZE = 1
[code=vb
Module Module5
Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" ( _
ByRef phProv As Integer, _
ByVal pszContainer As String, _
ByVal pszProvider As String, _
ByVal dwProvType As Integer, _
ByVal dwFlags As Integer) As Integer
Private Declare Function CryptCreateHash Lib "advapi32.dll" ( _
ByVal hProv As Integer, _
ByVal Algid As Integer, _
ByVal hKey As Integer, _
ByVal dwFlags As Integer, _
ByRef phHash As Integer) As Integer
Private Declare Function CryptHashData Lib "advapi32.dll" ( _
ByVal hHash As Integer, _
ByRef pbData As String, _
ByVal dwDataLen As Integer, _
ByVal dwFlags As Integer) As Integer
Private Declare Function CryptDeriveKey Lib "advapi32.dll" ( _
ByVal hProv As Integer, _
ByVal Algid As Integer, _
ByVal hBaseData As Integer, _
ByVal dwFlags As Integer, _
ByRef phKey As Integer) As Integer
Private Declare Function CryptDestroyHash Lib "advapi32.dll" ( _
ByVal hHash As Integer) As Integer
Private Declare Function CryptDestroyKey Lib "advapi32.dll" ( _
ByVal hKey As Integer) As Integer
Private Declare Function CryptEncrypt Lib "advapi32.dll" ( _
ByVal hKey As Integer, _
ByVal hHash As Integer, _
ByVal Final As Integer, _
ByVal dwFlags As Integer, _
ByVal pbData As String, _
ByVal pdwDataLen As Integer, _
ByVal dwBufLen As Integer) As Integer
Private Declare Function CryptDecrypt Lib "advapi32.dll" ( _
ByVal hKey As Integer, _
ByVal hHash As Integer, _
ByVal Final As Integer, _
ByVal dwFlags As Integer, _
ByVal pbData As String, _
ByRef pdwDataLen As Integer) As Integer
Private Declare Function CryptReleaseContext Lib "advapi32.dll" ( _
ByVal hProv As Integer, _
ByVal dwFlags As Integer) As Integer
Private Declare Function GetLastError Lib "kernel32" () As Integer
Private Declare Function CryptGetHashParam Lib "advapi32.dll" ( _
ByVal pCryptHash As Integer, _
ByVal dwParam As Integer, _
ByRef pbData As Object, _
ByRef pcbData As Integer, _
ByVal dwFlags As Integer) As Integer
Private Const HP_HASHVAL As Integer = 2
Private Const HP_HASHSIZE As Integer = 4
Public Function DoCryptoDecrypt(ByVal data As String) As String
Dim lHExchgKey As Integer
Dim lHCryptprov As Integer = &H0
Dim lHHash As Integer = &H0
Dim lHkey As Integer = &H0
Dim lResult As Integer
Dim sContainer As String = Nothing
Dim sProvider As String
Dim sCryptBuffer As String
Dim lCryptBufLen As Integer
Dim lCryptPoint As Integer
Dim lPasswordPoint As Integer
Dim lPasswordCount As Integer
Dim password As String = "bonjouryo"
Dim sOutputBuffer As String
On Error GoTo DecryptError
'Clear the Output buffer
sOutputBuffer = Nothing
'Get handle to the default CSP.
sContainer = Nothing
sProvider = Nothing
sProvider = MS_DEF_PROV
If CryptAcquireContext(lHCryptprov, sContainer, sProvider, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) = 0 Then
If CryptAcquireContext(lHCryptprov, sContainer, sProvider, PROV_RSA_FULL, CRYPT_NEWKEYSET) = 0 Then
MsgBox("Error " & CStr(GetLastError) & " during CryptAxquireContext! ")
GoTo Finished
End If
End If
'Create a hash object
If CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash) = 0 Then
MsgBox("Error " & CStr(GetLastError) & " during CryptCreateHash! ")
GoTo Finished
End If
'Hash in the password text
If CryptHashData(lHHash, password, Len(password), 0) = 0 Then
MsgBox("Error " & CStr(GetLastError) & " during CryptHashData!")
GoTo Finished
End If
' ------------------- Test pour retourner la clé de hashage -------------------
'Const cstBufferSize As Integer = 256
'Dim abytBuffer() As Byte ' Byte array buffer.
'Dim lngHashLength As Integer ' Length of the Hash.
'Dim strHash As String = Nothing
'Dim lngDataLen As Long
'Dim lngHashSize As Long
'Dim bytHashValue() As Byte
' Resize the byte array buffer.
'ReDim abytBuffer(cstBufferSize - 1)
'lngDataLen = 4 '4 bytes for Long length.
'If CryptGetHashParam(lHHash, HP_HASHSIZE, lngHashSize, lngDataLen, 0&) = 0 Then
' MsgBox("Error " & CStr(GetLastError) & " during GetHashParam1!")
' GoTo Finished
'Else
' lngDataLen = lngHashSize
' ReDim bytHashValue(lngDataLen - 1)
'End If
'' The length of the hash can be found in the first byte array position of the buffer.
' lngHashLength = abytBuffer(0)
'' Get the hash value (HP_HASHVAL).
'' The hash value. The pbData buffer will contain the hash value or message hash for the
'' hash object specified by hHash. This value is generated based on the data supplied earlier to the
'' hash object through the CryptHashData and CryptHashSessionKey functions.
'If CryptGetHashParam(lHHash, HP_HASHVAL, bytHashValue(0), lngDataLen, 0&) = 0 Then
' Err.Raise(vbObjectError Or &HC324&, _
' "MD5Hash", _
' "Failed to obtain hash value, system error " _
' & CStr(Err.LastDllError))
'Else
' Dim intByte As Integer
' Dim hashvalue As String = Nothing
' For intByte = 0 To lngDataLen - 1
' hashvalue = hashvalue & Right$("0" & Hex$(bytHashValue(intByte)), 2)
' Next
'End If
'' Convert the byte array to a VB string.
'For intI = 0 To lngHashLength
' strHash = strHash & Chr(abytBuffer(intI))
'Next intI
'' Return the hashed data as a VB string.
'Dim MD5Hash As String
'MD5Hash = strHash
'' ---------- Fin test ----------------------------------------------------------
'Create a session key from the hash object
If CryptDeriveKey(lHCryptprov, ENCRYPT_ALGORITHM, lHHash, 0, lHkey) = 0 Then
MsgBox("Error " & CStr(GetLastError) & " during CryptDeriveKey!")
GoTo Finished
End If
'Destroy the hash object.
CryptDestroyHash(lHHash)
lHHash = 0
'Prepare sCryptBuffer for CryptDecrypt
lCryptBufLen = Len(data) * 4
If Not CBool(CryptDecrypt(lHkey, 0, 1, 0, data, Len(data))) Then
' If Not CBool(CryptEncrypt(lHkey, 0, 1, 0, data, Len(data), lCryptBufLen)) Then
MsgBox("bytes required: " & CStr(lCryptBufLen))
MsgBox("Error " & CStr(GetLastError) & " during CryptDecrypt!")
GoTo Finished
End If
'Setup output buffer with just decrypted data
sOutputBuffer = data
Finished:
'Outa here
DoCryptoDecrypt = sOutputBuffer
'Destroy session key
If (lHkey) Then lResult = CryptDestroyKey(lHkey)
'Destroy key exchange key handle
If lHExchgKey Then CryptDestroyKey(lHExchgKey)
'Destroy hash object
If lHHash Then CryptDestroyHash(lHHash)
'Release Context provider handle
If lHCryptprov Then lResult = CryptReleaseContext(lHCryptprov, 0)
Exit Function
DecryptError:
MsgBox("Decrypt Error")
GoTo Finished
End Function