'Study/AutoIt/AutoHotKey'에 해당되는 글 2건
- 2013.09.23 [연습]사내 온라인 산업안전교육 자동수강 및 자동시험응시기 1
- 2013.07.29 [펌]Alternative to _StringEncrypt()
#AutoIt3Wrapper_icon=AutoㅇㅇㅇㅇLecture.ico
; 목적 : AutoIt 학습 및 imagesearch 라이브러리 사용법 학습
; 버전 : 3.3.8.1
; 일자 : 2013.06월의 어느날
; 동작 :
; ㅇㅇㅇㅇ 사내 산업안전교육 온라인 교육 및 시험을 자동으로 수강 및 응시한다.
#include <ImageSearch.au3>
Global $gs_mainwintitle = "ㅇㅇㅇㅇ㈜"
Global $gs_lecwintitle = "교안보기..."
Global $gs_googledef = "Google 계정"
Global $gs_chromedef = "- Chrome"
Global $ghWnd_MWin = 0
Global $gs_userid = ""
Global $gs_userpw = ""
Global $gl_maxleccnt = 5
Global $gl_limitsec = 1210
Global $gpos_basex1 = 0
Global $gpos_basey1 = 0
Global $Paused
Global $gl_tolerance = 0
If MutexExists("ㅇㅇㅇㅇOnlineLecture") Then
MsgBox(0, "알림", "자동수강이 이미 실행 중입니다.")
ToolTip("")
Exit
EndIf
Func MutexExists($sOccurenceName)
Local $ERROR_ALREADY_EXISTS = 183, $handle, $lastError
$sOccurenceName = StringReplace($sOccurenceName, "", "")
$handle = DllCall("kernel32.dll", "int", "CreateMutex", "int", 0, "long", 1, "str", $sOccurenceName)
$lastError = DllCall("kernel32.dll", "int", "GetLastError")
Return $lastError[0] = $ERROR_ALREADY_EXISTS
EndFunc
HotKeySet("{ESC}", "ExitForce")
HotKeySet("+!p", "TogglePause")
ToolTip("산업안전 자동수강 실행중...", 0, 0)
If FileExists(@ProgramFilesDir & "\Google\Chrome\Application\chrome.exe") Then
If MsgBox(4,"알림", "ㅇㅇㅇㅇ 온라인 법정안전교육 자동수강을 시작하시겠습니까?" & @CRLF & _
"(모든 강의를 수강하신 후 이 프로그램을 재실행 하시면" & @CRLF & " 자동 시험응시가 가능합니다!!)") == 6 Then
While 1
$gl_tolerance = InputBox("입력", "이미지 검색 오차범위를 설정하여 주세요.(1~10사이숫자범위)" & @CRLF & @CRLF & "검색 오차범위는 프로그램이 창을 찾지 못하면 높게 잡아주세요!!" & @CRLF & @CRLF &"오차범위가 낮을수록 프로그램 정확도가 올라갑니다!!" & @CRLF & "(3정도가 가장 좋은 인식율을 보이는것 같습니다)",3,"",400,250)
If $gl_tolerance == "" Then
If MsgBox(4, "입력", "프로그램을 실행을 중지하시겠습니까?") == 6 Then
ToolTip("")
Exit
EndIf
EndIf
If $gl_tolerance < 1 or $gl_tolerance > 10 Then
MsgBox(0, "알림", "1~10 사이 숫자만 입력가능합니다!!")
Else
ExitLoop
EndIf
WEnd
Main()
Else
MsgBox(0, "알림", "사용자 요청으로 자동수강을 종료합니다!!", 3)
ToolTip("")
Exit
EndIf
Else
If MsgBox(4, "알림", "크롬 브라우저가 설지되어 있지 않습니다!!" & @CRLF & "자동수강은 크롬 브라우저가 설치되어 있어야 정상 동작합니다!!" & @CRLF & "크롬 브라우저를 설치하시겠습니까?") == 6 Then
$ll_retval = ShellExecute("explorer.exe", "http://www.google.com/intl/ko/chrome/browser/")
If $ll_retval == 1 Then
MsgBox(0, "알림", "멋진 선택이십니다. 크롬 설치후 다시한번 실행하여 주세요!!")
ToolTip("")
Exit
Else
MsgBox(0, "알림", "알수없는 오류로 인하여 크롬브라우저 설치 진행에 실패하였습니다!! 아쉽지만 다음기회에....")
ToolTip("")
Exit
EndIf
Else
MsgBox(0, "알림", "크롬브라우저가 없어 진행을 종료합니다!!")
ToolTip("")
Exit
EndIf
EndIf
Func Main()
$lpos_x1 = 0
$lpos_y1 = 0
$ll_retval = 0
While ProcessExists("chrome.exe")
ProcessClose("chrome.exe")
WEnd
Close_ExistWindow($gs_mainwintitle)
Close_ExistWindow($gs_lecwintitle)
$ghWnd_MWin = Find_Window($gs_mainwintitle)
If $ghWnd_MWin < 0 Then
MsgBox(0, "알림", "ㅇㅇㅇㅇ 온라인 법정안전교육 창을 찾지 못했습니다!!")
ToolTip("")
Exit
EndIf
$ll_retval = WinMove($ghWnd_MWin,"", 0,0,1024,768)
If $ll_retval == 0 Then
MsgBox(0, "알림", "ㅇㅇㅇㅇ 온라인 법정안전교육 창을 찾지 못했습니다!!")
ToolTip("")
Exit
EndIf
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("GoogleDef.bmp",3,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1+85,$lpos_y1)
Sleep(100)
MouseClick("left")
EndIf
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("ChromeDef.bmp",3,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(100)
MouseClick("left")
EndIf
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("LectureDef.bmp",3,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(100)
MouseClick("left")
EndIf
Activate_MainWin($ghWnd_MWin)
Input_UserInfo()
Login_Site()
GoToLectureRoom()
WatchLecture()
EndFunc
Func GetBasePosition()
$lpos_x1 = 0
$lpos_y1 = 0
$lpos = 0
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("BasePosition.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
MsgBox(0,"알림", "시험답안지 기준점을 찾는데 실패하였습니다" & @CRLF & "프로그램을 종료합니다!!")
ToolTip("")
Exit
EndIf
MouseMove($lpos_x1, $lpos_y1)
$lpos = MouseGetPos()
$gpos_basex1 = $lpos[0] + 1
$gpos_basey1 = $lpos[1] + 3
;MsgBox(0, "알림", "Mouse x,y:" & $pos[0] & "," & $pos[1])
EndFunc
Func AnswerSheetMatrix($al_ques_no, $al_answ_no, ByRef $apos_x1, ByRef $apos_y1)
$lpos_x1 = $gpos_basex1
If $al_ques_no <= 6 Then
$lpos_y1 = $gpos_basey1
Else
$al_ques_no = $al_ques_no - 7
$lpos_y1 = $gpos_basey1 + 316
EndIf
$apos_x1 = $lpos_x1 + (41 * $al_answ_no)
$apos_y1 = $lpos_y1 + (57 * $al_ques_no)
EndFunc
Func EnterExam()
Dim $larray_answer[11] = [0,4,4,4,2,1,4,2,3,4,3]
$lpos_x1 = 0
$lpos_y1 = 0
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("ExamSeason.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
MsgBox(0,"알림", "3분기 근로자 정기교육만 자동응시 가능합니다!!" & @CRLF & "프로그램을 종료합니다!!")
ToolTip("")
Exit
EndIf
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("EnterExam01.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("EnterExam02.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
EndIf
If $ll_retval <> 1 Then
MsgBox(0,"알림", "응시할 시험이 없습니다!!" & @CRLF & "프로그램을 종료합니다!!")
ToolTip("")
Exit
EndIf
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(1000)
MouseClick("left")
Sleep(1000)
MouseMove(0,0)
For $i = 1 To 6
Send("{RIGHT}")
Sleep(50)
Next
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("AnswerSheet.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
MsgBox(0,"알림", "답안지를 찾을 수 없습니다!!" & @CRLF & "프로그램을 종료합니다!!")
ToolTip("")
Exit
EndIf
GetBasePosition()
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(100)
MouseClick("left")
$lpos_basex1 = $lpos_x1
$lpos_basey1 = $lpos_y1
For $i = 1 To UBound($larray_answer) - 1
If $i == 8 Then
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_basex1,$lpos_basey1)
Sleep(50)
MouseClick("left")
For $j = 1 To 3
Activate_MainWin($ghWnd_MWin)
Send("{DOWN}")
Sleep(50)
Next
EndIf
AnswerSheetMatrix($i - 1, $larray_answer[$i] - 1, $lpos_x1, $lpos_y1)
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(100)
MouseClick("left")
Sleep(100)
Next
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_basex1,$lpos_basey1)
Sleep(50)
MouseClick("left")
For $j = 1 To 3
Activate_MainWin($ghWnd_MWin)
Send("{DOWN}")
Sleep(50)
Next
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("Submit.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
MsgBox(0,"알림", "제출버튼을 찾을 수 없습니다!!" & @CRLF & "수동으로 제출해주세요!!")
EndIf
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(100)
MouseClick("left")
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("Confirm.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
MsgBox(0,"알림", "제출버튼을 찾을 수 없습니다!!" & @CRLF & "수동으로 제출해주세요!!")
EndIf
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(100)
MouseClick("left")
MsgBox(0, "알림", "시험 응시가 끝났습니다!! 쌍콤한 하루 되세요~")
ToolTip("")
Exit
EndFunc
Func WatchLecture()
$lpos_x1 = 0
$lpos_y1 = 0
$ll_errcnt = 0
$idx_lec = 1
While 1
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("Completing.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval <> 1 Then
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("NotComplete.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
EndIf
If $ll_retval == 1 Then
$lpos_x1 = $lpos_x1 - 330
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(1000)
MouseClick("left")
Sleep(2000)
MouseMove(0,0)
$ll_retval = WinSetState($ghWnd_MWin, "", @SW_MINIMIZE)
$ll_retval = WinExists($gs_lecwintitle)
If $ll_retval == 1 Then
$lhWnd_LecWin = WinGetHandle($gs_lecwintitle)
$ll_retval = WinMove($lhWnd_LecWin,"", 0,0,1024,768)
$ll_retval = WinSetState($lhWnd_LecWin, "", @SW_MINIMIZE)
$ls_msg = ""
ProgressOn("자동수강 진행사항", String($idx_lec) & "차시 강의 " & "0% 수강중...(다른작업 가능합니다.)", "")
$ll_watchrate = 0
; 테스트 30초 수강!!
;$gl_limitsec = 10
For $ll_remainsec = 1 To $gl_limitsec
$ll_watchrate = Round(($ll_remainsec / $gl_limitsec) * 100, 0)
$ls_msg = "중지:ESC / 일시정지:Alt+Shift+p"
$ll_retval = WinExists($lhWnd_LecWin)
If $ll_retval <> 1 Then
ProgressOff()
MsgBox(0, "알림", "강의창이 사라져 자동수강을 종료합니다!!")
ToolTip("")
Exit
EndIf
ProgressSet($ll_watchrate, String($ll_watchrate) & "% 수강중...(다른작업 가능합니다.)", $ls_msg)
Sleep(1000)
Next
ProgressSet(100, "강의 100% 수강완료!!", $ls_msg)
Sleep(500)
ProgressOff()
WinClose($lhWnd_LecWin)
$idx_lec = $idx_lec + 1
If $idx_lec > $gl_maxleccnt Then
ExitLoop
EndIf
Else
MsgBox(0,"알림","미수강 강의 리스트를 찾지 못하였습니다!!" & @CRLF & "3초후 강의를 재탐색 합니다!!", 3)
WinClose($gs_lecwintitle)
$ll_errcnt = $ll_errcnt + 1
If $ll_errcnt >= 3 Then
MsgBox(0, "알림", "더이상 수강 가능한 강의를 찾을 수 없어 프로그램을 종료 합니다!!")
ToolTip("")
Exit
EndIf
EndIf
Else
MsgBox(0,"알림","미수강 강의 리스트를 찾지 못하였습니다!!" & @CRLF & "3초후 강의를 재탐색 합니다!!", 3)
WinClose($gs_lecwintitle)
$ll_errcnt = $ll_errcnt + 1
If $ll_errcnt >= 3 Then
MsgBox(0, "알림", "더이상 수강 가능한 강의를 찾을 수 없어 프로그램을 종료 합니다!!")
ToolTip("")
Exit
EndIf
EndIf
WEnd
EndFunc
Func GoToLectureRoom()
$lpos_x1 = 0
$lpos_y1 = 0
Activate_MainWin($ghWnd_MWin)
For $i = 1 To 3
If $i == 3 Then
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("PossibleExam01.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
If MsgBox(4, "알림", "이미 모든 강의를 수강하셨습니다!! 시험에 응시 하시겠습니까?") == 6 Then
EnterExam()
ToolTip("")
Exit
EndIf
EndIf
EndIf
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("ClassRoom0" & String($i) & ".bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1)
Sleep(1000)
MouseClick("left")
Sleep(1000)
Else
MsgBox(0,"알림","강의실 바로가기를 찾지 못하였습니다!!")
ToolTip("")
Exit
EndIf
Next
EndFunc
Func Activate_MainWin($ahWnd_Win)
$ll_retval = WinActivate($ahWnd_Win)
MouseMove(0,0)
If $ll_retval == 0 Then
MsgBox(0, "알림", "ㅇㅇㅇㅇ 온라인 법정안전교육 창을 찾지 못했습니다!!")
ToolTip("")
Exit
EndIf
EndFunc
Func Login_Site()
$lpos_x1 = 0
$lpos_y1 = 0
Activate_MainWin($ghWnd_MWin)
$ll_retval = _WaitForImageSearch("Logout.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
if $ll_retval == 1 Then
MouseMove($lpos_x1-55,$lpos_y1+19)
Sleep(1000)
MouseClick("left")
Sleep(3000)
EndIf
$lpos_x1 = 0
$lpos_y1 = 0
$ll_retval = _WaitForImageSearch("Login.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
Activate_MainWin($ghWnd_MWin)
MouseMove($lpos_x1,$lpos_y1-12,1)
MouseClick("left")
Send($gs_userid)
Sleep(500)
Send("{Enter}")
Sleep(500)
Send("{Tab}")
Sleep(500)
Send($gs_userpw)
Send("{Enter}")
$ll_retval = _WaitForImageSearch("Loginfail.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
If $ll_retval == 1 Then
Close_ExistWindow($gs_mainwintitle)
MsgBox(0, "알림", "입력하신 ID 또는 PW가 올바르지 않습니다!!" & @CRLF & "확인 후 다시 시도하여 주세요!!")
ToolTip("")
Exit
EndIf
Else
MsgBox(0,"알림","로그인창을 찾지 못하였습니다!!")
ToolTip("")
Exit
EndIf
EndFunc
Func Input_UserInfo()
$gs_userid = ""
$gs_userpw = ""
$gs_userid = InputBox("입력", "교육대상자 ID를 입력!!" & @CRLF & "(그룹웨어ID[사번아님]})")
$gs_userpw = InputBox("입력", "교육대상자 비밀번호 입력!!" & @CRLF & "(기본패스워드 1111)", "", "*")
If $gs_userid == "" Or $gs_userpw == "" Then
If MsgBox(4, "알림", "교육대상자 ID 또는 패스워드가 올바르지 않습니다!!" & @CRLF & "다시 입력하시겠습니까?") == 6 Then
Input_UserInfo()
Else
MsgBox(0, "알림", "프로그램을 종료합니다!!",5)
ToolTip("")
Exit
EndIf
Else
Return 1
EndIf
EndFunc
Func Find_Window($as_wintitle)
$ll_retval = 0
$ll_retval = WinExists($as_wintitle)
If $ll_retval == 1 Then
Return WinGetHandle($as_wintitle)
Else
$ll_retval = ShellExecute("chrome.exe", "--incognito http://ㅇㅇㅇㅇ.산업안전보건교육.kr/")
If $ll_retval == 1 Then
Sleep(2000)
Return WinGetHandle($as_wintitle)
Else
Return -1
EndIf
EndIf
EndFunc
Func Close_ExistWindow($as_wintitle)
While 1
$lhWnd_temp = 0
$ll_retval = 0
$lpos_x1 = 0
$lpos_y1 = 0
$ll_retval = WinExists($as_wintitle)
If $ll_retval == 1 Then
$lhWnd_temp = WinGetHandle($as_wintitle)
Activate_MainWin($lhWnd_temp)
Sleep(500)
$ll_retval = WinMove($lhWnd_temp,"", 0,0,1024,768)
If $ll_retval == 0 Then
MsgBox(0, "알림", "ㅇㅇㅇㅇ 온라인 법정안전교육 창을 찾지 못했습니다!!")
ToolTip("")
Exit
EndIf
$ll_retval = _WaitForImageSearch("Logout.bmp",5,1,$lpos_x1,$lpos_y1,$gl_tolerance)
if $ll_retval == 1 Then
MouseMove($lpos_x1-55,$lpos_y1+19)
Sleep(500)
MouseClick("left")
Sleep(1000)
EndIf
WinClose($lhWnd_temp)
Else
ExitLoop
EndIf
WEnd
EndFunc
Func ExitForce()
If MsgBox(4,"알림", "ㅇㅇㅇㅇ 온라인 법정안전교육 자동수강을 강제 종료 하시겠습니까?") == 6 Then
ToolTip("")
Exit
EndIf
EndFunc
Func TogglePause()
$Paused = Not $Paused
While $Paused
Sleep(100)
ToolTip("자동수강 일시정지중...", -1, -1)
WEnd
ToolTip("자동수강 진행중....", -1, -1)
EndFunc
'Study > AutoIt/AutoHotKey' 카테고리의 다른 글
[펌]Alternative to _StringEncrypt() (0) | 2013.07.29 |
---|
원문출처 : http://www.autoitscript.com/forum/topic/66005-alternative-to-stringencrypt/
This function came out of my trying to understand a supposed problem with _StringEncrypt(). Only one user reported that problem, and it was never duplicated by anyone else, so it very well may not be a bug, and this is not an attempt to "fix" _StringEncrypt().
The only thing I wanted to achieve was a version of _StringEncrypt() that would output a "standard" result for RC4 encryption, that would match RC4 implementations on web sites and in other languages. The encryption side of that was completely solved by SkinnyWhiteGuy in the topic: equal encrypt function autoit and php
What I did here was match the string-based nature of _StringEncrypt(), vice the binary nature of RC4(), with the multi-pass encryption functionality provided by the $i_EncryptLevel parameter. The result is __StringEncrypt(), with the double-underbar in the name.
Here is the function, within a basic test script:
$sString = "This is a text string" ConsoleWrite("Debug: $sString = " & $sString & @LF) $sKey = "Key phrase" ConsoleWrite("Debug: $sKey = " & $sKey & @LF) For $z = 1 To 4 ConsoleWrite("Debug: Encryption level = " & $z & @LF) $sEncrypted = __StringEncrypt(1, $sString, $sKey, $z) ConsoleWrite("Debug: $sEncrypted = " & $sEncrypted & @LF) $sDecrypted = __StringEncrypt(0, $sEncrypted, $sKey, $z) ConsoleWrite("Debug: $sDecrypted = " & $sDecrypted & @LF) Next ;=============================================================================== ; ; Function Name: __StringEncrypt() ; Description: RC4 Based string encryption/decryption ; Parameter(s): $i_Encrypt - 1 to encrypt, 0 to decrypt ; $s_EncryptText - string to encrypt ; $s_EncryptPassword - string to use as an encryption password ; $i_EncryptLevel - integer to use as number of times to encrypt string ; Requirement(s): None ; Return Value(s): On Success - Returns the encrypted string ; On Failure - Returns a blank string and sets @error = 1 ; Author(s): (Original _StringEncrypt) Wes Wolfe-Wolvereness <Weswolf at aol dot com> ; (Modified __StringEncrypt) PsaltyDS at www.autoitscript.com/forum ; (RC4 function) SkinnyWhiteGuy at www.autoitscript.com/forum ;=============================================================================== ; 1.0.0.0 | 03/08/08 | First version posted to Example Scripts Forum ;=============================================================================== Func __StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1) Local $RET, $sRET = "", $iBinLen, $iHexWords ; Sanity check of parameters If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then SetError(1) Return '' ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then SetError(1) Return '' EndIf If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1 ; Encrypt/Decrypt If $i_Encrypt Then ; Encrypt selected $RET = $s_EncryptText For $n = 1 To $i_EncryptLevel If $n > 1 Then $RET = Binary(Random(0, 2 ^ 31 - 1, 1)) & $RET & Binary(Random(0, 2 ^ 31 - 1, 1)) ; prepend/append random 32bits $RET = rc4($s_EncryptPassword, $RET) ; returns binary Next ; Convert to hex string $iBinLen = BinaryLen($RET) $iHexWords = Int($iBinLen / 4) If Mod($iBinLen, 4) Then $iHexWords += 1 For $n = 1 To $iHexWords $sRET &= Hex(BinaryMid($RET, 1 + (4 * ($n - 1)), 4)) Next $RET = $sRET Else ; Decrypt selected ; Convert input string to primary binary $RET = Binary("0x" & $s_EncryptText) ; Convert string to binary ; Additional passes, if required For $n = 1 To $i_EncryptLevel If $n > 1 Then $iBinLen = BinaryLen($RET) $RET = BinaryMid($RET, 5, $iBinLen - 8) ; strip random 32bits from both ends EndIf $RET = rc4($s_EncryptPassword, $RET) Next $RET = BinaryToString($RET) EndIf ; Return result Return $RET EndFunc ;==>__StringEncrypt ; ------------------------------------------------------- ; Function: rc4 ; Purpose: An encryption/decryption RC4 implementation in AutoIt ; Syntax: rc4($key, $value) ; Where: $key = encrypt/decrypt key ; $value = value to be encrypted/decrypted ; On success returns encrypted/decrypted version of $value ; Author: SkinnyWhiteGuy on the AutoIt forums at www.autoitscript.com/forum ; Notes: The same function encrypts and decrypts $value. ; ------------------------------------------------------- Func rc4($key, $value) Local $S[256], $i, $j, $c, $t, $x, $y, $output Local $keyLength = BinaryLen($key), $valLength = BinaryLen($value) For $i = 0 To 255 $S[$i] = $i Next For $i = 0 To 255 $j = Mod($j + $S[$i] + Dec(StringTrimLeft(BinaryMid($key, Mod($i, $keyLength) + 1, 1), 2)), 256) $t = $S[$i] $S[$i] = $S[$j] $S[$j] = $t Next For $i = 1 To $valLength $x = Mod($x + 1, 256) $y = Mod($S[$x] + $y, 256) $t = $S[$x] $S[$x] = $S[$y] $S[$y] = $t $j = Mod($S[$x] + $S[$y], 256) $c = BitXOR(Dec(StringTrimLeft(BinaryMid($value, $i, 1), 2)), $S[$j]) $output = Binary($output) & Binary('0x' & Hex($c, 2)) Next Return $output EndFunc ;==>rc4
These are the results of two consecutive runs in SciTE:
>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\AutoIt3\Scripts\Test_1.au3" Debug: $sString = This is a text string Debug: $sKey = Key phrase Debug: Encryption level = 1 Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C5 Debug: $sDecrypted = This is a text string Debug: Encryption level = 2 Debug: $sEncrypted = FC92DD7408FBF831F8F71EB9518557EDB7A25191EAB226AEB9CF3776E3 Debug: $sDecrypted = This is a text string Debug: Encryption level = 3 Debug: $sEncrypted = 553996021B3DCE57CB21CDFD0B8808FD6D2C304B15CB79F796C76BA4680B031A03B2175035 Debug: $sDecrypted = This is a text string Debug: Encryption level = 4 Debug: $sEncrypted = BCB5DD4D8F19ED1D98BF8285385EDBB937216F5BCF45182D69BE34FD472FE0D6CE404C747C269F35C8F27D790C Debug: $sDecrypted = This is a text string +>13:06:59 AutoIT3.exe ended.rc:0
>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\AutoIt3\Scripts\Test_1.au3" Debug: $sString = This is a text string Debug: $sKey = Key phrase Debug: Encryption level = 1 Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C5 Debug: $sDecrypted = This is a text string Debug: Encryption level = 2 Debug: $sEncrypted = 3F83796908FBF831F8F71EB9518557EDB7A25191EAB226AEB9261552D2 Debug: $sDecrypted = This is a text string Debug: Encryption level = 3 Debug: $sEncrypted = 4FEEF71FB98E811CCB21CDFD0B8808FD6D2C304B15CB79F796C76BA468D39E1676FC9FAA1A Debug: $sDecrypted = This is a text string Debug: Encryption level = 4 Debug: $sEncrypted = 600821594D0853343717D6DA385EDBB937216F5BCF45182D69BE34FD472FE0D6CE1CC2B3258B0833861A82BA52 Debug: $sDecrypted = This is a text string +>13:08:34 AutoIT3.exe ended.rc:0
Note that the encrypted string is the same between the two runs where $i_EncryptLevel = 1, but is different where $i_EncryptLevel > 1. It is useful in some contexts to get different results that can still be decrypted with same key to the same value.
As long as $i_EncryptLevel = 1, this function should be directly compatible with string-based RC4 implementations elsewhere (on web sites or in applications). For example, you'll get the same result if you put the string "This is a text string" and the key "Key phrase" into this web site: http://www.4guysfromrolla.com/
'Study > AutoIt/AutoHotKey' 카테고리의 다른 글
[연습]사내 온라인 산업안전교육 자동수강 및 자동시험응시기 (1) | 2013.09.23 |
---|