encrypt a string and write it out to a file

  • Thread starter Thread starter Jon Paal
  • Start date Start date
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
 
You will need to call the encryptedStream.Flush() method. Which causes
all unbuffered data to be written.
 
Back
Top