论坛备份,RSA调用DLL加密的另一个例子
1.; ============================================================================================================
; 名称: CAPICOM-例子
; ------------------------------------------------------------------------------------------------------------
; Dlls: capicom.dll
; 文档: https://msdn.microsoft.com/en-us/library/windows/desktop/aa375732(v=vs.85).aspx
; 下载: http://www.microsoft.com/en-us/download/details.aspx?id=25281
; 注释: 安装CAPICOM-SDK后,需要手动注册CAPICOM.dll。.
; 此示例已在Windows 8.1 64位Enterprise和AutoIt版本3.3.14.1中进行了测试。
; ============================================================================================================
#RequireAdmin
#AutoIt3Wrapper_UseX64=N
RunWait(@ComSpec & " /c /s" & 'regsvr32 capicom.dll ', "", @SW_HIDE)
AutoItSetOption('MustDeclareVars', 1)
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_RC2 = 0 ; 使用RSA RC2加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_RC4 = 1 ; 使用RSA RC4加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_DES = 2 ; 使用DES加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_3DES = 3 ;使用三重DES加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_AES = 4 ; 使用高级加密标准(AES)算法.
Dim Const $SECRET_PASSWORD = 'Bzi9i/uEfgg'
; 加密
Dim $Encrypter = ObjCreate('CAPICOM.EncryptedData') ; 数据集
$Encrypter.Content = '这是个秘密消息!'
$Encrypter.SetSecret($SECRET_PASSWORD)
$Encrypter.Algorithm.Name = $CAPICOM_ENCRYPTION_ALGORITHM_RC4
Dim $EncryptedText = $Encrypter.Encrypt() ; As String
ConsoleWrite('加密文本: ' &$EncryptedText & @CRLF)
; 解密
Dim $Encrypter = ObjCreate('CAPICOM.EncryptedData')
$Encrypter.SetSecret($SECRET_PASSWORD)
$Encrypter.Algorithm.Name = $CAPICOM_ENCRYPTION_ALGORITHM_RC4
$Encrypter.Decrypt($EncryptedText)
Dim $DecryptedText = $Encrypter.Content ; 作为字符串
ConsoleWrite('解密文本: ' & $DecryptedText & @CRLF)
2.; ============================================================================================================
; 名称: CAPICOM-例子
; ------------------------------------------------------------------------------------------------------------
; Dlls: capicom.dll
; 文档: https://msdn.microsoft.com/en-us/library/windows/desktop/aa375732(v=vs.85).aspx
; 下载: http://www.microsoft.com/en-us/download/details.aspx?id=25281
; 注释: 安装CAPICOM-SDK后,需要手动注册CAPICOM.dll.
; 此示例已在Windows 8.1 64位Enterprise和AutoIt版本3.3.14.1中进行了测试。
; DLL右键安装证书到个人
; ============================================================================================================
#AutoIt3Wrapper_UseX64=N
RunWait(@ComSpec & " /c /s" & 'regsvr32 capicom.dll ', "", @SW_HIDE)
AutoItSetOption('MustDeclareVars', 1)
; CAPICOM 加密算法
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_RC2 = 0 ; 使用RSA RC2加密
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_RC4 = 1 ; 使用RSA RC4加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_DES = 2 ; 使用DES加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_3DES = 3 ; 使用三重DES加密.
Dim Const $CAPICOM_ENCRYPTION_ALGORITHM_AES = 4 ; 使用高级加密标准(AES)算法.
; CAPICOM 编码类型
Dim Const $CAPICOM_ENCODE_BASE64 = 0 ; 数据保存为base64编码字符串
Dim Const $CAPICOM_ENCODE_BINARY = 1 ; 数据保存为纯二进制序列
Dim $Store = ObjCreate('CAPICOM.Store') ; As IStore3
; Store.Open([ ByVal StoreLocation ], [ ByVal StoreName ], [ ByVal OpenMode ])
$Store.Open()
; Certificates.Select([ ByVal Title ], [ ByVal DisplayString ], [ ByVal bMultiSelect ])
Dim $Certificates = $Store.Certificates.Select('证书', '请选择证书 ...') ; 作为IC证书2
If $Certificates >= 1 Then
Dim $Certificate = $Certificates.Item(1) ; 作为IC证书2
ConsoleWrite('签发时间: ' & $Certificate.GetInfo(0) & @CRLF)
ConsoleWrite('签发人: ' & $Certificate.GetInfo(1) & @CRLF)
ConsoleWrite('私钥可用: ' & $Certificate.HasPrivateKey() & @CRLF)
ConsoleWrite('版本: ' & $Certificate.Version & @CRLF)
ConsoleWrite('有效起始日期: ' & $Certificate.ValidFromDate & @CRLF)
ConsoleWrite('有效期至: ' & $Certificate.ValidToDate & @CRLF)
ConsoleWrite('序列号: ' & $Certificate.SerialNumber & @CRLF)
ConsoleWrite('指纹: ' & $Certificate.Thumbprint & @CRLF)
ConsoleWrite(@CRLF)
Dim $EnvelopedData = ObjCreate('CAPICOM.EnvelopedData') ; 作为输入数据
$EnvelopedData.Algorithm.Name = $CAPICOM_ENCRYPTION_ALGORITHM_RC4
$EnvelopedData.Content = '这是个秘密消息!'
$EnvelopedData.Recipients.Add($Certificate)
Dim $EncryptedText = $EnvelopedData.Encrypt($CAPICOM_ENCODE_BASE64) ; 作为字符串
ConsoleWrite('加密文本: ' & $EncryptedText & @CRLF)
Dim $EncryptedFile = FileOpen('EncryptedFile.txt', 1)
FileWriteLine($EncryptedFile, $EncryptedText)
FileWriteLine($EncryptedFile, "================================================================="&@CRLF)
FileClose($EncryptedFile)
EndIf
测试效果
所需DLL
官方原版
|