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

ASCII / ANSI Secret Key in Symmetric Encryption

Using an ASCII (or ANSI) secret key w/ symmetric encryption: AES, Blowfish, Twofish, RC2, 3DES, etc.

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

    --  A frequent support question goes something like this:
    --  ------------------
    --  I am not able to decrypt strings encrypted using
    --  cryptix.jce.provider.key.RawSecretKey (Java) like I would normally do.

    -- The key generation is being done with the Rijndael algorithm (256-bit)
    --  The AES encryption algorithm is "Rijndael/ECB/PKCS#5"

    --  The 32 character source key I'm using is this:
    --  1234567890123456789012345678912
    -- 
    --  ------------------
    --  The customer tries to call GenerateSecretKey to
    --  convert the 32-character source key to a binary secret key,
    --  but this is incorrect (in this case).  GenerateSecretKey
    --  hashes a string of any length and produces binary hash output
    --  that is of the appropriate size to be used as a secret key.

    --  The telltale sign is that in this case, the string is the exact
    --  length of the secret key.  32 ANSI characters = 256 bits.
    --  Therefore, it is obvious that the bytes of the binary
    --  secret key should be the exact ascii character values
    --  of the string.

    DECLARE @encKey nvarchar(4000)

    SELECT @encKey = '1234567890123456789012345678912'

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'
    --  Mode is "ecb" or "cbc"
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'ecb'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 256

    --  Call SetEncodedKey with "ascii" to tell the component
    --  to use the ascii-bytes as the binary secret key.
    --  Make sure you call this after setting the KeyLength.
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @encKey, 'ascii'

    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'
    DECLARE @text nvarchar(4000)

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

    --  Encrypt a string and return the binary encrypted data
    --  in a base-64 encoded string.
    DECLARE @encText nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encText OUT, @text


    PRINT @encText

    --  Decrypt and show the original string:
    DECLARE @decryptedText nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'DecryptStringENC', @decryptedText OUT, @encText


    PRINT @decryptedText
END
GO

 

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

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