TLSharp/TLSharp.Core/MTProto/Crypto/RSA.cs

84 lines
3 KiB
C#
Raw Permalink Normal View History

2015-09-28 04:01:17 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
2016-04-18 12:50:57 +02:00
namespace TLSharp.Core.MTProto.Crypto
{
2015-09-28 04:01:17 +02:00
2016-04-18 12:50:57 +02:00
class RSAServerKey
{
2015-09-28 04:01:17 +02:00
private string fingerprint;
private BigInteger m;
private BigInteger e;
2016-04-18 12:50:57 +02:00
public RSAServerKey(string fingerprint, BigInteger m, BigInteger e)
{
2015-09-28 04:01:17 +02:00
this.fingerprint = fingerprint;
this.m = m;
this.e = e;
}
2016-04-18 12:50:57 +02:00
public byte[] Encrypt(byte[] data, int offset, int length)
{
using (MemoryStream buffer = new MemoryStream(255))
using (BinaryWriter writer = new BinaryWriter(buffer))
{
using (SHA1 sha1 = new SHA1Managed())
{
2015-09-28 04:01:17 +02:00
byte[] hashsum = sha1.ComputeHash(data, offset, length);
2016-04-18 12:50:57 +02:00
writer.Write(hashsum);
2015-09-28 04:01:17 +02:00
}
buffer.Write(data, offset, length);
2016-04-18 12:50:57 +02:00
if (length < 235)
{
2015-09-28 04:01:17 +02:00
byte[] padding = new byte[235 - length];
new Random().NextBytes(padding);
buffer.Write(padding, 0, padding.Length);
}
byte[] ciphertext = new BigInteger(1, buffer.ToArray()).ModPow(e, m).ToByteArrayUnsigned();
2016-04-18 12:50:57 +02:00
if (ciphertext.Length == 256)
{
2015-09-28 04:01:17 +02:00
return ciphertext;
2016-04-18 12:50:57 +02:00
}
else {
2015-09-28 04:01:17 +02:00
byte[] paddedCiphertext = new byte[256];
int padding = 256 - ciphertext.Length;
2016-04-18 12:50:57 +02:00
for (int i = 0; i < padding; i++)
{
2015-09-28 04:01:17 +02:00
paddedCiphertext[i] = 0;
}
ciphertext.CopyTo(paddedCiphertext, padding);
2016-04-18 12:50:57 +02:00
return paddedCiphertext;
2015-09-28 04:01:17 +02:00
}
}
}
}
2016-04-18 12:50:57 +02:00
public class RSA
{
2015-09-28 04:01:17 +02:00
private static readonly Dictionary<string, RSAServerKey> serverKeys = new Dictionary<string, RSAServerKey>() {
{ "216be86c022bb4c3", new RSAServerKey("216be86c022bb4c3", new BigInteger("00C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F", 16), new BigInteger("010001", 16)) }
};
2016-04-18 12:50:57 +02:00
public static byte[] Encrypt(string fingerprint, byte[] data, int offset, int length)
{
2015-09-28 04:01:17 +02:00
string fingerprintLower = fingerprint.ToLower();
2016-04-18 12:50:57 +02:00
if (!serverKeys.ContainsKey(fingerprintLower))
{
2015-09-28 04:01:17 +02:00
return null;
}
RSAServerKey key = serverKeys[fingerprintLower];
return key.Encrypt(data, offset, length);
}
}
2016-04-18 12:50:57 +02:00
2015-09-28 04:01:17 +02:00
}