Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
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
MS Storage Providers
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
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Encrypt File in Chunks using AES CBC

Demonstrates how to use the FirstChunk/LastChunk properties to encrypt a file chunk-by-chunk.

Important This example requires Chilkat v9.5.0.68 or later.

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
    -- This example requires the Chilkat Crypt API to have been previously unlocked.
    -- See Unlock Chilkat Crypt for sample code.

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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', 128

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

    DECLARE @fileToEncrypt nvarchar(4000)
    SELECT @fileToEncrypt = 'qa_data/hamlet.xml'
    DECLARE @facIn int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.FileAccess', @facIn OUT

    DECLARE @success int
    EXEC sp_OAMethod @facIn, 'OpenForRead', @success OUT, @fileToEncrypt
    IF @success <> 1
      BEGIN

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

    DECLARE @outputEncryptedFile nvarchar(4000)
    SELECT @outputEncryptedFile = 'qa_output/hamlet.enc'
    DECLARE @facOutEnc int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.FileAccess', @facOutEnc OUT

    EXEC sp_OAMethod @facOutEnc, 'OpenForWrite', @success OUT, @outputEncryptedFile
    IF @success <> 1
      BEGIN

        PRINT 'Failed to encrypted output file.'
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @facIn
        EXEC @hr = sp_OADestroy @facOutEnc
        RETURN
      END

    -- Let's encrypt in 10000 byte chunks.
    DECLARE @chunkSize int
    SELECT @chunkSize = 10000
    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_9_5_0.BinData', @bd 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

        -- Encrypt.
        EXEC sp_OAMethod @crypt, 'EncryptBd', @success OUT, @bd

        -- Write the encrypted chunk to the output file.
        EXEC sp_OAMethod @facOutEnc, 'FileWriteBd', @success OUT, @bd, 0, 0

        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
    EXEC sp_OAMethod @facOutEnc, 'FileClose', NULL

    -- Decrypt the encrypted output file in a single call using CBC mode:
    DECLARE @decryptedFile nvarchar(4000)
    SELECT @decryptedFile = 'qa_output/hamlet_dec.xml'
    EXEC sp_OAMethod @crypt, 'CkDecryptFile', @success OUT, @outputEncryptedFile, @decryptedFile
    -- Assume success for the example..

    -- Compare the contents of the decrypted file with the original file:
    DECLARE @bSame int
    EXEC sp_OAMethod @facIn, 'FileContentsEqual', @bSame OUT, @fileToEncrypt, @decryptedFile

    PRINT 'bSame = ' + @bSame

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


END
GO

 

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