Sample code for 30+ languages & platforms
SQL Server

Write a PFX to a File

See more PFX/P12 Examples

Demonstrates Pfx.ToFile, which serializes the current PFX contents and writes the resulting PKCS#12 data to a file, using the supplied output password.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The .pfx and .p12 extensions both denote PKCS#12 files. The output password protects the private keys in the resulting file.

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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @pfx int
    EXEC @hr = sp_OACreate 'Chilkat.Pfx', @pfx OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  The file path is relative to the application's current working directory.  An absolute path
    --  may also be used.  Supply the path appropriate to your own environment.
    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'myPfxPassword'
    EXEC sp_OAMethod @pfx, 'LoadPfxFile', @success OUT, 'qa_data/identity.pfx', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        RETURN
      END

    --  Serialize the PFX contents and write the PKCS#12 data to a file, using the given output password.
    --  The output password should come from a secure source rather than being hard-coded.
    DECLARE @outputPassword nvarchar(4000)
    SELECT @outputPassword = 'myOutputPassword'
    EXEC sp_OAMethod @pfx, 'ToFile', @success OUT, @outputPassword, 'qa_output/identity.pfx'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        RETURN
      END

    PRINT 'PFX written to file.'

    EXEC @hr = sp_OADestroy @pfx


END
GO