VBS 加解密 For CAPICOM_vbs

复制代码 代码如下:

'******************************************************************************
'
' 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

时间: 2024-10-14 17:19:28

VBS 加解密 For CAPICOM_vbs的相关文章

VBS 加解密 For MS Script Encode

这篇文章主要介绍了微软自己的代码加解密实现方法,需要的朋友可以参考下   一.加密 复制代码 代码如下: Dim ObjectFSO If (lcase(right(wscript.fullname,11))="wscript.exe") Then  WScript.Quit(0) End If If wscript.arguments.count<2 Then  Wscript.Echo "VBS Code Encoder v1.0 Powered by ENUN.

VBS 加解密 For CAPICOM

这篇文章主要介绍了vbs中通过CAPICOM进行加解密的实现代码,需要的朋友可以参考下   复制代码 代码如下: '****************************************************************************** ' ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, ' EITHER EXPRESSED OR IMPLIE

VBS 加解密 For MS Script Encode_vbs

一.加密 复制代码 代码如下: Dim ObjectFSO If (lcase(right(wscript.fullname,11))="wscript.exe") Then WScript.Quit(0)End If If wscript.arguments.count<2 Then Wscript.Echo "VBS Code Encoder v1.0 Powered by ENUN. http://www.enun.net/" Wscript.Echo

一个批量编码转换及ASP/JS加解密/简繁转换的工具_相关技巧

详情看这里  http://hi.baidu.com/lael80/blog/item/6633d7fc89f9f282b801a061.html  http://www.gzyd.net/lael/coding.rar  文件编码转换/Screnc加解密/简繁转换 1.0  使用注意:  1.使用前请备份,使用本软件造成的损失自负:  2.进行编码转换前请确认所有文件的源编码一致并选择正确,否则可能出现错误.  3.对ASP代码进行加密或解密或简繁转换前须确认文件的编码为ANSI,否则可能出现

使用证书对XML文档进行加解密

xml|解密 Imports SystemImports System.XmlImports System.Security.CryptographyImports System.Security.Cryptography.XmlImports System.Security.Cryptography.X509CertificatesPublic Class Form1     Private Sub Form1_Load(ByVal sender As System.Object, ByVal

DES 加解密的封装和 使用的例子

封装|解密 原创:zhouxianli1978@msn.com ,欢迎喜欢Java的网友加我 DES加密封装 package org.jtool.desutils;import java.security.SecureRandom;import javax.crypto.*;import javax.crypto.spec.DESKeySpec;public class DESEncrypt {    private byte[] desKey;    public DESEncrypt(byt

WPS for Linux A10新功能:增XLSX文件加解密支持

1. 全新2013界面 2. 加入多语言机制, 可根据需要设置语言 3. ET新增图表读取 支持 4. ET新增TXT. CSV.PRN文件读写,支持多编码 5. ET新增XML文 件读写 6. ET新增XLSX 文件加解密支持 7. WPS新增TXT 文件读写,支持多编码 修 复 1. ET写XLSX文 件时丢失部分自选图形填充效果 2. 无法打开文件名含 有反斜杠的文件 3. WPP插入特定背 景音乐文件导致崩溃 立即下载 kingsoft-office_9.1.0.4032~a10_i38

VB.net2008实例 编写文字加解密程序

  本篇文章的主要开发环境是Visual Studio 2008,Visual Studio系列产品一直以来都提供了强大的控件功能,然而我们利用这些控件可以编写出功能强大的应用程序,Visual Studio2008在一些特性上也做出了许多的改进.本文主要利用微软的最新.net开发工具为大家展示文字加密与解密的应用方法,为大家介绍创建基本加密与解密程序的一些技巧.很适合.net开发工具的初学者,具有一定的实用价值. 打开 Visual Studio 2008在文件 (File) 菜单上,单击新建

WinXP通过命令行加解密文件的技巧

  对文件加解密的方法有很多种,WinXP系统就为用户提供了一个特殊功能--通过命令行cipher加解密文件或文件夹. 下面我们来了解下这功能吧. 详解如下: 1.命令格式: cipher [{/e|/d}] [/sir] [/a] [/I] [/f] [/q] [/h] [/k] [/u[/n]] [PathName [-]] | [/rathNameWithoutExtension] | [/wathName] 2.参数设置: 在不含带参数的情况下使用,则 cipher 将显示当前文件夹及其