学习笔记之文本加密

3/8/2017来源:ASP.NET技巧人气:3093

使用异或(xor)操作作为加密算法,异或操作有一个特点:一个数连续与另一个数进行两次异或则结果等于其自身,因此进行一次加密(异或)得到密文,再异或一次得到原文。

修改参与异或操作的数据数值则可以形成不同的加密算法。VBS脚本加密文本文件示例

     'On Error Resume Next

'1打开文件

do

  strLoadFile = BrowseForFile()

  if strLoadFile <> "" then

    WScript.Echo strLoadFile

    exit do

  else

k=msgbox("您未选中任何文件,重新选择吗?",vbYesNo,"vbs代码加密工具")

    if k=vbno then wscript.quit

  end if

loop

'2 打开选择的文件

set fso = CreateObject("Scripting.FileSystemObject")

set ws = createobject("wscript.shell")

set f=fso.getfile(strLoadFile)

path=f.parentfolder

name=f.name

set fr=fso.opentextfile(strLoadFile)

set fw=fso.createtextfile("加密" +name,2)

'3 加密过程

do while fr.atendofstream=false

  line=fr.readline

  dow=""

  for i=1 to len(line)   

    achar=mid(line,i,1)

'----------------加密算法,可以修改-------------------------

'--将下面的123换成0~255的任何值,则对应不同的加密算法-----

bchar=chr(asc(achar) Xor 123)

dow=dow & (bchar)

'dow=dow & (achar)

next

fw.write dow

loop

fr.close

fw.close

'4 运行结束弹出提示对话框

ws.popup "加密成功!",3

'VBS打开选择文件对话框,适用于Windows7

Function BrowseForFile()

    Dim shell : Set shell = CreateObject("WScript.Shell")

    Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")

    Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)

    Dim tempName : tempName = fso.GetTempName()

    Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")

    tempFile.Write _

    "<html>" & _

    "<head>" & _

    "<title>Browse</title>" & _

    "</head>" & _

    "<body>" & _

    "<input type='file' id='f' />" & _

    "<script type='text/javascript'>" & _

    "var f = document.getElementById('f');" & _

    "f.click();" & _

    "var shell = new ActiveXObject('WScript.Shell');" & _

    "shell.RegWrite('HKEY_CURRENT_USER\\Volatile Environment\\MsgResp', f.value);" & _

    "window.close();" & _

    "</script>" & _

    "</body>" & _

    "</html>"

    tempFile.Close

    shell.Run tempFolder & "\" & tempName & ".hta", 0, True

    BrowseForFile = shell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp")

    shell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp"

End Function

加密后的内容:

#ServerInfo#MS7U2EL1pj488Im0X04dtSbyjcz3kir/rIYxMiJhH/5ZzvS/qyvmIYkA9hjm/UCjk4MirOJSW6eeKAB9TN2fvsMJqdfDzoYxcbTFIgIGKucMg+ISQVe7CgXsEl398qD6GBhNOrUV+uE1aJvNDAjO2WIn9N31PEo93t/HFOeusBYlgKtXK6lIiBBD5oUkdtYxaL7p7+kJchyHag/5C5DOE1M2E96buM2FbgllscpSkRfb8W2OXndkPXsIIwjVRu5Bf2ioyY5Zfit78w5ig3Ygm4Ja4lF+EF4bc2288git5VH4XiRiMAuqY+6ZMzEBZg5sFU/PbF4F86C6G7OgTnrLG8MGY5wucbJUjzgZDebGBzWqaCLtn7DcFUtJ/wx462OhybGSktEb4Q7ylzDhLuKR5UOG2swLuV1/DS564dTiCFYmrgHslTeHTAvEGkYqt7G0fslY50bBJ9F1zPMBiEnx/ebCppNdoc5jiwu95O8BYJveJz/iNE1pLFpkhBm2cOnQGTPYKQtls0DxtiA1/UJ495IiEC21FBYaIVqhau1YFe1DosLmAe0/2POUvb5JzPHqkINPEmaSLqJSNob6cUCohXQ04XNkg2xMizL3nmvrrD4FmYeCyrWvDvbwT85xnZkoCzH6hxKOpJDnqU5NxmO1F1w63Yfxgajse40LQsrN3fMlTcJXQhqG7GFqxZ7rqm/zFYvebXoNM4txTrh1CvBhOOP858KTvhcUWfC5l7qzjbRQ6Bu82B6GjpZJ3GvTuX5c93CNzk6/db/IieEGixt9rl3g3f5206hNIS7GliDDSdb6L2xllFP2FN1usSXYsv/gHeoN+NBEWBXvqKJB70yDoZUNeJC+Cn39QEUMTpjgqcsBo5MDfdm3plpZSE7vpWfZye+IfXZqxurw6C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