-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABPHPack.cs
57 lines (46 loc) · 1.62 KB
/
ABPHPack.cs
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
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using System;
using System.Text;
namespace ABPPack
{
public class ABPHPack
{
public byte version = 0;
public byte[] data;
public ABPHPack() { }
public ABPHPack(byte[] x, RsaKeyParameters key)
{
this.version = x[0];
var enc = new byte[x.Length - 1];
Array.Copy(x, 1, enc, 0, enc.Length);
this.data = ABPRSA.Decrypt(enc, key);
}
public byte[] Pack(RsaKeyParameters key)
{
var enc = ABPRSA.Encrypt(this.data, key);
var x = new byte[1 + enc.Length];
x[0] = this.version;
Array.Copy(enc, 0, x, 1, enc.Length);
return x;
}
public static bool SelfTest(AsymmetricCipherKeyPair keypair)
{
string testInputStr = "Testing Testing 123!!!";
var testInput = Encoding.UTF8.GetBytes(testInputStr);
var pack = new ABPHPack { data = testInput };
var packBytes = pack.Pack((RsaKeyParameters)keypair.Public);
var decryptedPack = new ABPHPack(packBytes, (RsaKeyParameters)keypair.Private);
var decryptedStr = Encoding.UTF8.GetString(decryptedPack.data);
if (decryptedStr != testInputStr)
{
Console.Error.WriteLine($"[ABPHPack.SelfTest] Selftest failed: Decrypted doesn't match input!");
return false;
}
else
{
return true;
}
}
}
}