forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrambledKeyPair.cs
More file actions
60 lines (54 loc) · 2.3 KB
/
Copy pathScrambledKeyPair.cs
File metadata and controls
60 lines (54 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace L2dotNET.Encryption
{
public class ScrambledKeyPair
{
private AsymmetricCipherKeyPair _pair;
public byte[] ScrambledModulus;
public AsymmetricKeyParameter PrivateKey;
public ScrambledKeyPair(AsymmetricCipherKeyPair pPair)
{
_pair = pPair;
AsymmetricKeyParameter publicKey = pPair.getPublic();
RSAKeyParameters rsaKeyParameters = publicKey as RSAKeyParameters;
if (rsaKeyParameters != null)
ScrambledModulus = ScrambleModulus(rsaKeyParameters.getModulus());
PrivateKey = pPair.getPrivate();
}
public static AsymmetricCipherKeyPair GenKeyPair()
{
RSAKeyGenerationParameters generationParameters = new RSAKeyGenerationParameters(BigInteger.valueOf(65537L), new SecureRandom(), 1024, 10);
RSAKeyPairGenerator keyPairGenerator = new RSAKeyPairGenerator();
keyPairGenerator.init(generationParameters);
return keyPairGenerator.generateKeyPair();
}
public byte[] ScrambleModulus(BigInteger modulus)
{
byte[] numArray1 = modulus.toByteArray();
if ((numArray1.Length == 129) && (numArray1[0] == 0))
{
byte[] numArray2 = new byte[128];
Array.Copy(numArray1, 1, numArray2, 0, 128);
numArray1 = numArray2;
}
for (int index = 0; index < 4; ++index)
{
byte num = numArray1[index];
numArray1[index] = numArray1[77 + index];
numArray1[77 + index] = num;
}
for (int index = 0; index < 64; ++index)
numArray1[index] = (byte)(numArray1[index] ^ (uint)numArray1[64 + index]);
for (int index = 0; index < 4; ++index)
numArray1[13 + index] = (byte)(numArray1[13 + index] ^ (uint)numArray1[52 + index]);
for (int index = 0; index < 64; ++index)
numArray1[64 + index] = (byte)(numArray1[64 + index] ^ (uint)numArray1[index]);
return numArray1;
}
}
}