|
begin process at 2008 07 06 02:42:45
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
CRYPTAGE / DECRYPTAGE RC4 POUR VB .NET
Information sur la source
Description
Hello, J'ai porté en VB .Net la classe de cryptage RC4 de lolo32. J'y ai ajouté quelques fonctionnalités en plus : -cryptage d'un fichier dans un variable - cryptage d'un fichier dans un autre fichier - décryptage d'un fichier dans une variable - décryptage d'un fichier dans un autre fichier - génération aléatoire d'une clef de cryptage
Source
- Option Explicit On
-
- Imports System.IO
- Imports System.Text
-
- Public Class RC4
- '/*******************************************/
- '/* Mise à jour v_1.0.1
-
- 'Classe de cryptage réécrite pour VB .NET à partir du code de
- 'lolo32 (http://www.vbfrance.com/article.aspx?Val=5278)
- ' Pour toute question dcampillo@gmail.com
-
- #Region "Const"
- Private _version As String = "1.0.1"
- #End Region
-
- #Region "Private var"
- Private S(255) As Integer
- Private cls_Key As String
- #End Region
-
- #Region "Property"
- Public Property Key() As String
- Get
- Return cls_Key
- End Get
- Set(ByVal Key As String)
- cls_Key = Key
- End Set
- End Property
-
- Public ReadOnly Property Version() As String
- Get
- Return _version
- End Get
- End Property
- #End Region
-
- Public Sub New()
-
- End Sub
-
- Public Sub New(ByVal Key As String)
- Me.Key = Key
- End Sub
-
- Public Function Crypt(ByVal Param As String) As String
- Dim ParamLen As Integer = Param.Length
- Dim C As Integer
- Dim T As Integer
- Dim i As Integer
- Dim j As Integer
-
- Dim oStringBuilder As New StringBuilder
-
- CreateKeyArray()
-
- For C = 0 To ParamLen - 1
- i = (i + 1) And 255
- j = (j + S(i)) And 255
- T = S(i)
- S(i) = S(j)
- S(j) = T
-
- T = (S(i) + S(j)) And 255
-
- oStringBuilder.Append(Chr(Asc(Param.Substring(C, 1)) Xor S(T)))
- Next C
-
- Return oStringBuilder.ToString
- End Function
-
- Public Overloads Function CryptFile(ByVal FilePath As String) As String
- Dim oFileInfo As New FileInfo(FilePath)
-
- If oFileInfo.Exists Then
- Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
- Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
-
- If oFileStream.CanRead Then
- Dim FileContent As String = oFileReader.ReadToEnd
- Dim FileLen As Int32 = FileContent.Length
- Dim C As Integer
- Dim T As Integer
- Dim i As Integer
- Dim j As Integer
-
- Dim oStringBuilder As New StringBuilder
-
- CreateKeyArray()
-
- For C = 0 To FileLen - 1
- i = (i + 1) And 255
- j = (j + S(i)) And 255
- T = S(i)
- S(i) = S(j)
- S(j) = T
-
- T = (S(i) + S(j)) And 255
-
- oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
- Next C
-
- Return oStringBuilder.ToString
- Else
- Throw New Exception("Impossible de lire le fichier " & FilePath)
-
- End If
- Else
- Throw New Exception("Impossible de trouver le fichier " & FilePath)
- End If
- End Function
-
- Public Overloads Function CryptFile(ByVal FilePath As String, ByVal OutPutFile As String) As Long
- Dim oFileInfo As New FileInfo(FilePath)
-
- If oFileInfo.Exists Then
- Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
- Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
-
- If oFileStream.CanRead Then
- Dim FileContent As String = oFileReader.ReadToEnd
- oFileStream.Close()
- oFileReader.Close()
-
- Dim ParamLen As Int32 = FileContent.Length
- Dim C As Integer
- Dim T As Integer
- Dim i As Integer
- Dim j As Integer
-
- Dim oStringBuilder As New StringBuilder
-
- CreateKeyArray()
-
- For C = 0 To ParamLen - 1
- i = (i + 1) And 255
- j = (j + S(i)) And 255
- T = S(i)
- S(i) = S(j)
- S(j) = T
-
- T = (S(i) + S(j)) And 255
-
- oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
- Next C
-
- Dim oWriteStream As New FileStream(OutPutFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
- Dim oFileWriter As New StreamWriter(oWriteStream, System.Text.Encoding.Default)
-
- Try
- oFileWriter.Write(oStringBuilder.ToString)
- Catch err As Exception
- Throw New Exception("Impossible d'écrire le fichier de sortie " & OutPutFile)
- End Try
-
- oFileWriter.Close()
-
- Return 0
-
- Else
- Throw New Exception("Impossible de lire le fichier " & FilePath)
- End If
- Else
- Throw New Exception("Impossible de trouver le fichier " & FilePath)
- End If
- End Function
-
- Public Function Decrypt(ByVal Param As String) As String
-
-
-
- Dim ParamLen As Integer = Len(Param)
- Dim C As Integer
- Dim T As Integer
- Dim i As Integer
- Dim j As Integer
-
- Dim oStringBuilder As New StringBuilder
-
- CreateKeyArray()
-
- For C = 0 To ParamLen - 1
- i = (i + 1) And 255
- j = (j + S(i)) And 255
- T = S(i)
- S(i) = S(j)
- S(j) = T
-
- T = (S(i) + S(j)) And 255
-
- oStringBuilder.Append(Chr(Asc(Param.Substring(C, 1)) Xor S(T)))
- Next C
- Return oStringBuilder.ToString
- End Function
-
- Public Overloads Function DecryptFile(ByVal FilePath As String) As String
-
- Dim oFileInfo As New FileInfo(FilePath)
-
- If oFileInfo.Exists Then
- Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
- Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
-
- If oFileStream.CanRead Then
- Dim FileContent As String = oFileReader.ReadToEnd
- Dim FileLen As Int32 = FileContent.Length
- Dim C As Integer
- Dim T As Integer
- Dim i As Integer
- Dim j As Integer
-
- Dim oStringBuilder As New StringBuilder
-
- CreateKeyArray()
-
- For C = 0 To FileLen - 1
- i = (i + 1) And 255
- j = (j + S(i)) And 255
- T = S(i)
- S(i) = S(j)
- S(j) = T
-
- T = (S(i) + S(j)) And 255
-
- oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
- Next C
- Return oStringBuilder.ToString
- Else
- Throw New Exception("Impossible de lire le fichier " & FilePath)
- End If
- Else
- Throw New Exception("Impossible de trouver le fichier " & FilePath)
- End If
- End Function
-
- Public Overloads Function DecryptFile(ByVal FilePath As String, ByVal OutPutFile As String) As Long
-
- Dim oFileInfo As New FileInfo(FilePath)
-
- If oFileInfo.Exists Then
- Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
- Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
-
- If oFileStream.CanRead Then
- Dim FileContent As String = oFileReader.ReadToEnd
- Dim FileLen As Int32 = FileContent.Length
- Dim C As Integer
- Dim T As Integer
- Dim i As Integer
- Dim j As Integer
-
- Dim oStringBuilder As New StringBuilder
-
- CreateKeyArray()
-
- For C = 0 To FileLen - 1
- i = (i + 1) And 255
- j = (j + S(i)) And 255
- T = S(i)
- S(i) = S(j)
- S(j) = T
-
- T = (S(i) + S(j)) And 255
-
- oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
- Next C
-
- Dim oWriteStream As New FileStream(OutPutFile, FileMode.Create, FileAccess.Write, FileShare.None)
- Dim oFileWriter As New StreamWriter(oWriteStream, System.Text.Encoding.Default)
-
- Try
- oFileWriter.Write(oStringBuilder.ToString)
- Catch err As Exception
- Throw New Exception("Impossible d'écrire le fichier de sortie " & OutPutFile)
- End Try
- oFileWriter.Close()
- Return 0
- Else
- Throw New Exception("Impossible de lire le fichier " & FilePath)
- End If
- Else
- Throw New Exception("Impossible de trouver le fichier " & FilePath)
- End If
- End Function
-
- Private Sub CreateKeyArray()
- Dim KeyLen As Integer
- Dim T As Integer
- Dim i As Integer = 0
- Dim j As Integer = 0
- Dim lItem As Integer
-
- If Key.Trim.Length > 0 Then
-
- KeyLen = cls_Key.Length
-
- For i = 0 To 255
- S(i) = i
- Next i
-
- For i = 0 To 255
- j = (j + S(i) + Asc(cls_Key.Substring(i Mod KeyLen, 1)) And 255)
- T = S(i)
- S(i) = S(j)
- S(j) = T
- Next i
- i = 0
- j = 0
- Else
- Throw New System.ArgumentException("La clef est vide")
- End If
-
- End Sub
-
- Public Overloads Function GenerateKey() As String
- Dim keyBuffer As New StringBuilder
- Dim KeyLen As Short = 255
- Dim i As Integer
-
- Randomize(Now.Millisecond)
-
-
- Do Until i >= KeyLen
- keyBuffer.Append(Chr(CInt(255 * Rnd())))
- i += 1
- Loop
-
- Me.Key = keyBuffer.ToString
- Return keyBuffer.ToString
-
- End Function
-
- Public Overloads Function GenerateKey(ByVal KeyLen As Integer) As String
- Dim keyBuffer As New StringBuilder
- Dim i As Integer
-
- Randomize(Now.Millisecond)
-
- Do Until i >= KeyLen
- keyBuffer.Append(Chr(CInt(255 * Rnd())))
- i += 1
- Loop
-
- Me.Key = keyBuffer.ToString
- Return keyBuffer.ToString
-
- End Function
-
- Public Overloads Function GenerateKey(ByVal KeyLen As Integer, ByVal Readable As Boolean) As String
- Dim keyBuffer As New StringBuilder
- Dim AvailableChar As String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- Dim lenAvailableChar As Integer = AvailableChar.Length - 1
- Dim i As Integer
-
- Randomize(Now.Millisecond)
-
- If Readable = True Then
- Do Until i >= KeyLen
- keyBuffer.Append(AvailableChar.Substring(CType(lenAvailableChar * Rnd(), Integer), 1))
- i += 1
- Loop
- Else
- Do Until i >= KeyLen
- keyBuffer.Append(Chr(CInt(255 * Rnd())))
- i += 1
- Loop
- End If
- Me.Key = keyBuffer.ToString
- Return keyBuffer.ToString
-
- End Function
-
- Public Overloads Function GenerateKey(ByVal KeyLen As Integer, ByVal AvailableChar As String) As String
- Dim keyBuffer As New StringBuilder
- Dim lenAvailableChar As Integer = AvailableChar.Length - 1
- Dim i As Integer
-
- Randomize(Now.Millisecond)
-
- Do Until i >= KeyLen
- keyBuffer.Append(AvailableChar.Substring(CType(lenAvailableChar * Rnd(), Integer), 1))
- i += 1
- Loop
-
- Me.Key = keyBuffer.ToString
- Return keyBuffer.ToString
-
- End Function
-
- End Class
Option Explicit On
Imports System.IO
Imports System.Text
Public Class RC4
'/*******************************************/
'/* Mise à jour v_1.0.1
'Classe de cryptage réécrite pour VB .NET à partir du code de
'lolo32 (http://www.vbfrance.com/article.aspx?Val=5278)
' Pour toute question dcampillo@gmail.com
#Region "Const"
Private _version As String = "1.0.1"
#End Region
#Region "Private var"
Private S(255) As Integer
Private cls_Key As String
#End Region
#Region "Property"
Public Property Key() As String
Get
Return cls_Key
End Get
Set(ByVal Key As String)
cls_Key = Key
End Set
End Property
Public ReadOnly Property Version() As String
Get
Return _version
End Get
End Property
#End Region
Public Sub New()
End Sub
Public Sub New(ByVal Key As String)
Me.Key = Key
End Sub
Public Function Crypt(ByVal Param As String) As String
Dim ParamLen As Integer = Param.Length
Dim C As Integer
Dim T As Integer
Dim i As Integer
Dim j As Integer
Dim oStringBuilder As New StringBuilder
CreateKeyArray()
For C = 0 To ParamLen - 1
i = (i + 1) And 255
j = (j + S(i)) And 255
T = S(i)
S(i) = S(j)
S(j) = T
T = (S(i) + S(j)) And 255
oStringBuilder.Append(Chr(Asc(Param.Substring(C, 1)) Xor S(T)))
Next C
Return oStringBuilder.ToString
End Function
Public Overloads Function CryptFile(ByVal FilePath As String) As String
Dim oFileInfo As New FileInfo(FilePath)
If oFileInfo.Exists Then
Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
If oFileStream.CanRead Then
Dim FileContent As String = oFileReader.ReadToEnd
Dim FileLen As Int32 = FileContent.Length
Dim C As Integer
Dim T As Integer
Dim i As Integer
Dim j As Integer
Dim oStringBuilder As New StringBuilder
CreateKeyArray()
For C = 0 To FileLen - 1
i = (i + 1) And 255
j = (j + S(i)) And 255
T = S(i)
S(i) = S(j)
S(j) = T
T = (S(i) + S(j)) And 255
oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
Next C
Return oStringBuilder.ToString
Else
Throw New Exception("Impossible de lire le fichier " & FilePath)
End If
Else
Throw New Exception("Impossible de trouver le fichier " & FilePath)
End If
End Function
Public Overloads Function CryptFile(ByVal FilePath As String, ByVal OutPutFile As String) As Long
Dim oFileInfo As New FileInfo(FilePath)
If oFileInfo.Exists Then
Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
If oFileStream.CanRead Then
Dim FileContent As String = oFileReader.ReadToEnd
oFileStream.Close()
oFileReader.Close()
Dim ParamLen As Int32 = FileContent.Length
Dim C As Integer
Dim T As Integer
Dim i As Integer
Dim j As Integer
Dim oStringBuilder As New StringBuilder
CreateKeyArray()
For C = 0 To ParamLen - 1
i = (i + 1) And 255
j = (j + S(i)) And 255
T = S(i)
S(i) = S(j)
S(j) = T
T = (S(i) + S(j)) And 255
oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
Next C
Dim oWriteStream As New FileStream(OutPutFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Dim oFileWriter As New StreamWriter(oWriteStream, System.Text.Encoding.Default)
Try
oFileWriter.Write(oStringBuilder.ToString)
Catch err As Exception
Throw New Exception("Impossible d'écrire le fichier de sortie " & OutPutFile)
End Try
oFileWriter.Close()
Return 0
Else
Throw New Exception("Impossible de lire le fichier " & FilePath)
End If
Else
Throw New Exception("Impossible de trouver le fichier " & FilePath)
End If
End Function
Public Function Decrypt(ByVal Param As String) As String
Dim ParamLen As Integer = Len(Param)
Dim C As Integer
Dim T As Integer
Dim i As Integer
Dim j As Integer
Dim oStringBuilder As New StringBuilder
CreateKeyArray()
For C = 0 To ParamLen - 1
i = (i + 1) And 255
j = (j + S(i)) And 255
T = S(i)
S(i) = S(j)
S(j) = T
T = (S(i) + S(j)) And 255
oStringBuilder.Append(Chr(Asc(Param.Substring(C, 1)) Xor S(T)))
Next C
Return oStringBuilder.ToString
End Function
Public Overloads Function DecryptFile(ByVal FilePath As String) As String
Dim oFileInfo As New FileInfo(FilePath)
If oFileInfo.Exists Then
Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
If oFileStream.CanRead Then
Dim FileContent As String = oFileReader.ReadToEnd
Dim FileLen As Int32 = FileContent.Length
Dim C As Integer
Dim T As Integer
Dim i As Integer
Dim j As Integer
Dim oStringBuilder As New StringBuilder
CreateKeyArray()
For C = 0 To FileLen - 1
i = (i + 1) And 255
j = (j + S(i)) And 255
T = S(i)
S(i) = S(j)
S(j) = T
T = (S(i) + S(j)) And 255
oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
Next C
Return oStringBuilder.ToString
Else
Throw New Exception("Impossible de lire le fichier " & FilePath)
End If
Else
Throw New Exception("Impossible de trouver le fichier " & FilePath)
End If
End Function
Public Overloads Function DecryptFile(ByVal FilePath As String, ByVal OutPutFile As String) As Long
Dim oFileInfo As New FileInfo(FilePath)
If oFileInfo.Exists Then
Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim oFileReader As New StreamReader(oFileStream, System.Text.Encoding.Default)
If oFileStream.CanRead Then
Dim FileContent As String = oFileReader.ReadToEnd
Dim FileLen As Int32 = FileContent.Length
Dim C As Integer
Dim T As Integer
Dim i As Integer
Dim j As Integer
Dim oStringBuilder As New StringBuilder
CreateKeyArray()
For C = 0 To FileLen - 1
i = (i + 1) And 255
j = (j + S(i)) And 255
T = S(i)
S(i) = S(j)
S(j) = T
T = (S(i) + S(j)) And 255
oStringBuilder.Append(Chr(Asc(FileContent.Substring(C, 1)) Xor S(T)))
Next C
Dim oWriteStream As New FileStream(OutPutFile, FileMode.Create, FileAccess.Write, FileShare.None)
Dim oFileWriter As New StreamWriter(oWriteStream, System.Text.Encoding.Default)
Try
oFileWriter.Write(oStringBuilder.ToString)
Catch err As Exception
Throw New Exception("Impossible d'écrire le fichier de sortie " & OutPutFile)
End Try
oFileWriter.Close()
Return 0
Else
Throw New Exception("Impossible de lire le fichier " & FilePath)
End If
Else
Throw New Exception("Impossible de trouver le fichier " & FilePath)
End If
End Function
Private Sub CreateKeyArray()
Dim KeyLen As Integer
Dim T As Integer
Dim i As Integer = 0
Dim j As Integer = 0
Dim lItem As Integer
If Key.Trim.Length > 0 Then
KeyLen = cls_Key.Length
For i = 0 To 255
S(i) = i
Next i
For i = 0 To 255
j = (j + S(i) + Asc(cls_Key.Substring(i Mod KeyLen, 1)) And 255)
T = S(i)
S(i) = S(j)
S(j) = T
Next i
i = 0
j = 0
Else
Throw New System.ArgumentException("La clef est vide")
End If
End Sub
Public Overloads Function GenerateKey() As String
Dim keyBuffer As New StringBuilder
Dim KeyLen As Short = 255
Dim i As Integer
Randomize(Now.Millisecond)
Do Until i >= KeyLen
keyBuffer.Append(Chr(CInt(255 * Rnd())))
i += 1
Loop
Me.Key = keyBuffer.ToString
Return keyBuffer.ToString
End Function
Public Overloads Function GenerateKey(ByVal KeyLen As Integer) As String
Dim keyBuffer As New StringBuilder
Dim i As Integer
Randomize(Now.Millisecond)
Do Until i >= KeyLen
keyBuffer.Append(Chr(CInt(255 * Rnd())))
i += 1
Loop
Me.Key = keyBuffer.ToString
Return keyBuffer.ToString
End Function
Public Overloads Function GenerateKey(ByVal KeyLen As Integer, ByVal Readable As Boolean) As String
Dim keyBuffer As New StringBuilder
Dim AvailableChar As String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim lenAvailableChar As Integer = AvailableChar.Length - 1
Dim i As Integer
Randomize(Now.Millisecond)
If Readable = True Then
Do Until i >= KeyLen
keyBuffer.Append(AvailableChar.Substring(CType(lenAvailableChar * Rnd(), Integer), 1))
i += 1
Loop
Else
Do Until i >= KeyLen
keyBuffer.Append(Chr(CInt(255 * Rnd())))
i += 1
Loop
End If
Me.Key = keyBuffer.ToString
Return keyBuffer.ToString
End Function
Public Overloads Function GenerateKey(ByVal KeyLen As Integer, ByVal AvailableChar As String) As String
Dim keyBuffer As New StringBuilder
Dim lenAvailableChar As Integer = AvailableChar.Length - 1
Dim i As Integer
Randomize(Now.Millisecond)
Do Until i >= KeyLen
keyBuffer.Append(AvailableChar.Substring(CType(lenAvailableChar * Rnd(), Integer), 1))
i += 1
Loop
Me.Key = keyBuffer.ToString
Return keyBuffer.ToString
End Function
End Class
Historique
- 19 juillet 2005 22:54:59 :
- Mise à jour version 1.0.1
--------------------------
- Modification du code de la sub CreateKeyArray()
- Utilisation des Integer en place des long => Les longs de VB6 sont devenu les integer de .NET ;-)
- Modification du code en enlevant toutes les convertions Integer -> Long ou Long -> Integer (le cryptage doit être un poil plus rapide ) ;-)
- Ajout d'une propriété Version de type string qui retourne la version de la classe
Sources de la même categorie
Commentaires
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | | | |
|
|
|