Sample code for 30+ languages & platforms
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 SQL Server Downloads

SQL Server
-- 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