on peut ajouter des caractere ? (appres la suite 0123456789ABCDEF)
christian_grandjean
Messages postés25Date d'inscriptiondimanche 7 novembre 2004StatutMembreDernière intervention19 novembre 20132 24 juil. 2008 à 09:29
Bonjour à tous, j'ai corrigé le code en mettant des types aux variables mais toujours pas de commentaires pas eu le temps de complèter.
'
' http://www.vbfrance.com/code.aspx?ID=41581 '
Module RC4EncryptDecrypt
Public Function RC4Encrypt(ByVal text As String, ByVal encryptkey As String) As String
Dim sbox(256) As Integer
Dim key(256) As Integer
Dim temp As Integer
Dim a As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim cipherby As Integer
Dim cipher As String = String.Empty
i = 0
j = 0
RC4Initialize(encryptkey, key, sbox)
For a = 1 To Len(text)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)
cipherby = (Asc(Mid$(text, a, 1))) Xor k
If Len(Hex(cipherby)) = 1 Then
cipher = cipher & "0" & Hex(cipherby)
Else
cipher = cipher & Hex(cipherby)
End If
Next
Return cipher
End Function
Public Function RC4Decrypt(ByVal text, ByVal encryptkey) As String
Dim sbox(256) As Integer
Dim key(256) As Integer
Dim Text2 As String = String.Empty
Dim temp As Integer
Dim a As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim cipherby As Integer
Dim cipher As String = String.Empty
For w = 1 To Len(text) Step 2
Text2 = Text2 & Chr(Dec(Mid$(text, w, 2)))
Next
i = 0
j = 0
RC4Initialize(encryptkey, key, sbox)
For a = 1 To Len(Text2)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)
cipherby = Asc(Mid$(Text2, a, 1)) Xor k
cipher = cipher & Chr(cipherby)
Next
Return cipher
End Function
Public Sub RC4Initialize(ByVal strPwd, ByRef key, ByRef sbox)
Dim tempSwap As Integer
Dim a As Integer
Dim b As Integer
Dim intlength As Integer
intlength = Len(strPwd)
For a = 0 To 255
key(a) = Asc(Mid$(strPwd, a Mod intlength + 1, 1))
sbox(a) = a
Next
b = 0
For a = 0 To 255
b = (b + sbox(a) + key(a)) Mod 256
tempSwap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempSwap
Next
End Sub
Public Function Dec(ByVal number) As String
Dim base As String
Dim iLen As Integer
Dim iReturn As Long
Dim iTemp As String
base = "0123456789ABCDEF"
iLen = Len(number)
For i = 0 To iLen - 1
iTemp = Mid$(number, iLen - i, 1)
iReturn = iReturn + (16 ^ i) * (InStr(1, base, iTemp) - 1)
Next
Return iReturn
End Function
End Module
bgdesigner
Messages postés6Date d'inscriptionmardi 2 janvier 2007StatutMembreDernière intervention18 avril 2008 22 avril 2008 à 14:37
Aussi
Vous feriez mieux en commentant le code, surtout pour ce qui est de son utilisation (dans la la rubrique Description).
Bonne continuation
Merci
bgdesigner
Messages postés6Date d'inscriptionmardi 2 janvier 2007StatutMembreDernière intervention18 avril 2008 22 avril 2008 à 14:17
Je note 17/20
bgdesigner
Messages postés6Date d'inscriptionmardi 2 janvier 2007StatutMembreDernière intervention18 avril 2008 22 avril 2008 à 14:14
Je viens de tester ce code avec VBA.
Je signale qu'à part quelques variables à déclarer
* variables i et w comme compteurs : as integer
* iTemp as String dans Function Dec
* intlength As Integer dans Function RC4Initialize
aussi que la fonction RC4Initialize peut être simplement locale (Private) car on a besoin, niveau appel, que des 2 fonction RC4Encrypt et RC4Decrypt
Le tout marche très bien : cryptage et décryptage.
C'est d'utilisation simple et trés pratique
Merci
madjidsniper
Messages postés3Date d'inscriptionvendredi 23 novembre 2007StatutMembreDernière intervention23 novembre 2007 23 nov. 2007 à 17:40
21 janv. 2017 à 21:51
24 juil. 2008 à 09:29
'
' http://www.vbfrance.com/code.aspx?ID=41581
'
Module RC4EncryptDecrypt
Public Function RC4Encrypt(ByVal text As String, ByVal encryptkey As String) As String
Dim sbox(256) As Integer
Dim key(256) As Integer
Dim temp As Integer
Dim a As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim cipherby As Integer
Dim cipher As String = String.Empty
i = 0
j = 0
RC4Initialize(encryptkey, key, sbox)
For a = 1 To Len(text)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)
cipherby = (Asc(Mid$(text, a, 1))) Xor k
If Len(Hex(cipherby)) = 1 Then
cipher = cipher & "0" & Hex(cipherby)
Else
cipher = cipher & Hex(cipherby)
End If
Next
Return cipher
End Function
Public Function RC4Decrypt(ByVal text, ByVal encryptkey) As String
Dim sbox(256) As Integer
Dim key(256) As Integer
Dim Text2 As String = String.Empty
Dim temp As Integer
Dim a As Long
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim cipherby As Integer
Dim cipher As String = String.Empty
For w = 1 To Len(text) Step 2
Text2 = Text2 & Chr(Dec(Mid$(text, w, 2)))
Next
i = 0
j = 0
RC4Initialize(encryptkey, key, sbox)
For a = 1 To Len(Text2)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp
k = sbox((sbox(i) + sbox(j)) Mod 256)
cipherby = Asc(Mid$(Text2, a, 1)) Xor k
cipher = cipher & Chr(cipherby)
Next
Return cipher
End Function
Public Sub RC4Initialize(ByVal strPwd, ByRef key, ByRef sbox)
Dim tempSwap As Integer
Dim a As Integer
Dim b As Integer
Dim intlength As Integer
intlength = Len(strPwd)
For a = 0 To 255
key(a) = Asc(Mid$(strPwd, a Mod intlength + 1, 1))
sbox(a) = a
Next
b = 0
For a = 0 To 255
b = (b + sbox(a) + key(a)) Mod 256
tempSwap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempSwap
Next
End Sub
Public Function Dec(ByVal number) As String
Dim base As String
Dim iLen As Integer
Dim iReturn As Long
Dim iTemp As String
base = "0123456789ABCDEF"
iLen = Len(number)
For i = 0 To iLen - 1
iTemp = Mid$(number, iLen - i, 1)
iReturn = iReturn + (16 ^ i) * (InStr(1, base, iTemp) - 1)
Next
Return iReturn
End Function
End Module
22 avril 2008 à 14:37
Vous feriez mieux en commentant le code, surtout pour ce qui est de son utilisation (dans la la rubrique Description).
Bonne continuation
Merci
22 avril 2008 à 14:17
22 avril 2008 à 14:14
Je signale qu'à part quelques variables à déclarer
* variables i et w comme compteurs : as integer
* iTemp as String dans Function Dec
* intlength As Integer dans Function RC4Initialize
aussi que la fonction RC4Initialize peut être simplement locale (Private) car on a besoin, niveau appel, que des 2 fonction RC4Encrypt et RC4Decrypt
Le tout marche très bien : cryptage et décryptage.
C'est d'utilisation simple et trés pratique
Merci
23 nov. 2007 à 17:40