-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
466 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
{ | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
1 comment
on commit b2a904d
There was a problem hiding this comment.
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
That's a huge method omg..