Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bazzilic committed Jan 19, 2022
1 parent d9b638a commit 4d2bcf1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<Description>Extension for the .NET Framework cryptography subsystem, which introduces the Pohlig-Hellman exponentiation cipher.</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aprismatic.BigIntegerExt" Version="0.1.8" />
<PackageReference Include="Aprismatic.BigIntegerExt" Version="0.1.10" />
</ItemGroup>
</Project>
16 changes: 6 additions & 10 deletions src/Aprismatic.PohligHellman/PohligHellman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public class PohligHellman : IDisposable
public PohligHellman(int keySize) {
_rng = RandomNumberGenerator.Create();
KeySize = keySize;
_keyStruct = CreateKeyPair();
var p = BigInteger.One.GenPseudoPrime(KeySize, 16, _rng);
_keyStruct = CreateKeyPair(p);
}

public PohligHellman(BigInteger P) {
public PohligHellman(BigInteger p) {
_rng = RandomNumberGenerator.Create();
KeySize = P.BitCount();
_keyStruct = CreateKeyPair(P);
KeySize = p.BitCount();
_keyStruct = CreateKeyPair(p);
}

// TODO: Consolidate constructors in one method
Expand All @@ -42,11 +43,6 @@ public PohligHellman(PohligHellmanParameters prms) {
public PohligHellman(string xml) : this(PohligHellmanParameters.FromXml(xml)) { }

// TODO: This method should probably move to KeyStruct
private PohligHellmanKeyStruct CreateKeyPair() {
var P = BigInteger.One.GenPseudoPrime(KeySize, 16, _rng);
return CreateKeyPair(P);
}

private PohligHellmanKeyStruct CreateKeyPair(BigInteger P) {
BigInteger E, D;
var PminusOne = P - BigInteger.One;
Expand All @@ -62,7 +58,7 @@ private PohligHellmanKeyStruct CreateKeyPair(BigInteger P) {

public byte[] EncryptData(BigInteger message) {
if (message < 0 || message > MaxEncryptableValue)
throw new ArgumentException($"Value should be 0 <= m <= {MaxEncryptableValue}", nameof(message));
throw new ArgumentException($"Value should be 0 <= m <= {MaxEncryptableValue} under the current encryption key", nameof(message));

var ctbs = _keyStruct.CiphertextLength;
var array = new byte[ctbs];
Expand Down
57 changes: 57 additions & 0 deletions test/PohligHellmanTests/MainTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Numerics;
using System.Security.Cryptography;
using Aprismatic;
using Aprismatic.PohligHellman;
using Xunit;
using Xunit.Abstractions;

namespace PohligHellmanTests
{
public class MainTests : IDisposable
{
private readonly ITestOutputHelper _output;

private readonly Random _rnd = new();
private readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();

public MainTests(ITestOutputHelper output) {
_output = output;
}

public void Dispose() {
_rng.Dispose();
}

[Fact(DisplayName = "Correctness")]
public void TestCorrectness() {
for (var i = 0; i < Globals.Iterations; i++) {
var ks = _rnd.Next(Globals.MinKeyLength, Globals.MaxKeyLength);
using var algorithm = new PohligHellman(ks);

Assert.Equal(ks, algorithm.KeySize);

var o = BigInteger.Zero.GenRandomBits(1, BigInteger.Pow(2, algorithm.KeySize - 1) - 1, _rng);

var o_enc = algorithm.EncryptData(o);
var o_dec = algorithm.DecryptData(o_enc);

Assert.Equal(o, o_dec);
}

for (var i = 0; i < Globals.Iterations; i++) {
var p = BigInteger.One.GenPseudoPrime(_rnd.Next(Globals.MinKeyLength, Globals.MaxKeyLength), 16, _rng);
using var algorithm = new PohligHellman(p);

Assert.Equal(p, algorithm.P);

var o = BigInteger.Zero.GenRandomBits(1, BigInteger.Pow(2, algorithm.KeySize - 1) - 1, _rng);

var o_enc = algorithm.EncryptData(o);
var o_dec = algorithm.DecryptData(o_enc);

Assert.Equal(o, o_dec);
}
}
}
}

0 comments on commit 4d2bcf1

Please sign in to comment.