Soyez le premier à donner votre avis sur cette source.
Snippet vu 19 140 fois - Téléchargée 42 fois
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
8 juil. 2009 à 16:09
crypt.CryptFile(fichier_source, fichier_sortie)
par contre si tu veux mettre un mot de passe pour ouvrir un fichier, tu n'as pas au bonne endroit.
20 juin 2009 à 12:20
20 juin 2009 à 00:43
le programme ne marche pas
j'ai mi tout le prog dans une classe j'ai bien intensié la classe mais lorsque je fait
RC4.Key = "mot de passe"
RC4.CryptFile("c:\monFichier.ini", "c:\monFichier.ini")
je n'obtient pas mon fichier crypté j'ai toujour monFichier.ini dans c: mais je peux l'ouvrir normalement sans mettrele mot de passe .
quelqu'un peut m'aider merci
30 juil. 2007 à 13:49
jen avais justement besoin pour crypter des données clients dans mon applications :)
Bonneprog ;D
16 juil. 2005 à 09:35
Il se peut qu'il y en ait d'autre. La base de la source etaient écrite en VB6, et je l'ai portée pour .NET. Je l'ai testé en long en large mais il y aurait sûrement de quoi l'optimiser. Concerant la ligne, je ferai une mise à jour du source lundi...
Pour la partie cryptage d'un fichier, ce n'est pas le top. Cette portion n'existait pas dans le source vb6. Pour crypter le tout, je charge le contenu du fichier en mémoire (dans une variable), crypte la variable et écrit le contenu dans un fichier de sortie... Pour les gros fichiers, ça génère des problèmes de mémoire. Il faudrait que je me repenche sur la source, pour voir si il est possible de crypter le fichier en direct, mettre le contenu du fichier en mémoire, mais en le cryptant au fur et à mesure de la lecture...
David
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.