Sinisterly
Word Encoder/Decoder | Made using string and stringbuilder class - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Visual Basic & .NET Framework (https://sinister.li/Forum-Visual-Basic-NET-Framework)
+--- Thread: Word Encoder/Decoder | Made using string and stringbuilder class (/Thread-Word-Encoder-Decoder-Made-using-string-and-stringbuilder-class)



Word Encoder/Decoder | Made using string and stringbuilder class - #Swag_mybb_import6403 - 05-20-2012

So my professor gave the assignment of making a word encoder/decoder using just the string and stringbuilder classes. I have no use for this project though besides getting a good grade in class so I've decided to share it here. I know it's very easy to crack but it's hard to make something very secure using just the string and stringbuilder class. Also here's an image for it:
[Image: iE5shEQeczDCj.gif]

Source: http://www.mediafire.com/?bgfsotsdow5u006

So what do you guys think about it?


RE: Word Encoder/Decoder | Made using string and stringbuilder class - Reaper_mybb_import6988 - 05-20-2012

Very, very nice! I like it.

How long did you need for it?


RE: Word Encoder/Decoder | Made using string and stringbuilder class - #Swag_mybb_import6403 - 05-20-2012

(05-20-2012, 01:17 AM)Don. Wrote: Very, very nice! I like it.

How long did you need for it?

10 mins coding and 5 mins commenting out everything. Wasn't very challenging lol


RE: Word Encoder/Decoder | Made using string and stringbuilder class - ArkPhaze - 05-20-2012

Using methods that are already part of the .NET library is not good. There is actually already a method named Replace(), which you've overwritten here, and looping through the char values with "chr" there's also the Chr() function. Things like this are bad habit.


RE: Word Encoder/Decoder | Made using string and stringbuilder class - ArkPhaze - 05-20-2012

Try this:
Code:
Private Const alpha = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890`~!@#$%^&*()-_=+{}|[]\:"";'<>?,./ " Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Encrypt_Decrypt(TextBox1.Text) End Sub Private Sub Encrypt_Decrypt(FullText As String) Dim sb As New StringBuilder Array.ForEach(FullText.ToCharArray, Sub(c) sb.Append(If(alpha.Contains(c), alpha.Reverse()(alpha.IndexOf(c)), c))) TextBox1.Text = sb.ToString sb.Clear() End Sub

[Image: pRZHP.gif]


RE: Word Encoder/Decoder | Made using string and stringbuilder class - #Swag_mybb_import6403 - 05-22-2012

(05-20-2012, 03:46 PM)PhazeShift Wrote: Using methods that are already part of the .NET library is not good. There is actually already a method named Replace(), which you've overwritten here, and looping through the char values with "chr" there's also the Chr() function. Things like this are bad habit.

I've never heard of the Chr() function before, thanks for letting me know about it. And when you say there's already a method named Replace, I didn't really overwrite that as I'm still using .replace inside the sub. I just made a sub for that to shorten my code and called it that.

(05-20-2012, 04:50 PM)PhazeShift Wrote: Try this:
Code:
Private Const alpha = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890`~!@#$%^&*()-_=+{}|[]\:"";'<>?,./ " Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Encrypt_Decrypt(TextBox1.Text) End Sub Private Sub Encrypt_Decrypt(FullText As String) Dim sb As New StringBuilder Array.ForEach(FullText.ToCharArray, Sub(c) sb.Append(If(alpha.Contains(c), alpha.Reverse()(alpha.IndexOf(c)), c))) TextBox1.Text = sb.ToString sb.Clear() End Sub

[Image: pRZHP.gif]

Yet again, you've found a easier and shorter way to create what I've done. Thanks for the tips and maybe one day I'll be simplifying your code for you Smile


RE: Word Encoder/Decoder | Made using string and stringbuilder class - HrDe - 05-22-2012

Cool work done, keep up man!


RE: Word Encoder/Decoder | Made using string and stringbuilder class - ArkPhaze - 05-22-2012

Quote:I've never heard of the Chr() function before, thanks for letting me know about it.

Still, best advice I have here for you is to keep things unique. If you create methods or variables which can be overriden easily (especially enums), that pre-existing one default with the .NET libraries is now invisible when you want to use it.

Here's the Chr() Function for you: http://msdn.microsoft.com/en-us/library/613dxh46(v=vs.71).aspx

There's both Chr() and ChrW().

Quote:And when you say there's already a method named Replace, I didn't really overwrite that as I'm still using .replace inside the sub. I just made a sub for that to shorten my code and called it that.

Oh... no Smile You've actually overriden Replace(), not String.Replace() as the extension method.

This one:
Code:
Dim t As String = "test 123" t = Replace(t, "test", "testing")

Look:
Code:
// Code size 48 (0x30) .maxstack 6 .locals init ([0] string str1, [1] string str2) IL_0000: nop IL_0001: ldstr "test2" IL_0006: ldstr "test" IL_000b: ldstr "testing" IL_0010: ldc.i4.1 IL_0011: ldc.i4.m1 IL_0012: ldc.i4.0 IL_0013: call string [Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::Replace(string, string, string, int32, int32, valuetype [Microsoft.VisualBasic]Microsoft.VisualBasic.CompareMethod) IL_0018: stloc.0 IL_0019: ldstr "test1" IL_001e: ldstr "test" IL_0023: ldstr "testing" IL_0028: callvirt instance string [mscorlib]System.String::Replace(string, string) IL_002d: stloc.1 IL_002e: nop IL_002f: ret

There's one from the VisualBasic methods, and the System.String namespace for VB in terms of Replace functions. I think you're also just unaware that the VisualBasic Replace() function existed Smile

My homework for you:
-Research Chr()
-Research Replace()

Here's the Replace functions, both the one you are thinking i'm talking about, and the one I am talking about:
http://msdn.microsoft.com/en-us/library/bt3szac5(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.replace.aspx

Quote:Yet again, you've found a easier and shorter way to create what I've done. Thanks for the tips and maybe one day I'll be simplifying your code for you

Just keep at it, i've been using .NET for quite a while now. And I practically use it everyday still.


RE: Word Encoder/Decoder | Made using string and stringbuilder class - #Swag_mybb_import6403 - 05-23-2012

Yea I read up on the chr() function right after you mentioned it, and thanks for clearing that up. I'll keep this in mind for future projects.