Sample code for 30+ languages & platforms
SQL Server

Duplicate OpensSSL Command that Decrypts Binary DER

See more OpenSSL Examples

This example duplicates the following:
openssl smime -decrypt -in INPUT_FILE -inform der -binary -out OUTPUT_FILE -recip PEM_CERT_AND_KEY -passin pass:PRIVKEY_PASSWORD

Note: Although "smime" is the OpenSSL command, we're not really dealing with S/MIME. The arguments "-inform der -binary" indicate that the input is simply the binary DER (i.e. the PKCS7 binary encrypted object). The output can be any type of file (whatever was encrypted).

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    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_OASetProperty @crypt, 'CryptAlgorithm', 'pki'

    DECLARE @pem int
    EXEC @hr = sp_OACreate 'Chilkat.Pem', @pem OUT

    EXEC sp_OAMethod @pem, 'LoadPemFile', @success OUT, 'qa_data/pem/myPem.pem', 'password'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @pem
        RETURN
      END

    DECLARE @privkey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privkey OUT

    EXEC sp_OAMethod @pem, 'PrivateKeyAt', @success OUT, 0, @privkey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @pem
        EXEC @hr = sp_OADestroy @privkey
        RETURN
      END

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @pem, 'CertAt', @success OUT, 0, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pem, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @pem
        EXEC @hr = sp_OADestroy @privkey
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    EXEC sp_OAMethod @crypt, 'SetDecryptCert2', @success OUT, @cert, @privkey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @pem
        EXEC @hr = sp_OADestroy @privkey
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    EXEC sp_OAMethod @crypt, 'CkDecryptFile', @success OUT, 'qa_data/infile.enc', 'qa_output/outfile.dat'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @pem
        EXEC @hr = sp_OADestroy @privkey
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @pem
    EXEC @hr = sp_OADestroy @privkey
    EXEC @hr = sp_OADestroy @cert


END
GO