Haberler:

Forum kuralları güncellendi LÜTFEN  okuyunuz:  https://bit.ly/2IjR3ME

Ana Menü

AXORLib

Başlatan ABITGAN, 10 Haziran 2014, 00:39:26

ABITGAN

public class AXORLib
{
	public enum PrintType { CHAR, HEX };

	public string toXOR(string text, string code, PrintType printType)
	{
		string result = null;
		int xorKeyForText = 0;
		int xorKeyForCode = 0;

		int orderCode = 0;
		int counter = 0;
		int lengthText = text.Length;
		int lengthCode = code.Length;

		byte[] arrayText = System.Text.Encoding.UTF8.GetBytes(text);
		byte[] arrayCode = System.Text.Encoding.UTF8.GetBytes(code);

		for (counter = 0; counter < lengthText; counter++)
		{
			xorKeyForText = (int)arrayText[counter];

			orderCode++;
			if (orderCode > lengthCode)
				orderCode = 1;

			xorKeyForCode = (int)arrayCode[orderCode - 1];

			switch (printType)
			{
				case PrintType.CHAR:
					result += (char)(xorKeyForText ^ xorKeyForCode);
					break;
				case PrintType.HEX:
					result += (xorKeyForText ^ xorKeyForCode).ToString("X2");
					break;
				default:
					break;
			}
		}

		return result;
	}

	public string fromXOR(string text, string code, PrintType printType)
	{
		string result = null;
		int xorKeyForText = 0;
		int xorKeyForCode = 0;

		int orderCode = 0;
		int counter = 0;
		int temp = 0;
		int lengthText = text.Length;
		int lengthCode = code.Length;
		bool isValid = true;

		byte[] arrayText = new byte[lengthText / 2];
		byte[] arrayCode = System.Text.Encoding.UTF8.GetBytes(code);

		if (printType == PrintType.HEX && !System.Text.RegularExpressions.Regex.IsMatch(text, @"\A\b[0-9a-fA-F]+\b\Z"))
			isValid = false;

		if (isValid)
		{
			if (printType == PrintType.HEX)
			{
				for (counter = 0; counter < lengthText; counter += 2)
				{
					arrayText[temp] = Convert.ToByte(text.Substring(counter, 2), 16);
					temp++;
				}
			}
			else
				arrayText = System.Text.Encoding.UTF8.GetBytes(text);

			for (counter = 0; counter < lengthText; counter++)
			{
				xorKeyForText = (int)arrayText[counter];

				orderCode += 1;
				if (orderCode > lengthCode)
					orderCode = 1;

				xorKeyForCode = (int)arrayCode[orderCode - 1];

				result += (char)(xorKeyForText ^ xorKeyForCode);
			}
		}
		else
			result = "Error : Text is invalid!";

		return result;
	}
}

muhittin_kaplan