vernam chiffre

    • VB6

    Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von wersi77.

      vernam chiffre

      Public Function vernam_chiffre(original As String, skey As String) As String
      Dim i As Long
      Dim bCode As Byte
      Dim bKey As Byte
      Dim bResult As Byte

      vernam_chiffre = ""

      If Len(original) <> Len(skey) Then
      MsgBox "Text und Key haben ungleiche Längen. " & _
      "Verfahren kann nicht angewendet werden."
      Exit Function
      End If

      For i = 1 To Len(original)
      bCode = Asc(Mid(original, i, 1))
      bKey = Asc(Mid(skey, i, 1))
      bResult = bCode Xor bKey
      vernam_chiffre = vernam_chiffre & Chr(bResult)
      Next i

      End Function


      Nachteil is', dass key und original die selbe länge haben müssen, aber dafür ist dieses verfahren doch recht sicher.

      m3rLiN
      mehr als ein LIPPER kann der Mensch kaum werden! :)
      Hier hätte ich auch noch eine Möglichkeit:
      Hier funktioniert es auch mit Nachrichtenlänge <> Schlüssellänge:

      Visual Basic-Quellcode

      1. Public Function vernam_encrypt(plainText As String, key As String)
      2. Dim i As Long
      3. Dim j As Long
      4. Dim letter As String
      5. Dim letterIndex As Integer
      6. Dim keyIndex As Integer
      7. Dim cipherText As String
      8. j = 1
      9. For i = 1 To Len(plainText)
      10. letterIndex = Asc(Mid(plainText, i, 1))
      11. If Len(key) > j Then
      12. j = 1
      13. End If
      14. keyIndex = Asc(Mid(key, j, 1))
      15. j = j + 1
      16. letterIndex = letterIndex + keyIndex
      17. cipherText = cipherText + Chr(letterIndex)
      18. letterIndex = 0
      19. Next i
      20. vernam_encrypt = cipherText
      21. End Function
      22. Public Function vernam_decrypt(cipherText As String, key As String)
      23. Dim i As Long
      24. Dim j As Long
      25. Dim letter As String
      26. Dim letterIndex As Integer
      27. Dim keyIndex As Integer
      28. Dim plainText As String
      29. j = 1
      30. For i = 1 To Len(cipherText)
      31. letterIndex = Asc(Mid(cipherText, i, 1))
      32. If Len(key) > j Then
      33. j = 1
      34. End If
      35. keyIndex = Asc(Mid(key, j, 1))
      36. j = j + 1
      37. letterIndex = letterIndex - keyIndex
      38. plainText = plainText + Chr(letterIndex)
      39. letterIndex = 0
      40. Next i
      41. vernam_decrypt = plainText
      42. End Function



      Edit by Mad Andy:
      VB-Tag eingefügt

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Mad Andy“ ()

      Und hier noch der Vollständigkeit halber die Ver-/Entschlüsselungen für Caesar-Chiffre und Transposition:

      Visual Basic-Quellcode

      1. Public Function caesar_encrypt(plainText As String, key As Integer) As String
      2. Dim i As Long
      3. Dim letter As String
      4. Dim letterIndex As Integer
      5. Dim cipherText As String
      6. For i = 1 To Len(plainText)
      7. letterIndex = Asc(Mid(plainText, i, 1))
      8. letterIndex = letterIndex + key
      9. cipherText = cipherText + Chr(letterIndex)
      10. letterIndex = 0
      11. Next i
      12. caesar_encrypt = cipherText
      13. End Function
      14. Public Function caesar_decrypt(cipherText As String, key As Integer)
      15. Dim i As Long
      16. Dim letter As String
      17. Dim letterIndex As Integer
      18. Dim plainText As String
      19. For i = 1 To Len(cipherText)
      20. letterIndex = Asc(Mid(cipherText, i, 1))
      21. letterIndex = letterIndex - key
      22. plainText = plainText + Chr(letterIndex)
      23. letterIndex = 0
      24. Next i
      25. caesar_decrypt = plainText
      26. End Function
      27. Public Function trans_encrypt(plainText As String)
      28. Dim cipherText As String
      29. Dim nextSquareroot As Integer
      30. Dim plainTextIndex As Integer
      31. Dim i As Integer
      32. Dim j As Integer
      33. plainTextIndex = 1
      34. nextSquareroot = mod_main.get_next_squareroot(Len(plainText))
      35. 'MsgBox nextSquareroot
      36. ReDim square(nextSquareroot, nextSquareroot) As String
      37. If Len(plainText) / nextSquareroot <> Int(Len(plainText) / nextSquareroot) Then
      38. For i = 0 To nextSquareroot * nextSquareroot - Len(plainText)
      39. plainText = plainText + "X"
      40. Next i
      41. End If
      42. For j = 0 To nextSquareroot - 1
      43. For i = 0 To nextSquareroot - 1
      44. 'square(i, j) = "X"
      45. square(i, j) = Mid(plainText, plainTextIndex, 1)
      46. plainTextIndex = plainTextIndex + 1
      47. Next i
      48. Next j
      49. For i = 0 To nextSquareroot - 1
      50. For j = 0 To nextSquareroot - 1
      51. cipherText = cipherText + square(i, j)
      52. Next j
      53. Next i
      54. trans_encrypt = cipherText
      55. End Function
      56. Public Function trans_decrypt(cipherText As String)
      57. Dim plainText As String
      58. Dim squareroot As Integer
      59. Dim cipherTextIndex As Integer
      60. Dim i As Integer
      61. Dim j As Integer
      62. cipherTextIndex = 1
      63. squareroot = Math.Sqr(Len(cipherText))
      64. ReDim square(squareroot, squareroot) As String
      65. For j = 0 To squareroot - 1
      66. For i = 0 To squareroot - 1
      67. 'square(i, j) = "X"
      68. square(i, j) = Mid(cipherText, cipherTextIndex, 1)
      69. cipherTextIndex = cipherTextIndex + 1
      70. Next i
      71. Next j
      72. For i = 0 To squareroot - 1
      73. For j = 0 To squareroot - 1
      74. plainText = plainText + square(i, j)
      75. Next j
      76. Next i
      77. trans_decrypt = plainText
      78. End Function
      79. Public Function get_next_squareroot(number As Integer)
      80. Dim squaretest As Single
      81. Dim i As Integer
      82. For i = number To number * number
      83. squaretest = Math.Sqr(number)
      84. If squaretest = Int(squaretest) Then
      85. get_next_squareroot = squaretest
      86. Else: number = number + 1
      87. End If
      88. Next i
      89. End Function