I am so lost at the moment as to why I cannot get something so simple to work!
I am decrypting a value & appending it to a string, E.g.
MessageBox.Show("TEST 1: " & DecryptedValue & " WHY AM I BEING STRIPPED???")
The problem is, anything after the DecryptedValue is being stripped from the string.
A complete project / Form example is below that shows this issue occurring:
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim DecryptedValue As String = Decrypt("mDnz8JVmfUyYxkgZYYiFbw==", "IXZOfRxCtb4pQcu2")
MessageBox.Show("TEST 1: " & DecryptedValue & " WHY AM I BEING STRIPPED???")
Dim sw As New StringWriter
sw.Write("TEST 2 ")
sw.Write(DecryptedValue)
sw.Write("WHY AM I BEING STRIPPED???")
MessageBox.Show(sw.ToString())
Dim okstr As String = "Ok"
MessageBox.Show("When Anything " & "Else " & "Works " & okstr)
End Sub
Friend Function Decrypt(ByVal str As String, ByVal key As String) As String
Dim cipher As Byte() = Convert.FromBase64String(str)
Dim btKey As Byte() = Encoding.ASCII.GetBytes(key)
Dim decryptor As ICryptoTransform = New RijndaelManaged() With { _
.Mode = CipherMode.ECB, _
.Padding = PaddingMode.None _
}.CreateDecryptor(btKey, Nothing)
Dim ms As New MemoryStream(cipher)
Dim cs As New CryptoStream(ms, decryptor, CryptoStreamMode.Read)
Dim plain As Byte() = New Byte(cipher.Length - 1) {}
Dim count As Integer = cs.Read(plain, 0, plain.Length)
ms.Close()
cs.Close()
Return Encoding.UTF8.GetString(plain, 0, count)
End Function
End Class
Has anyone run into this issue in the past / can spot something really basic I am looking over?
If the decrypted return includes a Null (Chr(0)) not much will 'see' the text appended. This is because Chr(0)/Null is the string termination character.
Dim sb As New StringBuilder
sb.Append("first part of string" & Convert.ToChar(0))
sb.Append("this is added")
Console.WriteLine(sb.ToString)
Console.WriteLine(sb.ToString.Length)
Only the first part will display, but the length will report 34 which also includes the embedded Null. The string created is in fact 34 chars long, but most everything stops at Chr(0).
The Console wont even see the newline which is sent after the entire string, so the display is:
first part of string34
For more information on how/why this works, see this answer