Guest User

Untitled

a guest
Mar 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. Sub Main()
  2. Dim Key As String
  3. Dim Msg As String
  4. Dim EncryptMsg As String
  5. Dim DecryptMsg As String
  6.  
  7. Key = "мечта"
  8. Msg = "реализация шифра методом вертикальной перестановки с ключом мечта"
  9.  
  10. EncryptMsg = VerticalShuffleEncrypt(Key, Msg)
  11. DecryptMsg = VerticalShuffleDecrypt(Key, EncryptMsg)
  12.  
  13. Cells(1, 1).Value = Msg
  14. Cells(2, 1).Value = EncryptMsg
  15. Cells(3, 1).Value = DecryptMsg
  16. End Sub
  17.  
  18. Function VerticalShuffleEncrypt(ByVal Key As String, ByVal Msg As String) As String
  19. Dim KeyArr As Object
  20. Dim L As Long
  21. Dim Col As String
  22. Dim Res As String
  23.  
  24. L = Len(Msg) Len(Key)
  25. If (Len(Msg) Mod Len(Key) <> 0) Then
  26. L = L + 1
  27. End If
  28.  
  29. Set KeyArr = CreateObject("System.Collections.ArrayList")
  30.  
  31. For I = 1 To Len(Key)
  32. KeyArr.Add Mid(Key, I, 1)
  33. Next I
  34.  
  35. KeyArr.Sort
  36.  
  37. Res = ""
  38. For Each K In KeyArr
  39. I = InStr(1, Key, K, vbTextCompare)
  40. Col = ""
  41. 'For J = I To Len(Msg) Step L
  42. ' Res = Res & Mid(Msg, J, 1)
  43. 'Next J
  44. For J = I To Len(Msg) Step Len(Key)
  45. Col = Col & Mid(Msg, J, 1)
  46. Next J
  47. If (Len(Col) <> L) Then
  48. Col = Col & " "
  49. End If
  50. Res = Res & Col
  51. Next
  52.  
  53. VerticalShuffleEncrypt = Res
  54. End Function
  55.  
  56. Function VerticalShuffleDecrypt(ByVal Key As String, ByVal Msg As String) As String
  57. Dim KeyArr As Object
  58. Dim IndexArr As Object
  59. Dim L As Long
  60. Dim Res As String
  61.  
  62. L = Len(Msg) Len(Key)
  63. If (Len(Msg) Mod Len(Key) <> 0) Then
  64. L = L + 1
  65. End If
  66.  
  67. Set KeyArr = CreateObject("System.Collections.ArrayList")
  68. Set IndexArr = CreateObject("System.Collections.ArrayList")
  69.  
  70. For I = 1 To Len(Key)
  71. KeyArr.Add Mid(Key, I, 1)
  72. Next I
  73.  
  74. KeyArr.Sort
  75.  
  76. For I = 1 To Len(Key)
  77. IndexArr.Add KeyArr.IndexOf(Mid(Key, I, 1), 0) + 1
  78. Next I
  79.  
  80. Res = ""
  81. For I = 1 To L
  82. For Each Index In IndexArr
  83. Res = Res & Mid(Msg, ((Index - 1) * L) + I, 1)
  84. Next
  85. Next I
  86.  
  87. VerticalShuffleDecrypt = Res
  88. End Function
  89.  
  90. м е ч т а
  91.  
  92. 3 2 5 4 1
  93.  
  94. р е а л и
  95. з а ц и я
  96. ш и ф р
  97. а м е т
  98. о д о м
  99. в е р т и
  100. к а л ь н
  101. о й п е
  102. р е с т а
  103. н о в к и
  104. с к л
  105. ю ч о м
  106. м е ч т а
Add Comment
Please, Sign In to add comment