Skip to content

Commit

Permalink
Started work on Crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
BurakDev committed Sep 19, 2016
1 parent 7cf4d5e commit b2a904d
Show file tree
Hide file tree
Showing 12 changed files with 466 additions and 95 deletions.
3 changes: 3 additions & 0 deletions Yupi.Controller/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Yupi.Controller
using Yupi.Net;
using Yupi.Protocol;
using Yupi.Util;
using Yupi.Crypto;

public class ClientManager
{
Expand All @@ -45,6 +46,8 @@ public class ClientManager

private RoomManager RoomManager;

private Encryption Encryption;

#endregion Fields

#region Properties
Expand Down
9 changes: 7 additions & 2 deletions Yupi.Controller/Yupi.Controller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Folder Include="Items\" />
<Folder Include="Room\" />
<Folder Include="Room\Chat\" />
<Folder Include="Room\Pathfinding\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yupi.Crypto\Yupi.Crypto.csproj">
<Project>{81b025ac-b1ff-4e84-96ff-6f54859d0f1e}</Project>
<Name>Yupi.Crypto</Name>
</ProjectReference>
<ProjectReference Include="..\Yupi.Util\Yupi.Util.csproj">
<Project>{6CC9871D-F8D6-4586-8681-E9AAECA60D60}</Project>
<Name>Yupi.Util</Name>
Expand All @@ -116,4 +118,7 @@
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions Yupi.Crypto/Encryption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yupi.Crypto
{
public class Encryption
{

}
}
36 changes: 36 additions & 0 deletions Yupi.Crypto/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Yupi.Crypto")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Yupi.Crypto")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("81b025ac-b1ff-4e84-96ff-6f54859d0f1e")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
66 changes: 66 additions & 0 deletions Yupi.Crypto/Utils/BigIntegerMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Numerics;

namespace Yupi.Crypto.Utils
{
public static class BigIntegerMethods
{
public static int BitLength(this BigInteger scope)
{
if (scope <= 0)
{
return -1;
}

return (int)BigInteger.Log(scope - 1, 2);
}

public static int GetLowestSetBit(this BigInteger scope)
{
if (scope <= 0)
{
return -1;
}

return (int)BigInteger.Log(scope & -scope, 2);
}

public static BigInteger ModInverse(this BigInteger a, BigInteger n)
{
BigInteger i = n;
BigInteger v = 0;
BigInteger d = 1;

while (a > 0)
{
BigInteger t = i / a;
BigInteger x = a;
a = i % x;
i = x;
x = d;
d = v - t * x;
v = x;
}
v %= n;

if (v < 0)
{
v = (v + n) % n;
}

return v;
}

public static byte[] ToByteArray(this BigInteger scope, bool asLittleEndian)
{
byte[] result = scope.ToByteArray();

if (!asLittleEndian)
{
Array.Reverse(result);
}

return result;
}
}
}
87 changes: 87 additions & 0 deletions Yupi.Crypto/Utils/BigIntegerPrime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Numerics;
using System.Security.Cryptography;

namespace Yupi.Crypto.Utils
{
public static class BigIntegerPrime
{
public static BigInteger GeneratePseudoPrime(int bitLength, int certainty, RandomNumberGenerator random)
{
byte[] bytes = new byte[(bitLength + 7) / 8];

BigInteger result = 0;
do
{
random.GetBytes(bytes);
bytes[bytes.Length - 1] = 0;
result = new BigInteger(bytes);
}
while (result.IsProbablePrime(certainty, random));

return result;
}

public static bool IsProbablePrime(this BigInteger source, int certainty, RandomNumberGenerator random)

This comment has been minimized.

Copy link
@ovflowd

ovflowd Sep 20, 2016

Owner

That's a huge method omg..

{
if (source == 2 || source == 3)
return true;
if (source < 2 || source % 2 == 0)
return false;

BigInteger d = source - 1;
int s = 0;

while (d % 2 == 0)
{
d /= 2;
s += 1;
}


if (random == null)
{
random = Randomizer.GetRandom();
}

BigInteger copy = new BigInteger((int)source);
int bitLength = 0;
do
{
bitLength++;
copy /= 2;
} while (copy != 0);

byte[] bytes = new byte[(bitLength + 7) / 8];
BigInteger a;

for (int i = 0; i < certainty; i++)
{
do
{
random.GetBytes(bytes);
//bytes[bytes.Length - 1] = 0;
a = new BigInteger(bytes);
}
while (a < 2 || a >= source - 2);

BigInteger x = BigInteger.ModPow(a, d, source);
if (x == 1 || x == source - 1)
continue;

for (int r = 1; r < s; r++)
{
x = BigInteger.ModPow(x, 2, source);
if (x == 1)
return false;
if (x == source - 1)
break;
}

if (x != source - 1)
return false;
}

return true;
}
}
}
24 changes: 24 additions & 0 deletions Yupi.Crypto/Utils/Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Yupi.Crypto.Utils
{
public static class Converter
{
public static string BytesToHexString(byte[] bytes)
{
string hexstring = BitConverter.ToString(bytes);
return hexstring.Replace("-", "");
}

public static byte[] HexStringToBytes(string hexstring)
{
int NumberChars = hexstring.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hexstring.Substring(i, 2), 16);
}
return bytes;
}
}
}
52 changes: 52 additions & 0 deletions Yupi.Crypto/Utils/RSACUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Numerics;

namespace Yupi.Crypto.Utils
{
public static class RSACUtils
{
public static BigInteger Base64ToBigInteger(string data, bool asLittleEndian)
{
if (data == null || data == "")
{
return 0;
}

byte[] bytes = Convert.FromBase64String(data);

if (asLittleEndian)
{
Array.Reverse(bytes);
}

return new BigInteger(bytes);
}

public static BigInteger Base64ToBigInteger(string data)
{
return Base64ToBigInteger(data, true);
}

public static string BigIntegerToBase64(BigInteger data, bool asLittleEndian)
{
if (data == null || data == 0)
{
return null;
}

byte[] bytes = data.ToByteArray();

if (!asLittleEndian)
{
Array.Reverse(bytes);
}

return Convert.ToBase64String(bytes);
}

public static string BigIntegerToBase64(BigInteger data)
{
return BigIntegerToBase64(data, true);
}
}
}
19 changes: 19 additions & 0 deletions Yupi.Crypto/Utils/Randomizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Security.Cryptography;

namespace Yupi.Crypto.Utils
{
public static class Randomizer
{
private static RandomNumberGenerator _random;

public static RandomNumberGenerator GetRandom()
{
if (_random == null)
{
_random = new RNGCryptoServiceProvider();
}

return _random;
}
}
}
Loading

1 comment on commit b2a904d

@ovflowd
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BigIntegerMethods.cs can be removed if we move to .NET 4.5.2 as discussed in #146

Please sign in to comment.