VBS 加解密 For 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, 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

(0)

相关推荐

  • VBS中通过调用CAPICOM对象实现SHA1&MD5加密

    本来打算自己写的,但是看到复杂的算法描述就知难而退了.算了吧,有现成的COM对象调用,又何必去关心算法呢,直接拿来使用就行了.如果下面的例子代码提示无法创建对象,需要下载CAPICOM组件并注册. 复制代码 代码如下: Const CAPICOM_HASH_ALGORITHM_MD2 = 1 Const CAPICOM_HASH_ALGORITHM_MD4 = 2 Const CAPICOM_HASH_ALGORITHM_MD5 = 3 Const CAPICOM_HASH_ALGORITHM_

  • VBS 加解密 For 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 ' WA

  • 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. 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,否则可能出现

  • Java OpenSSL生成的RSA公私钥进行数据加解密详细介绍

    Java中使用OpenSSL生成的RSA公私钥进行数据加解密 RSA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准.目前该加密方式广泛用于网上银行.数字签名等场合.RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困

  • Base64加解密的实现方式实例详解

    Base64加解密的实现方式实例详解 本实现方式基于JDK 1.8 实现: import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; public class Main { static String src = "hello,sahadev"; public static void main(String[] args) { // 获取加密对象 Encoder

  • Java加解密技术系列之RSA详解

    距离上一次写博客感觉已经很长时间了,先吐槽一下,这个月以来,公司一直在加班,又是发版.上线,又是新项目太紧,具体的就不多说了.今天来说说非对称加密真的是太重要了,我们的日常生活中,都离不开非对称加密. 概念 在说 RSA 之前,首先聊聊什么是非对称加密.在讲对称加密的时候,就曾经说过,对称加密算法在加密和解密时使用的是同一个秘钥,加解密双方必须使用同一个密钥才能进行正常的沟通.而非对称加密则不然,非对称加密算法需要两个密钥来进行加密和解密,分别是公钥和私钥. 需要注意的一点,这个公钥和私钥必须是

  • asp.net实现的MD5加密和DES加解密算法类完整示例

    本文实例讲述了asp.net实现的MD5加密和DES加解密算法类.分享给大家供大家参考,具体如下: #region MD5算法 public string md5(string str, int code) { if (code == 32) //32位加密 { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower(); } else /

  • php5数字型字符串加解密代码

    <?php /* ---------------------------------------------------------------------------- * Script Name: encrypt.php * Creation Date: 2008-4-7 10:36 * Last Modified: 2008-4-12 16:00 * Author: meyu * Copyright (c) 2007 * Purpose: 数字字符串简易加解密 * ------------

  • 详解PHP数据压缩、加解密(pack, unpack)

    网络通信.文件存储中经常需要交换数据,为了减少网络通信流量.文件存储大小以及加密通信规则,经常需要对数据进行双向加解密以保证数据的安全. PHP中实现此功能主要需要使用的函数主要是pack及unpack函数 pack 压缩资料到位字符串之中. 语法: string pack(string format, mixed [args]...); 返回值: 字符串 本函数用来将资料压缩打包到位的字符串之中. a - NUL- 字符串填满[padded string] 将字符串空白以 NULL 字符填满

  • AES加解密在php接口请求过程中的应用示例

    在php请求接口的时候,我们经常需要考虑的一个问题就是数据的安全性,因为数据传输过程中很有可能会被用fillder这样的抓包工具进行截获.一种比较好的解决方案就是在客户端请求发起之前先对要请求的数据进行加密,服务端api接收到请求数据后再对数据进行解密处理,返回结果给客户端的时候也对要返回的数据进行加密,客户端接收到返回数据的时候再解密.因此整个api请求过程中数据的安全性有了一定程度的提高. 今天结合一个简单的demo给大家分享一下AES加解密技术在php接口请求中的应用. 首先,准备一个AE

随机推荐