当前位置: 移动技术网 > IT编程>脚本编程>VBScript > VBS 加解密 For CAPICOM

VBS 加解密 For CAPICOM

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:'***************************************************************************
复制代码 代码如下:

'******************************************************************************
'
' 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, password)
   dim message
   dim encrypteddata

   ' create the encrypteddata object.
   set encrypteddata = createobject("capicom.encrypteddata")

   ' set decryption password.
   encrypteddata.setsecret password

   ' display main title.
   wscript.stdout.writeline "decrypting encrypted text file " & filenames(0) & "."
   wscript.stdout.writeline

   ' load the encrypted message.
   loadfile filenames(0), message

   ' now decrypt it.
   encrypteddata.decrypt(message)

   ' display more detail for verbose operation.
   if verbose then
      displaydetail encrypteddata
   end if

   ' finally, save decrypted content to filenames(1).
   savefile filenames(1), encrypteddata.content
   wscript.stdout.writeline "successful - decrypted content saved to " & filenames(1) & "."

   ' free resources.
   set encrypteddata = nothing
end sub ' end dodecryptcommand
'******************************************************************************
'
' subroutine: loadfile
'
' synopsis  : read content of a text file.
'
' parameter : filename - input text filename.
'
'             buffer - string buffer to receive the text file content.
'
'******************************************************************************
sub loadfile (filename, buffer)  
    dim fso
    set fso = createobject("scripting.filesystemobject")
    if not fso.fileexists(filename) then
        wscript.stdout.writeline "error: file " & filename & " not found."
        wscript.quit(-5)
    end if

    dim ts
    set ts = fso.opentextfile(filename, forreading) 
    buffer = ts.readall

end sub ' end loadfile
'******************************************************************************
'
' subroutine: savefile
'
' synopsis  : save string to file.
'
' parameter : filename - output filename.
'
'             buffer - string buffer to be saved.
'
'******************************************************************************
sub savefile (filename, buffer)
    dim fso
    set fso = createobject("scripting.filesystemobject")

    dim ts
    set ts = fso.opentextfile(filename, forwriting, true)
    ts.write buffer

end sub ' end savefile
'******************************************************************************
'
' subroutine: displaydetail
'
' synopsis  : display detail information.
'
' parameter : encrypteddata - encrypteddata object.
'
'******************************************************************************
sub displaydetail (encrypteddata)
   dim algonames(4)
   algonames(0) = "rc2"
   algonames(1) = "rc4"
   algonames(2) = "des"
   algonames(3) = "3des"
   algonames(4) = "aes"
   wscript.stdout.writeline "algorithm : " & algonames(encrypteddata.algorithm.name)
   wscript.stdout.write     "key length: "

   select case encrypteddata.algorithm.keylength
   case capicom_encryption_key_length_40_bits
      wscript.stdout.writeline "40 bits"

   case capicom_encryption_key_length_56_bits
      wscript.stdout.writeline "56 bits"
   case capicom_encryption_key_length_128_bits
      wscript.stdout.writeline "128 bits"

   case capicom_encryption_key_length_192_bits
      wscript.stdout.writeline "192 bits"

   case capicom_encryption_key_length_256_bits
      wscript.stdout.writeline "256 bits"

   case else
      wscript.stdout.writeline "maximum"

   end select
   wscript.stdout.writeline

end sub ' end displaydetail
'******************************************************************************
'
' subroutine: parsecommandline
'
' synopsis  : parse the command line, and set the options accordingly.
'
' parameter : none
'
'******************************************************************************
sub parsecommandline
   ' constants for command line parsing states.
   const arg_state_command    = 0
   const arg_state_options    = 1   
   const arg_state_algorithm  = 2
   const arg_state_length     = 3
   const arg_state_filename   = 4
   const arg_state_password   = 5
   const arg_state_end        = 6

   ' parse command line.
   dim arg
   dim argstate : argstate = arg_state_command

   for each arg in wscript.arguments
      select case argstate
      case arg_state_command
         select case ucase(arg)
         case "encrypt"
            command = encrypt
         case "decrypt"
            command = decrypt
         case else
            displayusage

         end select

         argstate = arg_state_options

      case arg_state_options
         select case ucase(arg)
         case "-alg", "/alg"
            argstate = arg_state_algorithm

         case "-length", "/length"
            argstate = arg_state_length

         case "-v", "/v"
            verbose = true

         case "-?", "/?"
            displayusage

         case else
            if left(arg, 1) = "-"  or left(arg, 1) = "/" then
               displayusage
            else
               redim filenames(0)
               filenames(0) = arg
            end if
            argstate = arg_state_filename

         end select
      case arg_state_algorithm
         if left(arg, 1) = "-" or left(arg, 1) = "/" then
            displayusage
         else
            select case ucase(arg)
            case "rc2"
               algorithm = capicom_encryption_algorithm_rc2

            case "rc4"
               algorithm = capicom_encryption_algorithm_rc4

            case "des"
               algorithm = capicom_encryption_algorithm_des

            case "3des"
               algorithm = capicom_encryption_algorithm_3des

            case "aes"
               algorithm = capicom_encryption_algorithm_aes

            case else
               displayusage

            end select
         end if
         argstate = arg_state_options

      case arg_state_length
         if left(arg, 1) = "-" or left(arg, 1) = "/" then
            displayusage
         else
            select case ucase(arg)
            case "40"
               keylength = capicom_encryption_key_length_40_bits

            case "56"
               keylength = capicom_encryption_key_length_56_bits

            case "128"
               keylength = capicom_encryption_key_length_128_bits

            case "192"
               keylength = capicom_encryption_key_length_192_bits

            case "256"
               keylength = capicom_encryption_key_length_256_bits

            case "max"
               keylength = capicom_encryption_key_length_maximum

            case else
               displayusage

            end select
         end if
         argstate = arg_state_options

      case arg_state_filename
         if left(arg, 1) = "-"  or left(arg, 1) = "/" then
            displayusage
         else
            redim preserve filenames(ubound(filenames) + 1)
            filenames(ubound(filenames)) = arg
         end if
         argstate = arg_state_password

      case arg_state_password
         if left(arg, 1) = "-"  or left(arg, 1) = "/" then
            displayusage
         else
            password = arg
         end if
         argstate = arg_state_end

      case else
         wscript.stdout.writeline "internal script error: unknown argument state (" & cstr(argstate) & ") encountered."
         wscript.quit(-3)

      end select
   next

   ' make sure we are in good state.
   if argstate <> arg_state_end then
      displayusage
   end if
end sub ' parsecommandline
'******************************************************************************
'
' subroutine: displayusage
'
' synopsis  : display the usage screen, and then exit with a negative error
'             code.
'
' parameter : none.
'
'******************************************************************************
sub displayusage
   select case command
      case unknown
         wscript.stdout.writeline "usage: cencrypt command [options] infile outfile password"
         wscript.stdout.writeline
         wscript.stdout.writeline "command:"
         wscript.stdout.writeline
         wscript.stdout.writeline "  encrypt                    -- encrypt a text file"
         wscript.stdout.writeline "  decrypt                    -- decrypt an encrypted text file"
         wscript.stdout.writeline
         wscript.stdout.writeline "for help on a specific command, enter ""cencrypt command -?"""

      case encrypt
         wscript.stdout.writeline "usage: cencrypt encrypt [options] contentfile encryptedfile password"
         wscript.stdout.writeline
         wscript.stdout.writeline "the encrypt command is used to encrypt a text file based on a secret password."
         wscript.stdout.writeline "encrypting protects the data from being read by others except those who know"
         wscript.stdout.writeline "the secret password."
         wscript.stdout.writeline
         wscript.stdout.writeline "options:"
         wscript.stdout.writeline
         wscript.stdout.writeline "  -alg    <algorithm>        -- rc2, rc4, des, 3des, or aes (default to rc2)"
         wscript.stdout.writeline "  -length <key length>       -- 40, 56, 128, 192, 256, or max (default to max,"
         wscript.stdout.writeline "                                and ignored for des or 3des)"
         wscript.stdout.writeline "  -v                         -- verbose operation"
         wscript.stdout.writeline "  -?                         -- this help screen"
         wscript.stdout.writeline
         wscript.stdout.writeline "  contentfile                -- text file to be encrypted"
         wscript.stdout.writeline
         wscript.stdout.writeline "  encryptedfile              -- encrypted text file"
         wscript.stdout.writeline
         wscript.stdout.writeline "note: all non-fatal invalid options for this specific command will be ignored."
         wscript.stdout.writeline

       case decrypt
         wscript.stdout.writeline "usage: cencrypt decrypt [options] encryptedfile contentfile password"
         wscript.stdout.writeline
         wscript.stdout.writeline "the decrypt command is used to decrypt an encrypted text file."
         wscript.stdout.writeline
         wscript.stdout.writeline "options:"
         wscript.stdout.writeline
         wscript.stdout.writeline "  -v                         -- verbose operation"
         wscript.stdout.writeline "  -?                         -- this help screen"
         wscript.stdout.writeline
         wscript.stdout.writeline "  encryptedfile              -- encrypted text file"
         wscript.stdout.writeline
         wscript.stdout.writeline "  contentfile                -- decrypted text file"
         wscript.stdout.writeline
         wscript.stdout.writeline "note: all non-fatal invalid options for this specific command will be ignored."
         wscript.stdout.writeline

      case else
         wscript.stdout.writeline "internal script error: unknown help state (command = " & cstr(command) & ")."
         wscript.quit(-2)

   end select
   wscript.quit(-1)

end sub ' end displayusage

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网