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
Crip - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Public Class Form1
  2.     Dim currentfile As String
  3.     Public Const Crypt As Boolean = True
  4.     Public Const Decrypt As Boolean = False
  5.     Dim mode As Boolean
  6.     Public Function SommaChar(ByVal s As String) As Int64
  7.         Dim i As Int32
  8.         Dim result As Int64
  9.  
  10.         For i = 0 To s.Length - 1
  11.             If i Mod 2 = 0 Then
  12.                 result = result - Asc(s.Chars(i))
  13.             Else
  14.                 result = result + Asc(s.Chars(i))
  15.             End If
  16.         Next
  17.         Return Math.Abs(result)
  18.     End Function
  19.  
  20.     Public Function AlgoritmoSemplice(ByVal cripmode As Boolean, ByVal s As String) As String
  21.         Dim i As Int32
  22.         Dim result As String
  23.         Dim c As Char
  24.  
  25.         If cripmode = Crypt Then
  26.             For i = 0 To len(s) - 1
  27.                 c = Chr(255 - Asc(s.Chars(i)))
  28.                 result = result & c
  29.             Next
  30.             Return result
  31.         Else
  32.             For i = 0 To s.Length - 1
  33.                 c = Chr(255 - Asc(s.Chars(i)))
  34.                 result = result & c
  35.             Next
  36.             Return result
  37.         End If
  38.     End Function
  39.  
  40.     Public Function AlgoritmoMedio(ByVal cripmode As Boolean, ByVal s As String) As String
  41.         Dim i As Int32
  42.         Dim result As String
  43.         Dim c As Char
  44.  
  45.         If cripmode = Crypt Then
  46.             For i = 0 To s.Length - 1
  47.                 If i Mod 2 = 0 Then
  48.                     c = Chr(Asc(s.Chars(i)) + i)
  49.                 Else
  50.                     c = Chr(Asc(s.Chars(i)) - i)
  51.                 End If
  52.                 result = result & c
  53.             Next
  54.             Return result
  55.         Else
  56.             For i = 0 To s.Length - 1
  57.                 If i Mod 2 = 0 Then
  58.                     c = Chr(Asc(s.Chars(i)) - i)
  59.                 Else
  60.                     c = Chr(Asc(s.Chars(i)) + i)
  61.                 End If
  62.                 result = result & c
  63.             Next
  64.             Return result
  65.         End If
  66.     End Function
  67.  
  68.     Public Function AlgoritmoDifficile(ByVal cripmode As Boolean, ByVal s As String) As String
  69.         Dim i As Int32
  70.         Dim result As String
  71.         Dim c As Char
  72.  
  73.         If cripmode = Crypt Then
  74.             For i = 0 To s.Length - 1
  75.                 If Math.Sin(i * 90) > 0 Then
  76.                     c = Chr((Asc(s.Chars(i)) + (s.Length - 1 + i)))
  77.                 Else
  78.                     c = Chr((Asc(s.Chars(i)) - (s.Length - 1 + i)))
  79.                 End If
  80.                 result = result & c
  81.             Next
  82.             Return result
  83.         Else
  84.             For i = 0 To s.Length - 1
  85.                 If Math.Sin(i * 90) > 0 Then
  86.                     c = Chr(Asc(s.Chars(i)) - (s.Length - 1 + i))
  87.                 Else
  88.                     c = Chr(Asc(s.Chars(i)) + (s.Length - 1 + i))
  89.                 End If
  90.             Next
  91.             Return result
  92.         End If
  93.     End Function
  94.  
  95.     Public Function AlgoritmoAChiave(ByVal cripmode As Boolean, ByVal s As String, ByVal key As String) As String
  96.         Dim i As Int32
  97.         Dim result As String
  98.         Dim c As Char
  99.         Dim somma As Int64
  100.  
  101.         somma = SommaChar(key)
  102.  
  103.         If cripmode = Crypt Then
  104.             For i = 0 To s.Length - 1
  105.                 c = Chr((Asc(s.Chars(i)) + somma) Mod 255)
  106.                 result = result & c
  107.             Next
  108.             Return result
  109.         Else
  110.             For i = 0 To s.Length - 1
  111.                 c = Chr(Asc(s.Chars(i)) - (somma Mod 255))
  112.                 result = result & c
  113.             Next
  114.             Return result
  115.         End If
  116.     End Function
  117.  
  118.     Private Sub sfoglia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sfoglia.Click
  119.         If apri.ShowDialog = DialogResult.OK Then
  120.             nome_f.Text = apri.FileName
  121.             currentfile = nome_f.Text
  122.         End If
  123.     End Sub
  124.  
  125.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  126.         mode = Crypt
  127.         esegui()
  128.     End Sub
  129.  
  130.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  131.         mode = Decrypt
  132.         esegui()
  133.     End Sub
  134.  
  135.     Public Sub esegui()
  136.         Dim r As New System.IO.StreamReader(currentfile)
  137.         Dim w As New System.IO.StreamWriter(currentfile & ".cry")
  138.         Dim s, o, chi As String
  139.         Dim index, size As Double
  140.  
  141.         If algor.SelectedIndex = 3 Then
  142.             chi = InputBox("Inserire la chiave di criptazione/decriptazione:", "Chiave")
  143.         End If
  144.         size = FileLen(currentfile)
  145.         index = 0
  146.         While index < size
  147.             s = r.ReadLine
  148.             Select Case algor.SelectedIndex
  149.                 Case 0 : o = AlgoritmoSemplice(mode, s)
  150.                 Case 1 : o = AlgoritmoMedio(mode, s)
  151.                 Case 2 : o = AlgoritmoDifficile(mode, s)
  152.                 Case 3 : o = AlgoritmoAChiave(mode, s, chi)
  153.             End Select
  154.             w.WriteLine(o)
  155.             index += Len(s)
  156.             ProgressBar1.Value = 100 * index / size
  157.         End While
  158.         r.Close()
  159.         w.Close()
  160.         r = Nothing
  161.         w = Nothing
  162.         MsgBox("Operazione completata. File: " & currentfile & ".cry", MsgBoxStyle.Information, "Fine")
  163.     End Sub
  164. End Class