Hack Forums
[C#] [C#][VB .NET][SRC]XOR Class - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [C#][VB .NET][SRC]XOR Class (/showthread.php?tid=1000034)



[C#][VB .NET][SRC]XOR Class - Ethereal - Jan 23, 2011

The original source was from Aeonhack. I updated it with string parameter overloads, and also translated it into C# for my fellow coders.

So here it is:

C#
Code
class XOREncryption
{
    /// <summary>
    /// Encrypts your data via XOR. Original source by Aeonhack. Updated and translated by Ethereal of HackForums.net
    /// Leave the credits.
    /// </summary>
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

    public string XORData(string data, string password) {  
  return encoding.GetString(XORData(encoding.GetBytes(data), encoding.GetBytes(password)));
    }

    public byte[] XORData(byte[] data, byte[] password)
    {
  int x = password.Length;
  for (int index = 0; index < data.Length; index++)
  {
    data[index] = Convert.ToByte(data[index] ^ password[index % x]);
  }
  return data;
    }

}

Usage:
Code
txtBox.Text = XOREncryption.XORData("Test text", "TestKeyVal");
txtBox.Text = XOREncryption.XORData(YourByteArrData, YourByteArrPass);

VB .NET
Code
Class XOREncryption
    'Original Byte() function by Aeonhack. Updated by Ethereal of HackForums.net
    'Leave the credits.
    Dim encoding As New System.Text.UTF8Encoding()

    Function XORData(ByVal data As String, ByVal password As String) As String
  Return encoding.GetString(XORData(encoding.GetBytes(data), encoding.GetBytes(password)))
    End Function

    Function XORData(ByVal data As Byte(), ByVal password As Byte()) As Byte()
  Dim X As Integer = password.Length
  For I As Integer = 0 To data.Length - 1
    data(I) = data(I) Xor password(I Mod X)
  Next
  Return data
    End Function
End Class

Usage:
Code
TextBox1.Text = XOREncryption.XORData("Test Data", "Test Pass")
Dim ByteArr As Byte() = XOREncryption.XORData(YourByteArrData, YourByteArrPass)



* - RiperFish - Jan 23, 2011

Thank's for the share bro :).


* - Cody8295 - Jan 23, 2011

Nice share, thanks for this.


* - Mr. Anderson - Jan 23, 2011

Good thing your ported it for us C# coders! thanks!


* - .Astro - Jan 23, 2011

Nice share, Ethereal.~
Another thing that can help me learn about encryption.~


* - Zer0Fact0r - Jan 23, 2011

Why not just make the string overload call the byte overload?
Also your for loop will miss the last item in the data byte[].


* - Ethereal - Jan 24, 2011

(Jan 23, 2011 04:29 PM)Zer0Fact0r Wrote:  Why not just make the string overload call the byte overload?
Also your for loop will miss the last item in the data byte[].

Thanks for notifying me of that - I will update it tonight.

I created two seperate functions for readability.




* - Nonmod - Jan 24, 2011

PHP Code

<?php 
for (int index = 0; index < sData.Length - 1; index++) 

You will miss the last item.

It should be:
PHP Code

<?php 
for (int index = 0; index < sData.Length; index++) 



* - Ethereal - Jan 24, 2011

(Jan 24, 2011 12:17 AM)Nonmod Wrote:  
PHP Code

<?php 
for (int index = 0; index < sData.Length - 1; index++) 

You will miss the last item.

It should be:
PHP Code

<?php 
for (int index = 0; index < sData.Length; index++) 

I did notice that, but I was just working from the original source be Aeonhack.

I will be sure to update it.

EDIT: Updated! The VB class was fine, but the C# class needed it changed. Made slightly more efficient for you, Zer0.