[RESOLVED] Help resolve this SIMPLE problem
First off I am SO tired it's not even funny! :blush:
I am trying to make sure my encrypt and decrypt functions are working
properly.
I was testing them out when I accidentally lost my work.
I have put everything back together and I'm exhausted and I can't figure
out where I am going wrong with this.
Basically I'm taking a value from textbox1 and decrypting it and showing that
value in textbox 2. Simple right?
Here is the code for the decrypt button, how would I then call for the value
in textbox 2?
Code:
Dim strArray As String() = Me.Decrypt(TextBox1.Text).Split(New Char() {"|"c})
TextBox2 = ?
Here is the decrypt function:
Code:
Public Function Decrypt(ByVal ciphertext As String) As String
Dim str2 As String
Dim str3 As String = DateTime.UtcNow.Hour.ToString.PadLeft(2, "0"c)
Dim bytes As Byte() = Encoding.UTF8.GetBytes(("GpWhFbntD" & str3.ToString & "Qpop38D@dUnrF"))
Dim rgbIV As Byte() = Encoding.UTF8.GetBytes("wFyEQPdN")
Dim provider As New TripleDESCryptoServiceProvider With { _
.BlockSize = &H40, _
.Padding = PaddingMode.Zeros _
}
Using stream As MemoryStream = New MemoryStream
Using stream2 As CryptoStream = New CryptoStream(stream, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write)
Dim buffer As Byte() = Convert.FromBase64String(ciphertext)
stream2.Write(buffer, 0, buffer.Length)
stream2.FlushFinalBlock()
str2 = Encoding.UTF8.GetString(stream.ToArray)
End Using
End Using
Return str2
End Function
-Chris
Re: Help resolve this SIMPLE problem
I'm a little confused, are you just creating a string "strArray" and putting the decrypted textbox1.text into it? then wouldn't you just simply say:
Code:
TextBox2.text = strArray
This almost seems too simple to be your problem. Am I misunderstanding your question? But then again, I don't know what your Decrypt sub doesn, and I never used the Split command before, so I could be completely off, but I thought I would suggest something.
Re: Help resolve this SIMPLE problem
Yeah, that's not going to work. I've already tried that.
Thanks.
Re: Help resolve this SIMPLE problem
Are you getting any errors? Type conversions? anything to go off of?
I didn't just offer a (stupid lol) solution, but I also asked some questions to help clarify the situation. Its not that it 'doesn't work' its 'why doesn't it work'.
Is the problem located in the dimention line? perhaps dimention on one line, and then set the variable on another? or, decrypt before you split? that way you can output the contents of strArray after each step and see where the problem is.
I know that's not much of a 'solution' but without being in front of your code, that's all I can suggest.
I really hope that helps.
Re: Help resolve this SIMPLE problem
I've just updated my first post with the decrypt function.
No errors. Just weird characters in the return of the decrypt string.
I input an encrypted string and the decrypt will either return TRUE or FALSE
-Chris
Re: Help resolve this SIMPLE problem
one more question, lets say you encrypt the value "hello"
you decrypt it and the value ends up being 'true' meaning the the enctyption was correct.
what then do you want in textbox2? do you want the encrypted text, or "hello", or "true"?
Re: Help resolve this SIMPLE problem
right now I am trying to decrypt an encrypted string in textbox1 and return the decrypted value into textbox2, so I want the return in textbox2 to show either TRUE or FALSE.
Re: Help resolve this SIMPLE problem
Well, this really is beyond my knowledge set, but now this thread has a lot more information that will help someone with the know-how understand your problem better. Sorry I couldn't be of much help.
Re: Help resolve this SIMPLE problem
No problem. Thanks stepdragon.
Re: Help resolve this SIMPLE problem
Whew...I fixed it! :)
Code:
TextBox2.Text = strArray(0).ToString
Re: [RESOLVED] Help resolve this SIMPLE problem
Your Decrypt() function returns a string. So, there is no need of storing it in a String array !
So, it would look like this:
vb.net Code:
Dim strString As String = Me.Decrypt(TextBox1.Text).Split(New Char() {"|"c})
TextBox2.Text = strString
:wave:
Re: [RESOLVED] Help resolve this SIMPLE problem
This is the error I get when I use your code:
Value of type '1-dimensional array of String' cannot be converted to 'String'.
Re: [RESOLVED] Help resolve this SIMPLE problem
this is similar to my question PLEES HEELP! =-C
http://www.vbforums.com/showthread.php?p=4053148
Re: [RESOLVED] Help resolve this SIMPLE problem
Quote:
Originally Posted by
cmmorris1
This is the error I get when I use your code:
Value of type '1-dimensional array of String' cannot be converted to 'String'.
Sorry, I didn't saw that you were passing splitted array of strings as parameter to the Decrypt() function.
See if this works:
vb.net Code:
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'~~~ empty the Textbox2
TextBox2.Text = ""
'~~~ Split the contents in Textbox1 delimited by "|" into an array
Dim strArray() As String = TextBox1.Text.Split(New Char() {"|"c}) '~~~ holds the splitted contents
'~~~ Loop through the array and decrypt it and store the decrypted string in the same array
For i As Integer = strArray.GetLowerBound(0) To strArray.GetUpperBound(0)
strArray(i) = Me.Decrypt(strArray(i))
Next
'~~~ Join the array back to a string (opposite of "split"), delimited by a "||"
TextBox2.Text = String.Join("||", strArray)
End Sub
Public Function Decrypt(ByVal ciphertext As String) As String
Dim str2 As String
Dim str3 As String = DateTime.UtcNow.Hour.ToString.PadLeft(2, "0"c)
Dim bytes As Byte() = Encoding.UTF8.GetBytes(("GpWhFbntD" & str3.ToString & "Qpop38D@dUnrF"))
Dim rgbIV As Byte() = Encoding.UTF8.GetBytes("wFyEQPdN")
Dim provider As New TripleDESCryptoServiceProvider With { _
.BlockSize = &H40, _
.Padding = PaddingMode.Zeros _
}
Using stream As MemoryStream = New MemoryStream
Using stream2 As CryptoStream = New CryptoStream(stream, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write)
Dim buffer As Byte() = Convert.FromBase64String(ciphertext)
stream2.Write(buffer, 0, buffer.Length)
stream2.FlushFinalBlock()
str2 = Encoding.UTF8.GetString(stream.ToArray)
End Using
End Using
Return str2
End Function
End Class
:wave: