SQL Server Stored Procedure Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

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

Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

AES Encryption to Match PHP's Mcrypt Extension

Demonstrates how to match AES encryption results with PHP's mcrypt extension.

Please refer to this discussion about PHP AES Encryption. The code here matches the PHP mcrypt results on that page.

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

    --  AES is also known as Rijndael.
    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'

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

    --  KeyLength may be 128, 192, 256
    EXEC sp_OASetProperty @crypt, 'KeyLength', 256

    --  Pad with NULL bytes (PHP pads with NULL bytes)
    EXEC sp_OASetProperty @crypt, 'PaddingScheme', 3

    --  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'

    DECLARE @ivAscii nvarchar(4000)

    SELECT @ivAscii = '1234567890123456'
    EXEC sp_OAMethod @crypt, 'SetEncodedIV', NULL, @ivAscii, 'ascii'

    --  The secret key must equal the size of the key.  For
    --  256-bit encryption, the binary secret key is 32 bytes.
    --  For 128-bit encryption, the binary secret key is 16 bytes.
    DECLARE @keyAscii nvarchar(4000)

    SELECT @keyAscii = '12345678901234561234567890123456'
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @keyAscii, 'ascii'

    DECLARE @plainText nvarchar(4000)

    SELECT @plainText = 'The quick brown fox jumped over the lazy dog'

    DECLARE @cipherText nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @cipherText OUT, @plainText
    PRINT @cipherText

    --  Do 128-bit AES encryption:
    EXEC sp_OASetProperty @crypt, 'KeyLength', 128
    SELECT @keyAscii = '1234567890123456'
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @keyAscii, 'ascii'

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @cipherText OUT, @plainText
    PRINT @cipherText

    --  -------
    --  Results
    --  -------
    --  You may use these as test vectors for testing your AES implementations...
    -- 
    --  ------------------------
    --  256-bit key, CBC mode
    --  ------------------------
    --  IV = '1234567890123456' 
    --   (hex: 31323334353637383930313233343536)
    --  Key = '12345678901234561234567890123456' 
    --   (hex: 3132333435363738393031323334353631323334353637383930313233343536)
    --  PlainText:
    --   'The quick brown fox jumped over the lazy dog'
	// CipherText(hex):
    --   2fddc3abec692e1572d9b7d629172a05caf230bc7c8fd2d26ccfd65f9c54526984f7cb1c4326ef058cd7bee3967299e3

    -- 
    --  ------------------------
    --  128-bit key, CBC mode
    --  ------------------------
    --  IV = '1234567890123456' 
    --   (hex: 31323334353637383930313233343536)
    --  Key = '1234567890123456' 
    --   (hex: 31323334353637383930313233343536)
    --  PlainText:
    --   'The quick brown fox jumped over the lazy dog'
	// CipherText(hex):
    --   f78176ae8dfe84578529208d30f446bbb29a64dc388b5c0b63140a4f316b3f341fe7d3b1a3cc5113c81ef8dd714a1c99

END
GO

 

Need a specific example? Send a request to support@chilkatsoft.com

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