萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> VBS 加解密 For CAPICOM

VBS 加解密 For CAPICOM

這篇文章主要介紹了vbs中通過CAPICOM進行加解密的實現代碼,需要的朋友可以參考下   復制代碼 代碼如下:


'******************************************************************************
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
' Copyright (C) 1999- 2002.  Microsoft Corporation.  All rights reserved.
'
'******************************************************************************
'
' CEncrypt.vbs
'
' This is a sample script to illustrate how to use the CAPICOM's EncryptedData
' to encrypt/decrypt text file.
'
' Note: For simplicity, this script does not handle exception.
'
'******************************************************************************
Option Explicit
Const ForReading = 1, ForWriting = 2
' Command.
Const Unknown                                = 0
Const Encrypt                                = 1
Const Decrypt                                = 2

' CAPICOM's constants.                                            
Const CAPICOM_ENCRYPTION_ALGORITHM_RC2       = 0
Const CAPICOM_ENCRYPTION_ALGORITHM_RC4       = 1
Const CAPICOM_ENCRYPTION_ALGORITHM_DES       = 2
Const CAPICOM_ENCRYPTION_ALGORITHM_3DES      = 3
Const CAPICOM_ENCRYPTION_ALGORITHM_AES       = 4

Const CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM  = 0
Const CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS  = 1
Const CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS  = 2
Const CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS = 3
Const CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS = 4
Const CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS = 5
' Command line arguments.
Dim Command       : Command                  = Unknown
Dim Password      : Password                 = Null
Dim Algorithm     : Algorithm                = CAPICOM_ENCRYPTION_ALGORITHM_RC2
Dim KeyLength     : KeyLength                = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
Dim Verbose       : Verbose                  = False
Dim FileNames()

' First make sure the script is executed by CScript.exe.
If InStr(1, UCase(Wscript.FullName), "CSCRIPT.EXE", vbTextCompare) = 0 Then
   Wscript.Echo "This script can only be executed by CScript.exe." & vbCRLF & vbCRLF &_
                "You can either:" & vbCRLF & vbCRLF & _
                "1. Set CScript.exe as the default (Run CScript //h:cscript), or" & vbCRLF & _
                "2. Run CScript.exe directly as in, CScript " & Wscript.ScriptName & "."
   Wscript.Quit(-1)
End If
' Parse the command line.
ParseCommandLine        
' Now process the command.
Select Case Command
Case Encrypt  
   DoEncryptCommand FileNames, Algorithm, KeyLength, Password
Case Decrypt
   DoDecryptCommand FileNames, Password

End Select

Wscript.Quit(0)
' End Main

'******************************************************************************
'
' Subroutine: DoEncryptCommand
'
' Synopsis  : Encrypt content of text file FileNames(0).
'
' Parameter : FileNames - Array of filenames.
'
'             Algorithm - Encryption algorithm
'
'             KeyLength - Key size.
'
'             Password - Secret password.
'
'******************************************************************************
Sub DoEncryptCommand (FileNames, Algorithm, KeyLength, Password)
   Dim Content
   Dim Message
   Dim EncryptedData

   ' Create the EncryptedData object.
   Set EncryptedData = CreateObject("CAPICOM.EncryptedData")

   ' Set algorithm, key size, and encryption password.
   EncryptedData.Algorithm.Name = Algorithm
   EncryptedData.Algorithm.KeyLength = KeyLength
   EncryptedData.SetSecret Password

   ' Display main title.
   Wscript.Stdout.Writeline "Encrypting text file " & FileNames(0) & "."
   Wscript.Stdout.Writeline

   ' Display more detail for verbose operation.
   If Verbose Then
      DisplayDetail EncryptedData
   End If

   ' Load content of text file to be encrypted.
   LoadFile FileNames(0), Content

   ' Now encrypt it.
   EncryptedData.Content = Content
   Message = EncryptedData.Encrypt

   ' Finally, save encrypted message to FileNames(1).
   SaveFile FileNames(1), Message
   Wscript.Stdout.Writeline "Successful - Encrypted message saved to " & FileNames(1) & "."
   ' Free resources.
   Set EncryptedData = Nothing

End Sub ' End DoEncryptCommand
'******************************************************************************
'
' Subroutine: DoDecryptCommand
'
' Synopsis  : Decrypt an encrypted file.
'
' Parameter : FileNames - Array of filenames.
'
'             Password - Secret password.
'
'******************************************************************************
Sub DoDecryptCommand (FileNames, Pass

copyright © 萬盛學電腦網 all rights reserved