Skip to content

Commit

Permalink
Solve second challange
Browse files Browse the repository at this point in the history
  • Loading branch information
Foryah committed Dec 24, 2018
1 parent 4fd3924 commit 011abc1
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 7,537 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,7 @@ __pycache__
*.swp
*.o
*.out
*.mypy_cache
csharp/bin/*
csharp/obj/*

104 changes: 90 additions & 14 deletions csharp/Set1.cs
@@ -1,38 +1,114 @@
using System;
using System.Text;

namespace Set1 {
namespace Set1
{
public enum StringType {Hex, Base64, ASCII};

public class BaseChanger {
public class HeXor
{
private BaseChanger _baseInput;

public HeXor(BaseChanger baseInput)
{
_baseInput = baseInput;
}

public BaseChanger Xor(BaseChanger key)
{
byte[] baseBytes, keyBytes, xoredBytes;

baseBytes = _baseInput.ToBytes();
keyBytes = key.ToBytes();

xoredBytes = ByteByByteXor(baseBytes, keyBytes);

return new BaseChanger(xoredBytes);
}

private byte[] ByteByByteXor(byte[] baseBytes, byte[] keyBytes)
{
if (baseBytes.Length != keyBytes.Length)
{
throw new InvalidOperationException("The two strings must have the same length");
}

byte[] resultBytes = new byte[baseBytes.Length];
for (int i = 0; i < baseBytes.Length; i++)
{
resultBytes[i] = (byte)((int)baseBytes[i] ^ (int)keyBytes[i]);
}

return resultBytes;
}
}

public class BaseChanger
{
private byte[] _byteInput;

public BaseChanger(string initialString, StringType type) {
if (type == StringType.Hex) {
LoadHex(initialString);
public BaseChanger(string initialString, StringType type)
{
switch (type)
{
case StringType.Hex:
LoadHex(initialString);
break;
default:
throw new InvalidOperationException("Operation not supported!");
}
}

public BaseChanger(byte[] initialBytes)
{
_byteInput = initialBytes;
}

public BaseChanger(BaseChanger anotherBaseChanger)
{
_byteInput = anotherBaseChanger.ToBytes();
}

private void LoadHex(string hexString) {
_byteInput = new byte[hexString.Length / 2];
for (int i = 0; i < _byteInput.Length; i++) {
_byteInput[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
private void LoadHex(string hexInput)
{
_byteInput = new byte[hexInput.Length / 2];
for (int i = 0; i < _byteInput.Length; i++)
{
_byteInput[i] = Convert.ToByte(hexInput.Substring(i * 2, 2), 16);
}
}

public string ToAscii() => Encoding.ASCII.GetString(_byteInput);
public string ToBase64() => Convert.ToBase64String(_byteInput);
public string ToHex() => BitConverter.ToString(_byteInput).Replace("-", string.Empty);
public byte[] ToBytes() => _byteInput;
}

public static class Solver {
public static string HexToAscii(string hexString) {
BaseChanger bChanger = new BaseChanger(hexString, StringType.Hex);

public static class Solver
{
public static string HexToAscii(string hexInput)
{
BaseChanger bChanger = new BaseChanger(hexInput, StringType.Hex);
return bChanger.ToAscii();
}

public static string HexToBase64(string hexString) {
BaseChanger bChanger = new BaseChanger(hexString, StringType.Hex);
public static string HexToBase64(string hexInput)
{
BaseChanger bChanger = new BaseChanger(hexInput, StringType.Hex);
return bChanger.ToBase64();
}

public static string XorEqualHexes(string hexInput, string hexKey)
{
BaseChanger inputBaseChanger = new BaseChanger(hexInput, StringType.Hex);
BaseChanger keyBaseChanger = new BaseChanger(hexKey, StringType.Hex);
BaseChanger xoredBaseChanger;

HeXor hexxor = new HeXor(inputBaseChanger);
xoredBaseChanger = hexxor.Xor(keyBaseChanger);

return xoredBaseChanger.ToHex();
}
}
}
9 changes: 9 additions & 0 deletions csharp/Set1Test.cs
Expand Up @@ -26,4 +26,13 @@ public void Hex_to_base_64()
)
);
}

[Fact]
public void Xor_two_hex_strings()
{
Assert.Equal(
"746865206B696420646F6E277420706C6179",
Solver.XorEqualHexes( "1c0111001f010100061a024b53535009181c", "686974207468652062756c6c277320657965")
);
}
}

0 comments on commit 011abc1

Please sign in to comment.