mirror of
https://github.com/raphaeIl/Novaria.git
synced 2025-12-12 22:44:35 +01:00
dh fully works, refactor packet handling stuff
This commit is contained in:
41
Novaria.Common/Crypto/DiffieHellman.cs
Normal file
41
Novaria.Common/Crypto/DiffieHellman.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Novaria.Common.Util;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Novaria.Common.Crypto
|
||||
{
|
||||
public class DiffieHellman : Singleton<DiffieHellman>
|
||||
{
|
||||
private BigInteger p = BigInteger.Parse("1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919");
|
||||
|
||||
private BigInteger g = 2;
|
||||
|
||||
private BigInteger spriv = new BigInteger(new byte[] { 1, 2, 3, 4 }); // hardcoded server priv key
|
||||
|
||||
public BigInteger ServerPublicKey { get; set; }
|
||||
|
||||
public DiffieHellman()
|
||||
{
|
||||
//g** Spriv mod p
|
||||
ServerPublicKey = BigInteger.ModPow(g, spriv, p);
|
||||
}
|
||||
|
||||
public byte[] CalculateKey(byte[] clientPubKey) // server calculates key like this
|
||||
{
|
||||
BigInteger clientPubKeyInt = new BigInteger(clientPubKey.Reverse().ToArray());
|
||||
|
||||
//Cpub**Spriv mod p
|
||||
return BigInteger.ModPow(clientPubKeyInt, spriv, p).ToByteArray(true, true)[..32];
|
||||
}
|
||||
|
||||
// this is for pcap parsing use only, officalServerPubKey is in the first IKE response, client priv will be pcaped too
|
||||
public byte[] CalculateKey(byte[] officalServerPubKey, byte[] officialClientPriv)
|
||||
{
|
||||
BigInteger officalServerPubKeyInt = new BigInteger(officalServerPubKey.Reverse().ToArray());
|
||||
BigInteger officialClientPrivInt = new BigInteger(officialClientPriv.Reverse().ToArray());
|
||||
|
||||
BigInteger result = BigInteger.ModPow(officalServerPubKeyInt, officialClientPrivInt, p);
|
||||
|
||||
return result.ToByteArray(true, true)[..32];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user