View Single Post
Old 12.01.2006, 10:23   #12
Sindol
Архивный участник
 
Join Date: Oct 2005
Posts: 21
Default

Ya thats correct forumula, the reason that the exponent isnt sent to the client is because 99% of the time, the exponent is either 3 or 65537 (usually this one, represented in byte[] form as 1,0,1), and since the exponent isnt used in decryption, the server could care less what the exponent is, so the client just creates its own, the only really important parts are N (public) and D (private).

The real problem with RSA and C# is that M$ thinks that everything RSA related should be padded, with either PKCS#1 v2 or PKCS#1 v1.5. Lineage 2 doesn't need to pad because the packet is always under 128 in length and there fore, never larger then the key. In Java, you can tell the RSA engine this by using NoPadding options. In C# if you try this (by using a direct crypto stream), C# will complain and either throw an exception or try to use PKCS#1 v1.5 by default, which of course screws up the decryption.

So the only option is to use the formulas yourself to decrypt, which means you'll need access to Big Integers since a 1024 bit RSA key is larger then a double. You can find many free C# versions of a BigInt on the web. Next you'll need a modpow method to perform the math on the BigInt. Last you'll need a method to perform ECB block handling on the encrypted data. The last is actually fairly easy, since the block size is the length of the key, so technically all you need to do is convert the encrypted data from little-endian to big-endian order.

The followin was my attempt at basic RSA encryption and decryption using a Big Integer class found at

http://www.codeproject.com/csharp/biginteger.asp

Code:
using System;

namespace Lineage2.Crypt
{

    public class RSAKeyPair
    {

        private BigInteger publicKey;
        private BigInteger privateKey;
        private BigInteger exponent;

        public BigInteger PublicKey
        {

            get
            {

                return publicKey;

            }
        }

        public BigInteger PrivateKey
        {

            get
            {

                return privateKey;

            }
        }

        public BigInteger Exponent
        {

            get
            {

                return exponent;

            }
        }

        public RSAKeyPair(BigInteger n, BigInteger d, BigInteger e)
        {

            publicKey = n;
            privateKey = d;
            exponent = e;

        }
    }

    public class RSAEngine
    {

        public static RSAKeyPair CreateKey(int lengthMax)
        {

            BigInteger p = FindRan(lengthMax * 4);
            BigInteger q = FindRan(lengthMax * 4);

            BigInteger n = p * q;

            BigInteger one = 1;
            BigInteger m = (p - one) * (q - one);

            BigInteger e = 65537;

            while(m.gcd(e).IntValue() > 1)
            {

                e = e + 2;

            }

            BigInteger d = e.modInverse(m);

            RSAKeyPair keyPair = new RSAKeyPair(n, d, e);

            return keyPair;

        }

        public static byte[] Decrypt(byte[] data, RSAKeyPair keyPair)
        {

            BigInteger encryptedData = new BigInteger(data, data.Length);

            BigInteger decryptedData = encryptedData.modPow(keyPair.PrivateKey, keyPair.PublicKey);

            return decryptedData.getBytes();

        }

        public static byte[] Encrypt(byte[] data, RSAKeyPair keyPair)
        {

            BigInteger decryptedData = new BigInteger(data, data.Length);

            BigInteger encryptedData = decryptedData.modPow(keyPair.Exponent, keyPair.PublicKey);

            return encryptedData.getBytes();

        }

        private static BigInteger FindRan(int lengthKey)
        {

            Random rn = new Random();
            BigInteger rnI = BigInteger.genPseudoPrime(lengthKey, 100, rn);

            return rnI;

        }
    }
}
This will encrypt and decrypt using RSA, but for reasons beyond me, it cannot decrypt packets sent from Lineage 2 client and I could never find out why, but you can take a look at it to see what has to be done to encrypt and decrypt without using M$'s RSACryptoServiceProvider.

Here's an example of how to use it:

Code:
// Byte Converter
System.Text.ASCIIEncoding ByteConverter = new System.Text.ASCIIEncoding();

// Create New Key Pair Using 1024bit strength
RSAKeyPair keyPair = RSAEngine.CreateKey(128);

String dataString = "This is my data to encrypt.";

Byte[] rawData = ByteConverter.GetBytes(dataString);

Byte[] encryptedData = RSAEngine.Encrypt(rawData, keyPair);

Console.WriteLine("Encrypted Data: {0}", ByteConverter.GetString(encryptedData));

Byte[] decryptedData = RSAEngine.Decrypt(encryptedData, keyPair);

Console.WriteLine("Decrypted Data: {0}", ByteConverter.GetString(decryptedData));
Sindol is offline