函数参考


_Crypt_DecryptFile

使用指定的密钥和算法解密文件

#Include <Crypt.au3>
_Crypt_DecryptFile($sSourceFile, $sDestinationFile, $vCryptKey, $iALG_ID)

参数

$sSourceFile 要被处理的文件
$sDestinationFile 处理后保存的文件
$vCryptKey 当指定了 CALG_USERKEY 标记时,需提供的密钥密码或句柄
$iALG_ID 使用的算法

返回值

成功: 返回 True
设置 @error 为 0
失败: 返回 -1 并设置 @error:
1 - 无法建立密钥
2 - 无法打开源文件
3 - 无法打开结果文件
4 - 无法解密最后部分
5 - 部分数据无法解密

注意/说明

None.

相关

_Crypt_EncryptFile, _Crypt_DeriveKey

示例/演示


#include <Crypt.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>

Local $bAlgorithm = $CALG_RC4
Local $sFilePath = ""

GUICreate("File Decrypter", 425, 100)
Local $iSourceInput = GUICtrlCreateInput("", 5, 5, 200, 20)
Local $iSourceBrowse = GUICtrlCreateButton("...", 210, 5, 35, 20)

Local $iDestinationInput = GUICtrlCreateInput("", 5, 30, 200, 20)
Local $iDestinationBrowse = GUICtrlCreateButton("...", 210, 30, 35, 20)

GUICtrlCreateLabel("Password:", 5, 60, 200, 20)
Local $iPasswordInput = GUICtrlCreateInput("", 5, 75, 200, 20)

Local $iCombo = GUICtrlCreateCombo("", 210, 75, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "3DES|AES (128bit)|AES (192bit)|AES (256bit)|DES|RC2|RC4", "RC4")
Local $iDecrypt = GUICtrlCreateButton("Decrypt", 355, 70, 65, 25)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        Case $iSourceBrowse
            $sFilePath = FileOpenDialog("Select a file to decrypt.", "", "所有文件(*.*)") ; Select a file to decrypt.
            If @error Then
                ContinueLoop
            EndIf
            GUICtrlSetData($iSourceInput, $sFilePath) ; Set the inputbox with the filepath.

        Case $iDestinationBrowse
            $sFilePath = FileSaveDialog("Save the file as ...", "", "所有文件(*.*)") ; Select a file to save the decrypted data to.
            If @error Then
                ContinueLoop
            EndIf
            GUICtrlSetData($iDestinationInput, $sFilePath) ; Set the inputbox with the filepath.

        Case $iCombo ; Check when the combobox is selected and retrieve the correct algorithm.
            Switch GUICtrlRead($iCombo) ; Read the combobox selection.
                Case "3DES"
                    $bAlgorithm = $CALG_3DES

                Case "AES (128bit)"
                    If @OSVersion = "WIN_2000" Then
                        MsgBox(16, "错误", "Sorry, this algorithm is not available on Windows 2000.") ; Show an error if the system is Windows 2000.
                        ContinueLoop
                    EndIf
                    $bAlgorithm = $CALG_AES_128

                Case "AES (192bit)"
                    If @OSVersion = "WIN_2000" Then
                        MsgBox(16, "错误", "Sorry, this algorithm is not available on Windows 2000.")
                        ContinueLoop
                    EndIf
                    $bAlgorithm = $CALG_AES_192

                Case "AES (256bit)"
                    If @OSVersion = "WIN_2000" Then
                        MsgBox(16, "错误", "Sorry, this algorithm is not available on Windows 2000.")
                        ContinueLoop
                    EndIf
                    $bAlgorithm = $CALG_AES_256

                Case "DES"
                    $bAlgorithm = $CALG_DES

                Case "RC2"
                    $bAlgorithm = $CALG_RC2

                Case "RC4"
                    $bAlgorithm = $CALG_RC4

            EndSwitch

        Case $iDecrypt
            Local $sSourceRead = GUICtrlRead($iSourceInput) ; Read the source filepath input.
            Local $sDestinationRead = GUICtrlRead($iDestinationInput) ; Read the destination filepath input.
            Local $sPasswordRead = GUICtrlRead($iPasswordInput) ; Read the password input.
            If StringStripWS($sSourceRead, 8) <> "" And StringStripWS($sDestinationRead, 8) <> "" And StringStripWS($sPasswordRead, 8) <> "" And FileExists($sSourceRead) Then ; Check there is a file available to decrypt and a password has been set.
                Local $iSuccess = _Crypt_DecryptFile($sSourceRead, $sDestinationRead, $sPasswordRead, $bAlgorithm) ; Decrypt the file.
                If $iSuccess Then
                    MsgBox(4096, "Success", "Operation succeeded.")
                Else
                    Switch @error
                        Case 1
                            MsgBox(16, "错误", "Failed to create the key.")
                        Case 2
                            MsgBox(16, "错误", "Couldn't open the source file.")
                        Case 3
                            MsgBox(16, "错误", "Couldn't open the destination file.")
                        Case 4 Or 5
                            MsgBox(16, "错误", "Decryption error.")
                    EndSwitch
                EndIf
            Else
                MsgBox(16, "错误", "Please ensure the relevant information has been entered correctly.")
            EndIf
    EndSwitch
WEnd