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

Duplicate Java's PBEWithMD5AndDES

Demonstrate's how to duplicate Java's PBEWithMD5AndDES.

Download Chilkat Crypt ActiveX

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    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
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'pbes1'
    EXEC sp_OASetProperty @crypt, 'PbesPassword', 'secret'

    EXEC sp_OASetProperty @crypt, 'PbesAlgorithm', 'des'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 64

    --  The salt for PBKDF1 is always 8 bytes:
    EXEC sp_OAMethod @crypt, 'SetEncodedSalt', NULL, 'C773218C7EC8EE99', 'hex'

    EXEC sp_OASetProperty @crypt, 'IterationCount', 20

    --  A hash algorithm needs to be set for PBES1:
    EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'md5'

    --  Indicate that the encrypted bytes should be returned
    --  as a hex string:
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex'

    DECLARE @plainText nvarchar(4000)

    SELECT @plainText = 'This is another example'

    DECLARE @encryptedText nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encryptedText OUT, @plainText

    --  The output should be: E729548140D20B14E9140F832A7E980AA449A84B295C75CE

    PRINT @encryptedText

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

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


    PRINT @decryptedText

    --  ----------------------------------------------------------------
    --  ----------------------------------------------------------------
    --  
The code above duplicates this Java JCE example:

	public static void main(String args[]) throws Exception
	{
	    PBEKeySpec pbeKeySpec;
	    PBEParameterSpec pbeParamSpec;
	    SecretKeyFactory keyFac;

	    // Salt
	    byte[] salt = {
		(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
		(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
	    };

	    // Iteration count
	    int count = 20;

	    // Create PBE parameter set
	    pbeParamSpec = new PBEParameterSpec(salt, count);

	    String password = "secret";
	    pbeKeySpec = new PBEKeySpec(password.toCharArray());
	    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
	    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

	    // Create PBE Cipher
	    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

	    // Initialize PBE Cipher with key and parameters
	    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

	    // Our cleartext
	    byte[] cleartext = "This is another example".getBytes();
	    
	    // Encrypt the cleartext
	    byte[] ciphertext = pbeCipher.doFinal(cleartext);
	    
     	System.out.print("ciphertext: ");
        System.out.println(toHex(ciphertext));
	}

END
GO

 

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