CryptoSys PKI Toolkit Manual

CMS_ReadEnvDataToString

Reads and decrypts CMS enveloped-data object using the recipient's private key writing the plaintext data directly into a string.

VB6/VBA Syntax

Public Declare Function CMS_ReadEnvDataToString Lib "diCrPKI.dll" (ByVal strDataOut As String, ByVal nDataLen As Long, ByVal strFileIn As String, ByVal strCertFile As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long

nRet = CMS_ReadEnvDataToString(strDataOut, nDataLen, strFileIn, strCertFile, strPrivateKey, nOptions) As Long

Parameters

strDataOut
[out] String to receive output plaintext.
nDataLen
[in] Long specifying the length of the output string.
strFileIn
[in] String with name of file containing input data or the data as a base64 or PEM string.
strCertFile
[in] String (optional) specifies the filename of the recipient's X.509 certificate.
strPrivateKey
[in] String containing the recipient's private key in string format.
nOptions
[in] Long option flags:
PKI_DEFAULT (0) for default options
PKI_CMS_FORMAT_BASE64 to read base64-encoded input (default expected BER-encoded binary) [not required in v3.5 and above]

C/C++ Syntax

long _stdcall CMS_ReadEnvDataToString(char *szDataOut, long nDataOutLen, const char *szFileIn, const char *szX509File, const char *szRSAPrivateKey, long nOptions);

Returns (VB6/C)

Long: If successful, the return value is the number of characters in the decrypted plaintext; otherwise it returns a negative error code.

.NET Equivalent

Cms.ReadEnvDataToString Method

Remarks

See the remarks for CMS_ReadEnvData above. This function assumes the output is plain ANSI text with no embedded NUL (zero) characters. Call the function with an empty or NULL strDataOut and zero nDataLen parameters to find out the required length of output string. C/C++ users should add one to this before allocating memory. Alternatively, use the CMS_QueryEnvData function with the query "sizeofEncryptedContent". This will return an upper bound on the length of the decrypted plaintext, at most 16 bytes too long. Calling CMS_ReadEnvData with a properly-sized output buffer will return the exact size of the recovered plaintext. The buffer must be large enough to receive the entire output or a SHORT_BUF_ERROR error will result.

Example

The following example reads the file created with CMS_MakeEnvData above. Bob's private key needs to be read into a string first (see RSA_ReadEncPrivateKey). The output is written into a string.

    Dim strPrivateKey As String
    Dim strFileIn As String
    Dim strDataOut As String
    Dim nLen As Long
    Dim strCertFile As String
    
    strFileIn = "C:\test\cms2bobandcarl.p7m"
    
    ' First, Bob reads his private key into a string
    strPrivateKey = rsaReadPrivateKey("C:\Test\BobPrivRSAEncrypt.epk", "password")
    If Len(strPrivateKey) = 0 Then
        MsgBox "Cannot read private key"
        Exit Sub
    End If
    
    ' Query the size of encrypted content (no need for an output buffer)
    nLen = CMS_QueryEnvData("", 0, strFileIn, "sizeofEncryptedContent", 0)
    Debug.Print "CMS_QueryEnvData returns " & nLen
    
    If nLen <= 0 Then
        GoTo CleanUp
    End If
    
    ' Pre-dimension string and read in the plaintext
    ' The final plaintext will always be shorter than the encrypted content.
    strDataOut = String(nLen, " ")
    nLen = CMS_ReadEnvDataToString(strDataOut, nLen, _
        strFileIn, "", strPrivateKey, 0)
    Debug.Print "CMS_ReadEnvDataToString returns " & nLen
    If nLen > 0 Then
        ' Fix correct size for final, unpadded plaintext
        strDataOut = Left(strDataOut, nLen)
        Debug.Print "Plaintext is '" & strDataOut & "'"
    End If
    
CleanUp:
    WIPE_String strPrivateKey, Len(strPrivateKey)
    strPrivateKey = ""

See Also

CMS_ReadEnvData CMS_MakeEnvDataFromString CMS_MakeEnvData CMS_QueryEnvData

[Contents] [Index]

[HOME]   [NEXT: CMS_ReadSigData...]

Copyright © 2004-10 D.I. Management Services Pty Ltd. All rights reserved.