Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Serial Generator 0.3 stabile
Forum - C# / VB.NET - Serial Generator 0.3 stabile - Pagina 2

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 9:55
Domenica, 14/03/2010
Posterò a breve una classe di esempio per generare e validare seriali. Ma nel frattempo ricordo che in questa sezione non si presentano lavori completi. Per quello c'è l'opzione di upload, almeno se si è membri.

PM Quote
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 18:22
Lunedì, 15/03/2010
Ecco l'esempio:
Codice sorgente - presumibilmente VB.NET

  1. ''' <summary>
  2. ''' Questa classe deve essere tenuta nascosta. Serve per generare un codice
  3. ''' seriale e/ verificarne la correttezza.
  4. ''' </summary>
  5. Public Class CodeGenerator
  6.     Private _CodeRank As Int16 = 8
  7.     Private _DerivingIterations As Int16 = 5
  8.     Private _SaltBytes As Byte()
  9.     Private Shared ReadOnly Rnd As Random
  10.  
  11.     ''' <summary>
  12.     ''' Definisce la forza del codice, direttamente legata alla sua lunghezza. Minimo 3.
  13.     ''' </summary>
  14.     Public Property CodeRank() As Int16
  15.         Get
  16.             Return _CodeRank
  17.         End Get
  18.         Set(ByVal value As Int16)
  19.             If value >= 3 Then
  20.                 _CodeRank = value
  21.             Else
  22.                 _CodeRank = 3
  23.             End If
  24.         End Set
  25.     End Property
  26.  
  27.     ''' <summary>
  28.     ''' Salt crittografico. Questo array di bytes viene usato per derivare il codice finito.
  29.     ''' E' il punto di forza dell'algoritmo, in quanto ogni programmatore che usa questa classe
  30.     ''' può utilizzare un array di bytes diverso da tutti gli altri, e ciò produrra anche un
  31.     ''' risultato diverso. Questo sorgente, quindi, è riusabile al 100%.
  32.     ''' </summary>
  33.     Public Property SaltBytes() As Byte()
  34.         Get
  35.             Return _SaltBytes
  36.         End Get
  37.         Set(ByVal value As Byte())
  38.             _SaltBytes = value
  39.         End Set
  40.     End Property
  41.  
  42.     ''' <summary>
  43.     ''' Numero di iterazioni compiute dall'algoritmo di derivazione. Anche questo è un fattore
  44.     ''' cambiando il quale cambia il risultato.
  45.     ''' </summary>
  46.     Public Property DerivingIterations() As Int16
  47.         Get
  48.             Return _DerivingIterations
  49.         End Get
  50.         Set(ByVal value As Int16)
  51.             If value >= 2 Then
  52.                 _DerivingIterations = value
  53.             Else
  54.                 _DerivingIterations = 2
  55.             End If
  56.         End Set
  57.     End Property
  58.  
  59.  
  60.     Shared Sub New()
  61.         Rnd = New Random()
  62.     End Sub
  63.  
  64.  
  65.     ''' <summary>
  66.     ''' Randomizza i parametri del generatore. NOTA BENE: per verificare un codice seriale, è
  67.     ''' necessario che il generatore mantenga le stesse proprietà dell'oggetto che ha creato il codice.
  68.     ''' Quindi non bisogna abusare di questa funzione.
  69.     ''' </summary>
  70.     Public Sub Randomize()
  71.         Me.CodeRank = Rnd.Next(6, 14)
  72.         Me.DerivingIterations = Rnd.Next(2, 10)
  73.  
  74.         Dim Bytes(Rnd.Next(10, 40)) As Byte
  75.         For I As Int16 = 0 To Bytes.Length - 1
  76.             Bytes(I) = Rnd.Next(0, 256)
  77.         Next
  78.         Me.SaltBytes = Bytes
  79.     End Sub
  80.  
  81.     ''' <summary>
  82.     ''' Genera un codice seriale esadecimale casuale basandosi sui dati immessi nelle proprietà
  83.     ''' della classe.
  84.     ''' </summary>
  85.     Public Function GenerateCode() As String
  86.         Dim Derive As Rfc2898DeriveBytes
  87.         Dim DerivedBytes() As Byte
  88.         Dim Result As New System.Text.StringBuilder
  89.         Dim RandomSequence(Me.CodeRank - 1) As Byte
  90.  
  91.         For I As Int16 = 0 To RandomSequence.Length - 1
  92.             RandomSequence(I) = Rnd.Next(0, 256)
  93.         Next
  94.  
  95.         Derive = New Rfc2898DeriveBytes(RandomSequence, Me.SaltBytes, Me.DerivingIterations)
  96.         DerivedBytes = Derive.GetBytes(Me.CodeRank)
  97.  
  98.         For I As Int16 = 0 To Me.CodeRank - 1
  99.             Result.AppendFormat("{0:X2}{1:X2}", RandomSequence(I), DerivedBytes(I))
  100.         Next
  101.  
  102.         Derive = Nothing
  103.         DerivedBytes = Nothing
  104.         RandomSequence = Nothing
  105.  
  106.         Return Result.ToString()
  107.     End Function
  108.  
  109.     ''' <summary>
  110.     ''' Controlla se il codice immesso è valido. Restituisce True in caso di validità.
  111.     ''' </summary>
  112.     ''' <param name="Code">Codice seriale proposto per la verifica.</param>
  113.     Public Function ValidateCode(ByVal Code As String) As Boolean
  114.         Dim OriginalCode As String = Regex.Replace(Code, "[\w\W-[A-Z0-9]]", "", RegexOptions.IgnoreCase)
  115.         Dim RandomSequence(Me.CodeRank - 1), CodeBytes(Me.CodeRank - 1), DerivedBytes() As Byte
  116.         Dim Derive As Rfc2898DeriveBytes
  117.  
  118.         If OriginalCode.Length <> Me.CodeRank * 4 Then
  119.             Return False
  120.         End If
  121.  
  122.         For I As Int16 = 0 To Me.CodeRank - 1
  123.             Try
  124.                 RandomSequence(I) = Int32.Parse(OriginalCode.Substring(I * 4, 2), Globalization.NumberStyles.AllowHexSpecifier)
  125.                 CodeBytes(I) = Int32.Parse(OriginalCode.Substring(I * 4 + 2, 2), Globalization.NumberStyles.AllowHexSpecifier)
  126.             Catch Ex As Exception
  127.                 Return False
  128.             End Try
  129.         Next
  130.  
  131.         Derive = New Rfc2898DeriveBytes(RandomSequence, Me.SaltBytes, Me.DerivingIterations)
  132.         DerivedBytes = Derive.GetBytes(Me.CodeRank)
  133.  
  134.         OriginalCode = Nothing
  135.         Derive = Nothing
  136.         RandomSequence = Nothing
  137.  
  138.         For I As Int16 = 0 To Me.CodeRank - 1
  139.             If CodeBytes(I) <> DerivedBytes(I) Then
  140.                 Return False
  141.             End If
  142.         Next
  143.  
  144.         CodeBytes = Nothing
  145.         DerivedBytes = Nothing
  146.  
  147.         Return True
  148.     End Function
  149.  
  150.     ''' <summary>
  151.     ''' Spezza il codice in parti di lunghezza specificata.
  152.     ''' </summary>
  153.     Public Shared Function SplitCode(ByVal Code As String, ByVal ParamArray Lengths() As Int16) As String()
  154.         Dim CodeReader As New IO.StringReader(Code)
  155.         Dim Result As New List(Of String)
  156.         Dim Buffer() As Char
  157.  
  158.         For Each Len As Int16 In Lengths
  159.             ReDim Buffer(Len - 1)
  160.             If CodeReader.ReadBlock(Buffer, 0, Len) > 0 Then
  161.                 Result.Add(Buffer)
  162.             Else
  163.                 Exit For
  164.             End If
  165.         Next
  166.  
  167.         Return Result.ToArray()
  168.     End Function
  169.  
  170. End Class


PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo