傳送信封資料訊息
[CAPICOM 是 32 位的僅限元件,可用於下列作業系統:Windows Server 2008、Windows Vista 和 Windows XP。 請改用.NET Framework來實作安全性功能。 如需詳細資訊,請參閱 使用 CAPICOM 的替代方案。]
在下列範例中,會從檔案讀取純文字訊息,並開啟包含預定郵件收件者憑證的憑證存放區。 存放區中的所有憑證都會新增為郵件的收件者、郵件會信封,而信封郵件會寫入檔案。
您可以新增其他程式碼,以顯示要新增為郵件收件者的憑證、在新增為預定收件者之前驗證這些憑證,或選擇要新增的憑證。
在任何 CAPICOM 錯誤上,會傳回負數十進位值為 Err.Number 。 如需詳細資訊,請參閱 CAPICOM_ERROR_CODE。 如需 Err.Number的正十進位值相關資訊,請參閱 Winerror.h。
Sub Envelope(ByVal InFile As String, ByVal OutFile As String, _
ByVal StoreName As String)
On Error GoTo ErrorHandler
'Open the input file and read the message to be enveloped.
Dim Text As String
Open InFile For Input As #1
Input #1, Text
Close #1
If Len(Text) < 1 Then
MsgBox "No message to be enveloped."
Exit Sub
End If
'Open the store containing the certificates of
'the message recipients.
Dim CertStore As New Store
CertStore.Open CAPICOM_CURRENT_USER_STORE, StoreName, _
CAPICOM_STORE_OPEN_READ_ONLY
' Check for an empty certificate store.
If CertStore.Certificates.Count < 1 Then
MsgBox "There are no recipient certificates available."
Set CertStore = Nothing
Exit Sub
End If
' Declare and initialize an EnvelopedData object
Dim EnvMessage As New EnvelopedData
EnvMessage.Content = Text
Dim I As Integer
For I = 1 To CertStore.Certificates.Count
EnvMessage.Recipients.Add CertStore.Certificates.Item(I)
' The following line can be uncommented to see a prompt
' for each certificate in the store.
' CertStore.Certificates.Item(I).Display
Next I
' Set the encryption algorithm and key length. Comment out
' or remove the following lines to use the default algorithm
' and key length. The triple DES algorithm and 128 bit key
' length may not be supported.
Envmessage.Algorithm.Name = ENCRYPTION_ALGORITHM_RC4
Envmessage.Algorithm.KeyLength = KEY_LENGTH_128_BITS
'Declare the string that will hold the enveloped message.
Dim EnvelopedMessage As String
'Encrypt the message into EnvelopedMessage. The enveloped message
'string contains everything that an intended recipient will need to
'decrypt the message, including information on the
'encryption algorithm key length.
EnvelopedMessage = EnvMessage.Encrypt
' Optionally, check the length of the encrypted string to make sure
' that the encrypt method worked.
If Len(EnvelopedMessage) < 1 Then
MsgBox "no message encrypted. "
Else
MsgBox " Message is " & Len(EnvelopedMessage) & " characters"
' Open an output file and write the encrypted message to the
' file. The file is not opened if there is no message to write.
Open OutFile For Output As #2
Write #2, EnvelopedMessage
Close #2
MsgBox "The message written to file "
End If
' Release the EncryptedData object and the Store object.
Set Envmessage = Nothing
Set CertStore = Nothing
Exit Sub
ErrorHandler:
If Err.Number > 0 Then
MsgBox "Visual Basic error found : " & Err.Description
Else
MsgBox "CAPICOM error found : " & Err.Number
End If
End Sub