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

RSA Encrypting Symmetric Secret Key

The RSA encryption algorithm is computationally expensive. It is not the best choice for encrypting large amounts of data. Symmetric encryption algorithms such as AES (i.e. Rijndael) or Blowfish are much more efficient. A typical application scenario is that you want to send encrypted messages to a partner, but you don't want to send the symmetric key unprotected. A solution is to generate a public/private RSA key pair and provide your partner with the public key (in advance). You may then encrypt the symmetric algorithm's key using the RSA private key. Next, encrypt the message using the symmetric algorithm, and send your partner both the encrypted key and encrypted message. Your partner decrypts by first RSA decrypting the key, and then using it to decrypt the message. One typical use of the RSA algorithm is to encrypt a symmetric encryption algorithm key so that it can be sent to

Download Chilkat RSA Public-Key Encryption ActiveX

Download Chilkat Crypt ActiveX

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

    DECLARE @success int

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

    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_OAMethod @crypt, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Crypt component unlock failed'
        RETURN
      END

    --  Load a public/private key pair from a .snk key file.
    --  Assume you have already provided your partner with
    --  the public key part of the key pair.
    DECLARE @xmlKey nvarchar(4000)

    EXEC sp_OAMethod @rsa, 'SnkToXml', @xmlKey OUT, 'chilkat2.snk'

    --  Alternatively, you may generate a public/private key pair
    --  by calling GenerateKey (as shown in the commented-out line
    --  below).  If you do this, comment out the lines that call
    --  ImportPrivateKey and ImportPublicKey.
    -- success = rsa.GenerateKey(2048);

    --  Import the private key into the RSA instance.
    EXEC sp_OAMethod @rsa, 'ImportPrivateKey', NULL, @xmlKey

    --  Our message data will be encrypted using 128-bit AES
    --  encryption, using CBC (cipher-block chaining).
    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'cbc'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 128

    --  Generate a 128-bit secret key from a passphrase and return it as a hex string.
    DECLARE @secretKey nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'GenEncodedSecretKey', @secretKey OUT, 'secret', 'hex'


    PRINT 'Unencrypted Key: ' + @secretKey

    --  Use the key we generated:
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @secretKey, 'hex'

    --  RSA encrypt the secret key and return as a hex string:
    EXEC sp_OASetProperty @rsa, 'EncodingMode', 'hex'
    DECLARE @bUsePrivateKey int

    SELECT @bUsePrivateKey = 0
    DECLARE @encryptedKey nvarchar(4000)

    EXEC sp_OAMethod @rsa, 'EncryptStringENC', @encryptedKey OUT, @secretKey, @bUsePrivateKey

    --  Symmetric encrypt a message.  For this example the message
    --  is very short, but typically this is where a large amount
    --  of data may be encrypted.
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'
    DECLARE @encryptedText nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encryptedText OUT, 'Hello World!'

    --  Show our encrypted key and encrypted text:


    PRINT 'Encrypted Key: ' + @encryptedKey


    PRINT 'Encrypted Text: ' + @encryptedText

    --  Assume we sent these strings to our partner...

    --  Here's what we do at the partner end:
    EXEC sp_OAMethod @rsa, 'ImportPublicKey', NULL, @xmlKey

    --  First, decrypt the encryptedKey:
    SELECT @bUsePrivateKey = 1
    DECLARE @decryptedKey nvarchar(4000)

    EXEC sp_OAMethod @rsa, 'DecryptStringENC', @decryptedKey OUT, @encryptedKey, @bUsePrivateKey


    PRINT 'Decrypted Key: ' + @decryptedKey

    --  Set our crypt object's properties and secret key:
    DECLARE @crypt2 int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt2 OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @crypt2, 'CryptAlgorithm', 'aes'
    EXEC sp_OASetProperty @crypt2, 'CipherMode', 'cbc'
    EXEC sp_OASetProperty @crypt2, 'KeyLength', 128
    EXEC sp_OASetProperty @crypt2, 'EncodingMode', 'base64'
    EXEC sp_OAMethod @crypt2, 'SetEncodedKey', NULL, @decryptedKey, 'hex'

    --  Decrypt the message:
    DECLARE @decryptedText nvarchar(4000)

    EXEC sp_OAMethod @crypt2, 'DecryptStringENC', @decryptedText OUT, @encryptedText


    PRINT 'Decrypted Text: ' + @decryptedText
END
GO

 

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

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