Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Decrypt File in Chunks using 256-bit AES

See more Encryption Examples
Shows how to decrypt a file chunk-by-chunk using FirstChunk/LastChunk properties and accumulate the results in memory with a Chilkat BinData object.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- This example decrypts the file previously encrypted in this example:
    -- Encrypt File in Chunks using AES CBC

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'cbc'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 256

    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, '000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F', 'hex'
    EXEC sp_OAMethod @crypt, 'SetEncodedIV', NULL, '000102030405060708090A0B0C0D0E0F', 'hex'

    DECLARE @fileToDecrypt nvarchar(4000)
    SELECT @fileToDecrypt = 'c:/temp/qa_output/hamlet.enc'
    DECLARE @facIn int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @facIn OUT

    EXEC sp_OAMethod @facIn, 'OpenForRead', @success OUT, @fileToDecrypt
    IF @success <> 1
      BEGIN

        PRINT 'Failed to open file to be decrytped.'
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @facIn
        RETURN
      END

    -- Let's decrypt in 32000 byte chunks.
    DECLARE @chunkSize int
    SELECT @chunkSize = 32000
    DECLARE @numChunks int
    EXEC sp_OAMethod @facIn, 'GetNumBlocks', @numChunks OUT, @chunkSize

    EXEC sp_OASetProperty @crypt, 'FirstChunk', 1
    EXEC sp_OASetProperty @crypt, 'LastChunk', 0

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    DECLARE @bdDecrypted int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdDecrypted OUT

    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numChunks
      BEGIN
        SELECT @i = @i + 1
        IF @i = @numChunks
          BEGIN
            EXEC sp_OASetProperty @crypt, 'LastChunk', 1
          END

        -- Read the next chunk from the file.
        -- The last chunk will be whatever amount remains in the file..
        EXEC sp_OAMethod @bd, 'Clear', @success OUT
        EXEC sp_OAMethod @facIn, 'FileReadBd', @success OUT, @chunkSize, @bd

        -- Decrypt this chunk.
        EXEC sp_OAMethod @crypt, 'DecryptBd', @success OUT, @bd
        -- Accumulate the decrypted chunks.
        EXEC sp_OAMethod @bdDecrypted, 'AppendBd', @success OUT, @bd

        EXEC sp_OASetProperty @crypt, 'FirstChunk', 0
      END

    -- Make sure both FirstChunk and LastChunk are restored to 1 after
    -- encrypting or decrypting in chunks.  Otherwise subsequent encryptions/decryptions
    -- will produce unexpected results.
    EXEC sp_OASetProperty @crypt, 'FirstChunk', 1
    EXEC sp_OASetProperty @crypt, 'LastChunk', 1

    EXEC sp_OAMethod @facIn, 'FileClose', NULL

    -- The fully decrypted file is contained in bdDecrypted.
    -- You can save to a file if desired, or use the decrypted data in your application directly from bdDecrypted.
    EXEC sp_OAMethod @bdDecrypted, 'WriteFile', @success OUT, 'c:/temp/qa_output/hamlet_decrypted.xml'

    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @facIn
    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @bdDecrypted


END
GO

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.