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 / Exemple :
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
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.