程序加密启动 v2.5

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

程序加密启动 v2.5

29 Jun 2017, 22:51

我写了个简单的程序加密启动脚本,可以让其他任何脚本加密启动并防止源代码泄漏。 :D :beer:

Code: Select all

/*
---------------------------------------------
  【程序加密启动】  v2.5  By FeiYue

  使用方法:

  1、将本脚本保存为“程序加密启动.ahk”(或其他文件名)。

  2、将“程序加密启动.ahk”拖动到AutoHotkey.exe
     程序图标上运行一次:先选择你要加密执行的脚本,
     然后设置任意密码(空格也算),之后会自动尝试
     编译本脚本(使用默认选项),最后会测试运行。
    (注:编译本脚本时会把目标脚本包含进来生成最终程序)

  3、如果自动编译失败,或者对自动编译的结果不满意
     (比如需要选择图标、选择bin文件、选择MPRESS压缩),
     可以手动运行Ahk2Exe将“程序加密启动.ahk”编译为程序。

  4、使用时目录中必须有AutoHotkey.exe,发布时一起打包。

---------------------------------------------
*/

;//输入密码部分
InputBox, Password, 程序启动, `n`n请输入启动密码:
Password:=GetHash("1234567890" . Password)
IfEqual, A_IsCompiled, 1, Goto, IsCompiled

;//加密部分
SplitPath, A_AhkPath,, dir
SetWorkingDir, %dir%
FileSelectFile, file, 1, %dir%, `n请选择需要加密的脚本, *.ahk; *.txt
IfNotExist, %file%
{
  MsgBox, 4096, 提示, `n没有找到目标脚本!程序退出!
  ExitApp
}
FileRead, str, %file%
str.="`n`n;=== The End ==="
size:=StrPut(str,"CP0"), k:=4, i:=-k
VarSetCapacity(data, size+k, 0), StrPut(str, &data, "CP0")
Loop, % Ceil(size/k)
  Password:=(Password*131+A_Index)&0xFFFFFFFF
    , NumPut(NumGet(data,i+=k,"uint")^Password,data,i,"uint")
file:=FileOpen(A_ScriptDir "\~data.tmp", "w")
file.RawWrite(data, size+k)
file.Close()
file=
MsgBox, 4096, 提示, `n目标脚本加密完毕!下面尝试自动编译本脚本!
if FileExist(exe:=dir "\Ahk2Exe.exe")
  or FileExist(exe:=dir "\Compiler\Ahk2Exe.exe")
{
  SplitPath, A_ScriptName,,,, name
  file:=dir "\" name ".exe"
  RunWait, "%exe%" /in "%A_ScriptFullPath%" /out "%file%" /mpress 1
  MsgBox, 4096, 提示
    , % "`n自动编译成功!下面测试运行:" file "`n`n"
    . "如果对自动编译的结果不满意,请手动运行Ahk2Exe.exe编译本脚本。"
  Run, %file%
}
else
  MsgBox, 4096, 提示, `n没有找到Ahk2Exe.exe!请自己手动编译本脚本!
ExitApp

;//解密部分
IsCompiled:
IfEqual, false, 1
  FileInstall, ~data.tmp, ~
lib:=DllCall("GetModuleHandle", "ptr",0, "ptr")
res:=DllCall("FindResource","ptr",lib,"Str","~data.tmp","ptr",10,"ptr")
if !res
{
  MsgBox, 4096, 提示, `n发生读取错误!程序退出!
  ExitApp
}
size:=DllCall("SizeofResource", "ptr",lib, "ptr",res)
hres:=DllCall("LoadResource", "ptr",lib, "ptr",res, "ptr")
pres:=DllCall("LockResource", "ptr",hres, "ptr")
k:=4, i:=-k, VarSetCapacity(data, size+k, 0)
DllCall("RtlMoveMemory", "ptr",&data, "ptr",pres, "ptr",size)
Loop, % size//k
  Password:=(Password*131+A_Index)&0xFFFFFFFF
    , NumPut(NumGet(data,i+=k,"uint")^Password,data,i,"uint")
str:=StrGet(&data, "CP0")
if !InStr(str,"`n`n;=== The End ===")
{
  MsgBox, 4096, 提示, `n输入密码错误!程序退出!
  ExitApp
}

;//动态运行部分
Ahk:=A_ScriptDir "\AutoHotkey.exe"
IfNotExist, %Ahk%
{
  MsgBox, 4096, 提示, `n没有找到“%Ahk%”!程序退出!
  ExitApp
}
Try {
  addScript:="`nSetWorkingDir, " A_ScriptDir
    . "`nMenu, Tray, Icon, " A_ScriptFullPath ", 1`n"
  PipeRun(addScript . str, Ahk)
}
ExitApp


;///// 下面是函数 /////


GetHash(str, seed=131)
{
  VarSetCapacity(var,n:=StrPut(str,"CP0"))
  StrPut(str,p:=&var,"CP0"), hash:=0
  Loop, % n-1
    hash:=((hash*seed)+(*p++))&0xFFFFFFFF
  return, hash
}

; thanks Lexikos - Run Dynamic Script Through a Pipe
; https://autohotkey.com/board/topic/23575-how-to-run-dynamic-script-through-a-pipe/
PipeRun(Script, Ahk="", arg="")
{
  Ptr:=A_PtrSize ? "UPtr" : "UInt"
  Ahk:=Ahk="" ? A_AhkPath:Ahk
  Random, n, 1000, 9999
  pipe:="\\.\pipe\Ahk" . A_TickCount . n
  Script:=RegExReplace(Script,"\R","`r`n")
  Script:=(A_IsUnicode ? chr(0xFEFF)
    : chr(0xEF) . chr(0xBB) . chr(0xBF)) . Script
  Size:=(StrLen(Script)+1)*(A_IsUnicode ? 2:1)
  Loop, 2
    if (-1=(p%A_Index%:=DllCall("CreateNamedPipe", "Str",pipe, "UInt",2
      ,"UInt",0, "UInt",255, "UInt",0, "UInt",0, "UInt",0, Ptr,0, Ptr)))
        Return, 0
  Run, "%Ahk%" "%pipe%" %arg%,, UseErrorLevel, pid
  if ErrorLevel
    Return, 0*DllCall("CloseHandle",Ptr,p1)*DllCall("CloseHandle",Ptr,p2)
  DllCall("ConnectNamedPipe", Ptr,p1, Ptr,0)
  DllCall("CloseHandle", Ptr,p1)
  DllCall("ConnectNamedPipe", Ptr,p2, Ptr,0)
  DllCall("WriteFile", Ptr,p2, "Str",Script, "UInt",Size, "UIntP",0, Ptr,0)
  DllCall("CloseHandle", Ptr,p2)
  Return, pid
}

;

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 20 guests