不能将样本函数分成两个单独的函数

时间:2018-05-06 12:30:35

标签: function vbscript

我发现this function要加密和解密字符串,这是其文档中的示例代码。在样本中,他们在相同的代码块中使用加密和解密,并生成一个名为bytOut的中介变量,用于解密和加密。

我想创建两个单独的函数,但我不知道如何在第二个函数中重用bytOut,因为它是在第一个函数中生成的:

这是样本来源:

<%
sPlain="test"
sPassword="key"

lLength = Len(sPlain)
ReDim bytIn(lLength-1)
For lCount = 1 To lLength
    bytIn(lCount-1) = CByte(AscB(Mid(sPlain, lCount, 1)))
Next

lLength = Len(sPassword)
ReDim bytPassword(lLength-1)
For lCount = 1 To lLength
    bytPassword(lCount-1) = CByte(AscB(Mid(sPassword, lCount, 1)))
Next

'Here the bytOut is generated:
bytOut = EncryptData(bytIn, bytPassword)

sTemp = ""
For lCount = 0 To UBound(bytOut)
    sTemp = sTemp & Right("0" & Hex(bytOut(lCount)), 2)
Next

Response.Write "Encrypted=" & sTemp & "<BR>"

'Here the bytOut is used again
bytClear = DecryptData(bytOut, bytPassword)

lLength = UBound(bytClear) + 1
sTemp = ""
For lCount = 0 To lLength - 1
    sTemp = sTemp & Chr(bytClear(lCount))
Next
Response.Write "Decrypted=" & sTemp
%>

我已经编写了这两个函数,但显然第二个函数不起作用,因为bytOut未初始化:

MyEncrypt功能:

Function MyEncrypt(sPlain)
    sPassword = "key"

    lLength = Len(sPlain)
    ReDim bytIn(lLength-1)
    For lCount = 1 To lLength
        bytIn(lCount-1) = CByte(AscB(Mid(sPlain, lCount, 1)))
    Next

    lLength = Len(sPassword)
    ReDim bytPassword(lLength-1)
    For lCount = 1 To lLength
        bytPassword(lCount-1) = CByte(AscB(Mid(sPassword, lCount, 1)))
    Next

    bytOut = EncryptData(bytIn, bytPassword)

    sTemp = ""
    For lCount = 0 To UBound(bytOut)
        sTemp = sTemp & Right("0" & Hex(bytOut(lCount)), 2)
    Next
    MyEncrypt = sTemp
End Function

MyDecrypt功能:

Function MyDecrypt(sPlain)
    sPassword = "key"

    lLength = Len(sPassword)
    ReDim bytPassword(lLength-1)
    For lCount = 1 To lLength
        bytPassword(lCount-1) = CByte(AscB(Mid(sPassword, lCount, 1)))
    Next

    'I don't know how to generate bytOut here?

    bytClear = DecryptData(bytOut, bytPassword)

    lLength = UBound(bytClear) + 1
    sTemp = ""
    For lCount = 0 To lLength - 1
        sTemp = sTemp & Chr(bytClear(lCount))
    Next

    MyDecrypt = sTemp
End Function

1 个答案:

答案 0 :(得分:1)

您需要将十六进制字符串传递给函数MyDecrypt()并将其转换回字节数组:

Function MyDecrypt(sCrypt)
    sPassword = "key"

    lLength = Len(sPassword)
    ReDim bytPassword(lLength-1)
    For lCount = 1 To lLength
        bytPassword(lCount-1) = CByte(AscB(Mid(sPassword, lCount, 1)))
    Next

    bLength = Int((Len(sCrypt)+1) / 2) - 1
    ReDim bytOut(bLength)
    For i = 0 To bLength
      bytOut(i) = CByte(Int("&h" & Mid(sCrypt, 2*i+1, 2)))
    Next

    bytClear = DecryptData(bytOut, bytPassword)

    lLength = UBound(bytClear) + 1
    sTemp = ""
    For lCount = 0 To lLength - 1
        sTemp = sTemp & Chr(bytClear(lCount))
    Next

    MyDecrypt = sTemp
End Function

我还建议将密码/密钥作为参数传递给两个函数,并将将其转换为字节数组的部分重构为另一个可在MyEncrypt()和{{1}中使用的函数}。