Post by sinkfaze » 02 Oct 2013, 20:25
COM Object: CAPICOM Series
Purpose: digitally sign data, sign code, verify digital signatures, envelop data for privacy, hash data, encrypt/decrypt data and more.
System Requirements: 32-bit OS, for more details, please see MSDN
Documentation Link: CAPICOM ReferenceOther Links:Platform SDK Redistributable: CAPICOMBasic Code Example: First require to install capicom.dll from microsoft's site above.
Code: [Select all] [Expand]GeSHi © Codebox Plus
/*
************************************************************
CAPIEncrypt.ahk
This is a sample script to illustrate how to use the CAPICOMs EncryptedData
to encrypt/decrypt text file.
Note: For simplicity, this script does not handle exception.
************************************************************
*/
ForReading := 1
ForWriting := 2
; CAPICOM's constants.
CAPICOM_ENCRYPTION_ALGORITHM_RC2 := 0
CAPICOM_ENCRYPTION_ALGORITHM_RC4 := 1
CAPICOM_ENCRYPTION_ALGORITHM_DES := 2
CAPICOM_ENCRYPTION_ALGORITHM_3DES := 3
CAPICOM_ENCRYPTION_ALGORITHM_AES := 4
CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM := 0
CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS := 1
CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS := 2
CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS := 3
CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS := 4
CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS := 5
strSF := "C:\test.txt"
strOF := "C:\Encrypted.txt"
Algorithm := CAPICOM_ENCRYPTION_ALGORITHM_AES
KeyLength := CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS
Password := "123456"
EncryptCommand(strSF, strOF, Algorithm, KeyLength, Password)
;~ DecryptCommand(strSF, strOF, Password)
return
; End Main
; EncryptCommand() - Encrypt content of text file ObjectFile.
EncryptCommand(SourceFile, ObjectFile, Algorithm, KeyLength, Password)
{
Content := ""
Message := ""
EncryptedData := ""
; Create the EncryptedData object.
EncryptedData := ComObjCreate("CAPICOM.EncryptedData")
; algorithm, key size, and encryption password.
EncryptedData.Algorithm.Name := Algorithm
EncryptedData.Algorithm.KeyLength := KeyLength
EncryptedData.SetSecret(Password)
; Display main title.
MsgBox, % "Encrypting text file " . SourceFile . "."
; Load content of text file to be encrypted.
LoadFile(SourceFile, Content)
; Now encrypt it.
EncryptedData.Content := Content
Message := EncryptedData.Encrypt
; Finally, save encrypted message to strSF.
SaveFile(ObjectFile, Message)
MsgBox, % "Successful - Encrypted message saved to " . ObjectFile . "."
; Free resources.
EncryptedData := ""
}
; DecryptCommand() - Decrypt an encrypted file.
DecryptCommand(SourceFile, ObjectFile, Password)
{
Message := ""
EncryptedData := ""
; Create the EncryptedData object.
EncryptedData := ComObjCreate("CAPICOM.EncryptedData")
; decryption password.
EncryptedData.SetSecret(Password)
; Display main title.
MsgBox, % "Decrypting encrypted text file " . SourceFile . "."
; Load the encrypted message.
LoadFile(SourceFile, Message)
; Now decrypt it.
EncryptedData.Decrypt(Message)
; Finally, save decrypted content to strSF.
SaveFile(ObjectFile, EncryptedData.Content)
MsgBox, % "Successful - Decrypted content saved to " . ObjectFile . "."
; Free resources.
EncryptedData := ""
}
; LoadFile() - Read content of a text file.
LoadFile(FileName, ByRef Buffer)
{
global ForReading
objFSO := "", objTS := ""
objFSO := ComObjCreate("Scripting.FileSystemObject")
If Not objFSO.FileExists(FileName)
{
MsgBox, % "Error: File " . FileName . " not found."
ExitApp
}
objTS := objFSO.OpenTextFile(FileName, ForReading)
Buffer := objTS.ReadAll
}
; SaveFile() - Save string to file.
SaveFile(FileName, ByRef Buffer)
{
global ForWriting
objFSO := "", objTS := ""
objFSO := ComObjCreate("Scripting.FileSystemObject")
objTS := objFSO.OpenTextFile(FileName, ForWriting, True)
objTS.Write(Buffer)
}
[b]COM Object:[/b] CAPICOM Series
[b]Purpose:[/b] digitally sign data, sign code, verify digital signatures, envelop data for privacy, hash data, encrypt/decrypt data and more.
[b]System Requirements:[/b] 32-bit OS, for more details, please see MSDN
[b]Documentation Link:[/b] [url=http://msdn.microsoft.com/en-us/library/aa375732]CAPICOM Reference[/url]
[b]Other Links:[/b][url=http://www.microsoft.com/downloads/en/details.aspx?FamilyID=860ee43a-a843-462f-abb5-ff88ea5896f6]Platform SDK Redistributable: CAPICOM[/url]
[b]Basic Code Example:[/b] First require to install capicom.dll from microsoft's site above.
[code]/*
************************************************************
CAPIEncrypt.ahk
This is a sample script to illustrate how to use the CAPICOMs EncryptedData
to encrypt/decrypt text file.
Note: For simplicity, this script does not handle exception.
************************************************************
*/
ForReading := 1
ForWriting := 2
; CAPICOM's constants.
CAPICOM_ENCRYPTION_ALGORITHM_RC2 := 0
CAPICOM_ENCRYPTION_ALGORITHM_RC4 := 1
CAPICOM_ENCRYPTION_ALGORITHM_DES := 2
CAPICOM_ENCRYPTION_ALGORITHM_3DES := 3
CAPICOM_ENCRYPTION_ALGORITHM_AES := 4
CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM := 0
CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS := 1
CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS := 2
CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS := 3
CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS := 4
CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS := 5
strSF := "C:\test.txt"
strOF := "C:\Encrypted.txt"
Algorithm := CAPICOM_ENCRYPTION_ALGORITHM_AES
KeyLength := CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS
Password := "123456"
EncryptCommand(strSF, strOF, Algorithm, KeyLength, Password)
;~ DecryptCommand(strSF, strOF, Password)
return
; End Main
; EncryptCommand() - Encrypt content of text file ObjectFile.
EncryptCommand(SourceFile, ObjectFile, Algorithm, KeyLength, Password)
{
Content := ""
Message := ""
EncryptedData := ""
; Create the EncryptedData object.
EncryptedData := ComObjCreate("CAPICOM.EncryptedData")
; algorithm, key size, and encryption password.
EncryptedData.Algorithm.Name := Algorithm
EncryptedData.Algorithm.KeyLength := KeyLength
EncryptedData.SetSecret(Password)
; Display main title.
MsgBox, % "Encrypting text file " . SourceFile . "."
; Load content of text file to be encrypted.
LoadFile(SourceFile, Content)
; Now encrypt it.
EncryptedData.Content := Content
Message := EncryptedData.Encrypt
; Finally, save encrypted message to strSF.
SaveFile(ObjectFile, Message)
MsgBox, % "Successful - Encrypted message saved to " . ObjectFile . "."
; Free resources.
EncryptedData := ""
}
; DecryptCommand() - Decrypt an encrypted file.
DecryptCommand(SourceFile, ObjectFile, Password)
{
Message := ""
EncryptedData := ""
; Create the EncryptedData object.
EncryptedData := ComObjCreate("CAPICOM.EncryptedData")
; decryption password.
EncryptedData.SetSecret(Password)
; Display main title.
MsgBox, % "Decrypting encrypted text file " . SourceFile . "."
; Load the encrypted message.
LoadFile(SourceFile, Message)
; Now decrypt it.
EncryptedData.Decrypt(Message)
; Finally, save decrypted content to strSF.
SaveFile(ObjectFile, EncryptedData.Content)
MsgBox, % "Successful - Decrypted content saved to " . ObjectFile . "."
; Free resources.
EncryptedData := ""
}
; LoadFile() - Read content of a text file.
LoadFile(FileName, ByRef Buffer)
{
global ForReading
objFSO := "", objTS := ""
objFSO := ComObjCreate("Scripting.FileSystemObject")
If Not objFSO.FileExists(FileName)
{
MsgBox, % "Error: File " . FileName . " not found."
ExitApp
}
objTS := objFSO.OpenTextFile(FileName, ForReading)
Buffer := objTS.ReadAll
}
; SaveFile() - Save string to file.
SaveFile(FileName, ByRef Buffer)
{
global ForWriting
objFSO := "", objTS := ""
objFSO := ComObjCreate("Scripting.FileSystemObject")
objTS := objFSO.OpenTextFile(FileName, ForWriting, True)
objTS.Write(Buffer)
}[/code]