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
FTP
HTML-to-XML
HTTP
MHT
MIME
RSA Encryption
Socket
Spider
String
Tar
Upload
XML
XMP
Zip

Byte Array
RSS
Atom
Self-Extractor

Matching .NET Framework AES Encryption Results

AES string encryption in SQL Server and the equivalent C# code using the .NET Framework.

Download Chilkat Crypt ActiveX

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int

    --  First, here's the C# code we're trying to match:

    --  RijndaelManaged rman = new RijndaelManaged();
    --  rman.Mode = CipherMode.CBC;
    --  rman.Padding = PaddingMode.PKCS7;
    --  rman.KeySize = 256;

    --  Use a 32-byte key (for 256-bit encryption)
    --  byte [] keyBytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
    --  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };

    --  The IV for AES is 16 bytes, because the AES block size is 16.
    --  byte [] ivBytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    --  ICryptoTransform encryptor = rman.CreateEncryptor(keyBytes, ivBytes);

    --  byte [] plainText = System.Text.Encoding.UTF8.GetBytes("This is a test");

    --  byte [] encrypted = encryptor.TransformFinalBlock(plainText, 0, plainText.Length);

    --  Output is 31k+86baFy9GJKQ9Y1ebCw==
    --  textBox1.Text = Convert.ToBase64String(encrypted);

    --  The following Chilkat code produces the same output:
    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

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'cbc'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 256
    EXEC sp_OASetProperty @crypt, 'PaddingScheme', 0
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'

    DECLARE @ivHex nvarchar(4000)

    SELECT @ivHex = '000102030405060708090A0B0C0D0E0F'
    DECLARE @keyHex nvarchar(4000)

    SELECT @keyHex = '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F'
    EXEC sp_OAMethod @crypt, 'SetEncodedIV', NULL, @ivHex, 'hex'
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @keyHex, 'hex'

    DECLARE @encStr nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encStr OUT, 'This is a test'
    PRINT @encStr


END
GO

 

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

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