อาจารย์คะ ถ้าหนูจะแปลงค่า โดยเทียบ encode กับ longkey มาเทียบกันทีละตัว โดยใช้ความยาวของตัวแปร message ที่ยังไม่ได้ตัดช่องว่าง
ซึ่งมันจะไปเก็บค่าไว้ในตัวแปร decode
จากตัวอย่าง
message = the best
Longkey = POPPOPP
encode = IVTQSHI
จะได้ decode ออกมาเป็น the best
ซึ่งความยาว Longkey = 7
แต่ความยาว message = 8
แต่มัน debug คะ ช่วยแก้ไขด้วยนะคะ
Code: Select all
Public Sub Crypto01()
Dim Key As String, Message As String, PlainText As String, Longkey As String, DecodeSpace As String, _
Decode As String
Dim i As Integer, p As Integer
Key = InputBox("Enter the key of encryption")
Key = UCase(Key)
Message = InputBox("Enter the message that you want to send")
PlainText = Replace(Message, " ", "")
Longkey = Left(Application.WorksheetFunction.Rept(Key, 10), Len(PlainText))
MsgBox "Key = " & Key
MsgBox "PlainText = " & PlainText
MsgBox "Longkey = " & Longkey
For i = 1 To Len(Longkey)
With Sheets("Sheet1")
t = Application.Index(.Range("A2:Z27") _
, Application.Match(Mid(PlainText, i, 1), .Range("A2:A27"), 0) _
, Application.Match(Mid(Longkey, i, 1), .Range("A1:Z1"), 0))
End With
Encode = Encode & t
Next i
MsgBox Encode
For p = 1 To Len(Message)
With Sheets("Sheet1")
If DecodeSpace = " " Then
Decode = Decode + " "
i = i - 1
Else
i = Application.Index(.Range("B2:Z27") _
, Application.Match(Mid(Encode, i, 1), .Range("B2:Z27"), 0))
, Application.Match(Mid(Message, i, 1), .Range("A1:Z1"), 0))
Decode = Decode & p
End If
End With
Next p
MsgBox Decode
End Sub