encrypt a string and write it out to a file

J

Jon Paal

need help with code to encrypt a string and write it out to a file.
code does not write out to file

=========code==============

Private Shared ReadOnly encryptionKeyBytes() As Byte = New Byte() {53, 53, 53, 53, 53, 53, 53, 53}
Private Shared Function GenerateEncryptedLicFile(ByVal inputText As String, ByVal outputFile As String) As Boolean

Dim outputStream As Stream = Nothing
Dim encryptedStream As Stream = Nothing
Dim result As Boolean = False

Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
des.Key = encryptionKeyBytes
des.IV = encryptionKeyBytes

Try
outputStream = New FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)

Dim desEncryptor As ICryptoTransform = des.CreateEncryptor()
encryptedStream = New CryptoStream(outputStream, desEncryptor, CryptoStreamMode.Write)

Dim contents() As Byte = des.EncryptValue(inputText)

encryptedStream.Write(contents, 0, contents.Length)
result = True

Finally
If Not encryptedStream Is Nothing Then
encryptedStream.Close()
encryptedStream = Nothing
End If
If Not outputStream Is Nothing Then
outputStream.Close()
outputStream = Nothing
End If
End Try

Return result
End Function
 
C

Chris Young

You will need to call the encryptedStream.Flush() method. Which causes
all unbuffered data to be written.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top