SQL Server Stored Procedure Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
DKIM / DomainKey
FTP
HTML Conversion
HTTP
MHT
MIME
NTLM
RSA
Diffie-Hellman
DSA
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
String
Tar
Upload
XML
XMP
Zip

Amazon S3
Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

112-bit 3DES Encryption

Demonstrates how to do 112-bit 3DES encryption.

Download Chilkat Crypt ActiveX

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

    DECLARE @success int

    EXEC sp_OAMethod @crypt, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Crypt component unlock failed'
        RETURN
      END

    --  Specify 3DES for the encryption algorithm:
    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', '3des'

    --  CipherMode may be "ecb" or "cbc"
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'ecb'

    --  Set the key length
    EXEC sp_OASetProperty @crypt, 'KeyLength', 112

    --  Choose a padding scheme...
    EXEC sp_OASetProperty @crypt, 'PaddingScheme', 0

    --  EncodingMode specifies the encoding of the output for
    --  encryption, and the input for decryption.
    --  It may be "hex", "url", "base64", or "quoted-printable".
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex'

    --  An initialization vector is required if using CBC or CFB modes.
    --  ECB mode does not use an IV.
    --  The length of the IV is equal to the algorithm's block size.
    --  It is NOT equal to the length of the key.
    DECLARE @ivHex nvarchar(4000)

    SELECT @ivHex = '0001020304050607'
    EXEC sp_OAMethod @crypt, 'SetEncodedIV', NULL, @ivHex, 'hex'

    --  The secret key must equal the size of the key.
    --  Remember, DES (i.e. 3DES) uses a parity bit in the key,
    --  so 112-bit 3DES requires 128 bits of key material
    --  (i.e. 16 bytes)
    DECLARE @keyHex nvarchar(4000)

    SELECT @keyHex = '11165395389c904862912aba16d315b8'
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @keyHex, 'hex'

    --  Encrypt a string...
    DECLARE @encStr nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encStr OUT, '999999987'
    --  The result should be: 8CDBB138C11EDC3A77F04E488B46385C
    PRINT @encStr

    --  Now decrypt:
    DECLARE @decStr nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'DecryptStringENC', @decStr OUT, @encStr
    PRINT @decStr
END
GO

 

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