函数参考


_Crypt_EncryptFile

以指定的密钥和算法加密文件

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

参数

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

返回值

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

注意/说明

根据算法, 结果文件可以大于源文件.

相关

_Crypt_EncryptData, _Crypt_DecryptFile, _Crypt_DeriveKey

示例/演示


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

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

GUICreate("File Encrypter", 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 $iEncrypt = GUICtrlCreateButton("Encrypt", 355, 70, 65, 25)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        Case $iSourceBrowse
            $sFilePath = FileOpenDialog("Select a file to encrypt.", "", "所有文件(*.*)") ; Select a file to encrypt.
            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 encrypted 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 $iEncrypt
            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 encrypt and a password has been set.
                Local $iSuccess = _Crypt_EncryptFile($sSourceRead, $sDestinationRead, $sPasswordRead, $bAlgorithm) ; Encrypt 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, "错误", "Encryption error.")
                    EndSwitch
                EndIf
            Else
                MsgBox(16, "错误", "Please ensure the relevant information has been entered correctly.")
            EndIf
    EndSwitch
WEnd