|
-
Aug 14th, 2011, 06:20 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Help resolve this SIMPLE problem
First off I am SO tired it's not even funny! 
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
Last edited by cmmorris1; Aug 14th, 2011 at 08:48 PM.
-
Aug 14th, 2011, 08:32 PM
#2
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.
-
Aug 14th, 2011, 08:35 PM
#3
Thread Starter
Addicted Member
Re: Help resolve this SIMPLE problem
Yeah, that's not going to work. I've already tried that.
Thanks.
-
Aug 14th, 2011, 08:42 PM
#4
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.
-
Aug 14th, 2011, 08:49 PM
#5
Thread Starter
Addicted Member
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
-
Aug 14th, 2011, 08:52 PM
#6
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"?
-
Aug 14th, 2011, 08:54 PM
#7
Thread Starter
Addicted Member
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.
-
Aug 14th, 2011, 08:56 PM
#8
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.
-
Aug 14th, 2011, 09:00 PM
#9
Thread Starter
Addicted Member
Re: Help resolve this SIMPLE problem
No problem. Thanks stepdragon.
-
Aug 14th, 2011, 09:43 PM
#10
Thread Starter
Addicted Member
Re: Help resolve this SIMPLE problem
Whew...I fixed it! 
Code:
TextBox2.Text = strArray(0).ToString
-
Aug 14th, 2011, 09:53 PM
#11
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Aug 14th, 2011, 10:36 PM
#12
Thread Starter
Addicted Member
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'.
-
Aug 15th, 2011, 04:10 AM
#13
New Member
Re: [RESOLVED] Help resolve this SIMPLE problem
this is similar to my question PLEES HEELP! =-C
http://www.vbforums.com/showthread.php?p=4053148
-
Aug 15th, 2011, 07:57 AM
#14
Re: [RESOLVED] Help resolve this SIMPLE problem
 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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|