Пример #1
0
 public static StartTransaction Create(string domain, System.Security.Cryptography.RSAParameters publicKey, SharedNetwork.Protocol protocol)
 {
     return(new StartTransaction()
     {
         ServerHandshake = Handshake.Create(protocol), Domain = domain, RSAKey = publicKey, Accepted = true, Encrypted = true
     });
 }
Пример #2
0
        public void init(int key_size)
        {
            //    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            //    keyGen.initialize(key_size, new SecureRandom());
            //    KeyPair pair = keyGen.generateKeyPair();
            //
            //    PublicKey pubKey=pair.getPublic();
            //    PrivateKey prvKey=pair.getPrivate();

            System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(key_size);
            RSAKeyInfo = rsa.ExportParameters(true);

            //    d=((RSAPrivateKey)prvKey).getPrivateExponent().toByteArray();
            //    e=((RSAPublicKey)pubKey).getPublicExponent().toByteArray();
            //    n=((RSAKey)prvKey).getModulus().toByteArray();
            //
            //    c=((RSAPrivateCrtKey)prvKey).getCrtCoefficient().toByteArray();
            //    ep=((RSAPrivateCrtKey)prvKey).getPrimeExponentP().toByteArray();
            //    eq=((RSAPrivateCrtKey)prvKey).getPrimeExponentQ().toByteArray();
            //    p=((RSAPrivateCrtKey)prvKey).getPrimeP().toByteArray();
            //    q=((RSAPrivateCrtKey)prvKey).getPrimeQ().toByteArray();

            d = RSAKeyInfo.D;
            e = RSAKeyInfo.Exponent;
            n = RSAKeyInfo.Modulus;

            c  = RSAKeyInfo.InverseQ;
            ep = RSAKeyInfo.DP;
            eq = RSAKeyInfo.DQ;
            p  = RSAKeyInfo.P;
            q  = RSAKeyInfo.Q;
        }
        public static bool _ImportParameters_System_Security_Cryptography_RSACryptoServiceProvider_System_Security_Cryptography_RSAParameters( )
        {
            //Parameters
            System.Security.Cryptography.RSAParameters parameters = null;


            //Exception
            Exception exception_Real        = null;
            Exception exception_Intercepted = null;

            InterceptionMaintenance.disableInterception( );

            try
            {
                returnValue_Real = System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(parameters);
            }

            catch (Exception e)
            {
                exception_Real = e;
            }


            InterceptionMaintenance.enableInterception( );

            try
            {
                returnValue_Intercepted = System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(parameters);
            }

            catch (Exception e)
            {
                exception_Intercepted = e;
            }
        }
Пример #4
0
        public void RsaDecryptBytesWithKey()
        {
            // Arrange
            CryptographyFactory f = new CryptographyFactory();

            // Act
            byte[] inputDataBytes = Convert.FromBase64String("uZj8Rm8xW0ISZICUKeQnvNGTMaG81zyhMG8f8DmThs4uTegvWYI0q6JPg1jj0Y+IsQSZHkv/XGLNUJSKrrwnkQYuS90a3OkCoqFiLw8HQnni07bp9TkzDIRQWN+RkZC+7yhIHpQvdQZYl6CSjs3/LaD+LIXHObyXvjMwXLl803I=");
            System.Security.Cryptography.RSAParameters rsaParameters = new System.Security.Cryptography.RSAParameters
            {
                D  = Convert.FromBase64String("fJY0+ECA7KN+RYl0qNsN/mwW2mVndFUTgXZ+CIfGleayMq2R5Y5TzngExcwHaYv9m99s6n9M9c+aDP33RB542z4q2OFgrucbeTKAwRhJlbOYNuLGDuCPmcejPy51JNginjkOtw31+Pn7QinMpgwNG+5n7aQrQwTi2jLI5KUoEpU="),
                DP = Convert.FromBase64String("EJGDjg0DYMTwU8EGNztrqOriZWFvHKidBZj6C0PUo6nx54DzBG9TdNaGj+o0zkUA6cXzf4x1jHYqV+yzG5Njmw=="),
                DQ = Convert.FromBase64String("GiRgLL/RiYS793OD5u/rcw6mYUXIw5bW42krYR2gV7JVrVOi2r7nHNV65StGwmfhwPNnDqwHDefVVFXSxu+O6Q=="),

                Exponent = Convert.FromBase64String("AQAB"),
                InverseQ = Convert.FromBase64String("N/6gNxkobfLf5MaqWwX9Y7hu4UT+urVWC6CASgYwmqsTxBZGGsizVZ3ocq1A814Lo9S+REgKYFYdqh6agXEYOQ=="),
                Modulus  = Convert.FromBase64String("wmkOrKR16oKeDz+9mUqCUOw9jdwDDusrzHH7CEtuWt2XmC6Yu5fG+fh5QquLLF1aC9fyKbr126NsJ2zPbSkejI7grUghDC71D25PCz8sY3bpbMhzXDr8XxM4WSvg3edsSsOhvYzkRa+yZkhWxiaQ+Z0Vb0tTYNyRPi34YLmQZCU="),

                P = Convert.FromBase64String("zvIZ1lx6Qa++2cQVrNguqNbWMziSRrtWUcWISBrtYuzm8aYOm10JbKdq0oRBZspwDyrAyvajGbLBmi2FvwlHZw=="),
                Q = Convert.FromBase64String("8H5JWPl4W6THX5Fw7HHta7x+fOpeV6q1MU4Bzpb6ld9rphD3v6r5F06dojdWTdujv8o94ezx1tm84TWVOpX8kw==")
            };
            byte[] plainTextBytes = f.Decrypt(inputDataBytes, rsaParameters);

            string resultBase64 = System.Text.Encoding.UTF8.GetString(plainTextBytes);

            Console.WriteLine("Result = [{0}]", resultBase64);

            // Assert
            Assert.AreEqual("Hello world", resultBase64);
        }
Пример #5
0
        public Operation.GenerateToken Encrypt(Operation.GenerateToken tokenRequest, byte[] exponent, byte[] modulus)
        {
            Guard.AgainstNullArgument("tokenRequest", tokenRequest);
            Guard.AgainstNullArgument("exponent", exponent);
            Guard.AgainstNullArgument("modulus", modulus);

            using (var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(512))
            {
                var rsaParms = new System.Security.Cryptography.RSAParameters
                {
                    Exponent = exponent,
                    Modulus  = modulus
                };
                rsa.ImportParameters(rsaParms);

                var encryptedUsername   = rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Username), false).BytesToHex();
                var encryptedPassword   = rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Password), false).BytesToHex();
                var encryptedClient     = string.IsNullOrWhiteSpace(tokenRequest.Client) ? "" : rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Client), false).BytesToHex();
                var encryptedExpiration = rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.ExpirationInMinutes.ToString()), false).BytesToHex();
                var encryptedReferer    = string.IsNullOrWhiteSpace(tokenRequest.Referer) ? "" : rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Referer), false).BytesToHex();

                tokenRequest.Encrypt(encryptedUsername, encryptedPassword, encryptedExpiration, encryptedClient, encryptedReferer);

                return(tokenRequest);
            }
        }
Пример #6
0
		public void init(int key_size)
		{
			//    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
			//    keyGen.initialize(key_size, new SecureRandom());
			//    KeyPair pair = keyGen.generateKeyPair();
			//
			//    PublicKey pubKey=pair.getPublic();
			//    PrivateKey prvKey=pair.getPrivate();

			System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(key_size);
			RSAKeyInfo = rsa.ExportParameters(true);

			//    d=((RSAPrivateKey)prvKey).getPrivateExponent().toByteArray();
			//    e=((RSAPublicKey)pubKey).getPublicExponent().toByteArray();
			//    n=((RSAKey)prvKey).getModulus().toByteArray();
			//
			//    c=((RSAPrivateCrtKey)prvKey).getCrtCoefficient().toByteArray();
			//    ep=((RSAPrivateCrtKey)prvKey).getPrimeExponentP().toByteArray();
			//    eq=((RSAPrivateCrtKey)prvKey).getPrimeExponentQ().toByteArray();
			//    p=((RSAPrivateCrtKey)prvKey).getPrimeP().toByteArray();
			//    q=((RSAPrivateCrtKey)prvKey).getPrimeQ().toByteArray();

			d= RSAKeyInfo.D ;
			e=RSAKeyInfo.Exponent ;
			n=RSAKeyInfo.Modulus ;

			c=RSAKeyInfo.InverseQ ;
			ep=RSAKeyInfo.DP ;
			eq=RSAKeyInfo.DQ ;
			p=RSAKeyInfo.P ;
			q=RSAKeyInfo.Q ;
		}
Пример #7
0
        public Operation.GenerateToken Encrypt(Operation.GenerateToken tokenRequest, byte[] exponent, byte[] modulus)
        {
            if (exponent == null || modulus == null)
            {
                throw new InvalidOperationException("Exponent and modulus must be set");
            }

            using (var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(512))
            {
                var rsaParms = new System.Security.Cryptography.RSAParameters
                {
                    Exponent = exponent,
                    Modulus  = modulus
                };
                rsa.ImportParameters(rsaParms);

                var encryptedUsername   = rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Username), false).BytesToHex();
                var encryptedPassword   = rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Password), false).BytesToHex();
                var encryptedClient     = String.IsNullOrWhiteSpace(tokenRequest.Client) ? "" : rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Client), false).BytesToHex();
                var encryptedExpiration = rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.ExpirationInMinutes.ToString()), false).BytesToHex();
                var encryptedReferer    = String.IsNullOrWhiteSpace(tokenRequest.Referer) ? "" : rsa.Encrypt(Encoding.UTF8.GetBytes(tokenRequest.Referer), false).BytesToHex();

                tokenRequest.Encrypt(encryptedUsername, encryptedPassword, encryptedExpiration, encryptedClient, encryptedReferer);

                return(tokenRequest);
            }
        }
Пример #8
0
		private byte[] Encrypt(byte[] buffer)
		{
			var RSAKeyInfo = new System.Security.Cryptography.RSAParameters();
			RSAKeyInfo.Modulus = EncKey;
			RSAKeyInfo.Exponent = Exponent;
			var rsa = new Mono.Security.Cryptography.RSAManaged();
			rsa.ImportParameters(RSAKeyInfo);
			return rsa.EncryptValue(buffer);
		}
Пример #9
0
 public void setPrvKey(System.Security.Cryptography.RSAParameters keyInfo)
 {
     //    RSAPrivateKeySpec rsaPrivKeySpec =
     //	new RSAPrivateKeySpec(new BigInteger(n),
     //			      new BigInteger(d));
     //    PrivateKey prvKey = keyFactory.generatePrivate(rsaPrivKeySpec);
     //    signature.initSign(prvKey);
     RSAKeyInfo = keyInfo;
 }
Пример #10
0
        public System.Security.Cryptography.RSA ToDotNetRSA()
        {
            RsaPrivateCrtKeyParameters keyParams = (RsaPrivateCrtKeyParameters)_KeyPair.Private;

            System.Security.Cryptography.RSAParameters rsaParameters = DotNetUtilities.ToRSAParameters(keyParams);
            var rsa = System.Security.Cryptography.RSA.Create();

            rsa.ImportParameters(rsaParameters);
            return(rsa);
        }
Пример #11
0
        private byte[] Encrypt(byte[] buffer)
        {
            var RSAKeyInfo = new System.Security.Cryptography.RSAParameters();

            RSAKeyInfo.Modulus  = EncKey;
            RSAKeyInfo.Exponent = Exponent;
            var rsa = new Mono.Security.Cryptography.RSAManaged();

            rsa.ImportParameters(RSAKeyInfo);
            return(rsa.EncryptValue(buffer));
        }
Пример #12
0
        public void ImportParameters(System.Security.Cryptography.RSAParameters parameters)
        {
            ThrowIfObjectDisposed();

            if (this.AlgorithmName != AsymmetricAlgName.RSA)
            {
                throw new InvalidOperationException("Only supported by RSA.");
            }

            (this.provider as System.Security.Cryptography.RSA).ImportParameters(parameters);
        }
Пример #13
0
            public SecurityKey GetKey()
            {
                var keyParams = new System.Security.Cryptography.RSAParameters()
                {
                    Exponent = KeyParamDecode(this.e),
                    Modulus  = KeyParamDecode(this.n),
                };
                var key = new RsaSecurityKey(keyParams);

                key.KeyId = this.kid;
                return(key);
            }
Пример #14
0
        // https://stackoverflow.com/questions/6029937/net-private-key-rsa-encryption
        public static void DotNetRsaParamtersFromPemFile()
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader("../../privatekey.pem"))
            {
                Org.BouncyCastle.OpenSsl.PemReader pr = new Org.BouncyCastle.OpenSsl.PemReader(sr);
                Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair KeyPair = (Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair)pr.ReadObject();

                System.Security.Cryptography.RSAParameters rsa = Org.BouncyCastle.Security.DotNetUtilities.ToRSAParameters(
                    (Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters)KeyPair.Private
                    );
            }
        }
Пример #15
0
 void GenerateRSAKEY(int dwKyeSize)
 {
     try {
         System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(dwKyeSize);
         xml_format = rsa.ToXmlString(true);
         keyinfo    = rsa.ExportParameters(true);
     }
     catch (System.Security.Cryptography.CryptographicException ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Пример #16
0
 /// <summary>
 /// Converts the .NET Framework <see cref="RSAParameters"/> struct to the type
 /// offered by the PCLCrypto library.
 /// </summary>
 /// <param name="value">The .NET Framework parameters.</param>
 /// <returns>The PCLCrypto parameters.</returns>
 public static RSAParameters ToPCLParameters(System.Security.Cryptography.RSAParameters value)
 {
     return(new RSAParameters
     {
         D = value.D,
         Q = value.Q,
         P = value.P,
         DP = value.DP,
         DQ = value.DQ,
         Exponent = value.Exponent,
         InverseQ = value.InverseQ,
         Modulus = value.Modulus,
     });
 }
Пример #17
0
 //License Utils
 public static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, System.Security.Cryptography.RSAParameters Key)
 {
     try
     {
         var RSAalg = new System.Security.Cryptography.RSACryptoServiceProvider();
         RSAalg.ImportParameters(Key);
         return(RSAalg.VerifySha1Data(DataToVerify, SignedData));
     }
     catch (System.Security.Cryptography.CryptographicException ex)
     {
         Tracer.Instance.WriteError(ex);
         return(false);
     }
 }
Пример #18
0
        public static string ToXmlString(this System.Security.Cryptography.RSACryptoServiceProvider rsa)
        {
            System.Security.Cryptography.RSAParameters parameters = rsa.ExportParameters(true);

            return(string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
                                 System.Convert.ToBase64String(parameters.Modulus),
                                 System.Convert.ToBase64String(parameters.Exponent),
                                 System.Convert.ToBase64String(parameters.P),
                                 System.Convert.ToBase64String(parameters.Q),
                                 System.Convert.ToBase64String(parameters.DP),
                                 System.Convert.ToBase64String(parameters.DQ),
                                 System.Convert.ToBase64String(parameters.InverseQ),
                                 System.Convert.ToBase64String(parameters.D)));
        }
Пример #19
0
        public byte[] Decrypt(byte[] inputDataBytes, System.Security.Cryptography.RSAParameters rsaParameters)
        {
            byte[] decryptedData;
            //Create a new instance of RSACryptoServiceProvider.
            using (System.Security.Cryptography.RSACryptoServiceProvider csp = new System.Security.Cryptography.RSACryptoServiceProvider())
            {
                // Import public key information.
                csp.ImportParameters(rsaParameters);

                // Decrypt the passed byte array and specify OAEP padding.
                // OAEP padding is only available on Microsoft Windows XP or later.
                decryptedData = csp.Decrypt(inputDataBytes, false);
            }
            return(decryptedData);
        }
Пример #20
0
        public void init(int key_size)
        {
            System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(key_size);
            RSAKeyInfo = rsa.ExportParameters(true);

            d = RSAKeyInfo.D;
            e = RSAKeyInfo.Exponent;
            n = RSAKeyInfo.Modulus;

            c  = RSAKeyInfo.InverseQ;
            ep = RSAKeyInfo.DP;
            eq = RSAKeyInfo.DQ;
            p  = RSAKeyInfo.P;
            q  = RSAKeyInfo.Q;
        }
        //Support to convert RSA key from PEM to XML, currently RSACryptoServiceProvider only support XML format.
        private static System.Security.Cryptography.RSAParameters ConvertPemToXmlFormat(string privateKey)
        {
            var privateKeyBits = Convert.FromBase64String(privateKey);

            var rsaParams = new System.Security.Cryptography.RSAParameters();

            using (var binr = new BinaryReader(new MemoryStream(privateKeyBits)))
            {
                byte   bt       = 0;
                ushort twobytes = 0;
                twobytes = binr.ReadUInt16();
                if (twobytes == 0x8130)
                {
                    binr.ReadByte();
                }
                else if (twobytes == 0x8230)
                {
                    binr.ReadInt16();
                }
                else
                {
                    throw new Exception("Unexpected value read binr.ReadUInt16()");
                }

                twobytes = binr.ReadUInt16();
                if (twobytes != 0x0102)
                {
                    throw new Exception("Unexpected version");
                }

                bt = binr.ReadByte();
                if (bt != 0x00)
                {
                    throw new Exception("Unexpected value read binr.ReadByte()");
                }

                rsaParams.Modulus  = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.D        = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.P        = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.Q        = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.DP       = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.DQ       = binr.ReadBytes(GetIntegerSize(binr));
                rsaParams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
            }

            return(rsaParams);
        }
        /// <summary>
        /// Encrypt a given plain text using the <see cref="PublicKey" data from the server./>
        /// </summary>
        /// <param name="key">The ArcGIS Server public key information to use.</param>
        /// <param name="plainText">The plain text to encrypt.</param>
        /// <returns>The hex encoded encrypted data.</returns>
        private static string EncryptData(PublicKey key, string plainText)
        {
            byte[] exponent = HexToBytes(key.Exponent);
            byte[] modulus  = HexToBytes(key.Modulus);

            using (var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(512))
            {
                var rsaParms = new System.Security.Cryptography.RSAParameters()
                {
                    Exponent = exponent, Modulus = modulus
                };
                rsa.ImportParameters(rsaParms);

                return(BytesToHex(rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), false)));
            }
        }
Пример #23
0
        //public static void ToJson(this System.Security.Cryptography.RSAParameters rsaParameters)
        //{
        //}

        //public static System.Collections.Specialized.StringDictionary ToDictionary(this System.Security.Cryptography.RSAParameters rsaParameters)
        //{
        //    System.Collections.Specialized.StringDictionary d = new System.Collections.Specialized.StringDictionary();

        //    d.Add("D", rsaParameters.D.ToBase64String());
        //    d.Add("DP", rsaParameters.DP.ToBase64String());
        //    d.Add("DQ", rsaParameters.DQ.ToBase64String());

        //    d.Add("Exponent", rsaParameters.Exponent.ToBase64String());
        //    d.Add("InverseQ", rsaParameters.InverseQ.ToBase64String());
        //    d.Add("Modulus", rsaParameters.Modulus.ToBase64String());

        //    d.Add("P", rsaParameters.P.ToBase64String());
        //    d.Add("Q", rsaParameters.Q.ToBase64String());

        //    return d;
        //}

        public static Dictionary <string, string> ToDictionary(this System.Security.Cryptography.RSAParameters rsaParameters)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            d.Add("D", rsaParameters.D.ToBase64String());
            d.Add("DP", rsaParameters.DP.ToBase64String());
            d.Add("DQ", rsaParameters.DQ.ToBase64String());

            d.Add("Exponent", rsaParameters.Exponent.ToBase64String());
            d.Add("InverseQ", rsaParameters.InverseQ.ToBase64String());
            d.Add("Modulus", rsaParameters.Modulus.ToBase64String());

            d.Add("P", rsaParameters.P.ToBase64String());
            d.Add("Q", rsaParameters.Q.ToBase64String());

            return(d);
        }
Пример #24
0
        public static AsymmetricKeyParameter TransformRSAPrivateKey(System.Security.Cryptography.AsymmetricAlgorithm privateKey)
        {
            System.Security.Cryptography.RSACryptoServiceProvider prov       = privateKey as System.Security.Cryptography.RSACryptoServiceProvider;
            System.Security.Cryptography.RSAParameters            parameters = prov.ExportParameters(true);

            // Obviously this assumes that the certificate includes a RSA Key but the same result can be achieved for DSA with DSACryptoServiceProvider and DSAParameters

            return(new RsaPrivateCrtKeyParameters(
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.Modulus),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.Exponent),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.D),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.P),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.Q),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.DP),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.DQ),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.InverseQ)
                       ));
        }
Пример #25
0
 public static void ToConsole(this System.Security.Cryptography.RSAParameters rsaParameters)
 {
     if (rsaParameters.D.Length > 0)
     {
         Console.WriteLine("D        = [{0}]", Convert.ToBase64String(rsaParameters.D));         // d, the private exponent      // privateExponent
     }
     if (rsaParameters.DP.Length > 0)
     {
         Console.WriteLine("DP       = [{0}]", Convert.ToBase64String(rsaParameters.DP));        // d mod (p - 1)                // exponent1
     }
     if (rsaParameters.DQ.Length > 0)
     {
         Console.WriteLine("DQ       = [{0}]", Convert.ToBase64String(rsaParameters.DQ));        // d mod (q - 1)                // exponent2
     }
     if (rsaParameters.Exponent.Length > 0)
     {
         Console.WriteLine("Exponent = [{0}]", Convert.ToBase64String(rsaParameters.Exponent));  // e, the public exponent       // publicExponent
     }
     if (rsaParameters.InverseQ.Length > 0)
     {
         Console.WriteLine("InverseQ = [{0}]", Convert.ToBase64String(rsaParameters.InverseQ));  // (InverseQ)(q) = 1 mod p      // coefficient
     }
     if (rsaParameters.Modulus.Length > 0)
     {
         Console.WriteLine("Modulus  = [{0}]", Convert.ToBase64String(rsaParameters.Modulus));   // n                            // modulus
     }
     if (rsaParameters.P.Length > 0)
     {
         Console.WriteLine("P        = [{0}]", Convert.ToBase64String(rsaParameters.P));         // p                            // prime1
     }
     if (rsaParameters.Q.Length > 0)
     {
         Console.WriteLine("Q        = [{0}]", Convert.ToBase64String(rsaParameters.Q));         // q                            // prime2
     }
     //Console.WriteLine("D        = [{0}]", Convert.ToBase64String(rsaParameters.D));         // d, the private exponent      // privateExponent
     //Console.WriteLine("DP       = [{0}]", Convert.ToBase64String(rsaParameters.DP));        // d mod (p - 1)                // exponent1
     //Console.WriteLine("DQ       = [{0}]", Convert.ToBase64String(rsaParameters.DQ));        // d mod (q - 1)                // exponent2
     //Console.WriteLine("Exponent = [{0}]", Convert.ToBase64String(rsaParameters.Exponent));  // e, the public exponent       // publicExponent
     //Console.WriteLine("InverseQ = [{0}]", Convert.ToBase64String(rsaParameters.InverseQ));  // (InverseQ)(q) = 1 mod p      // coefficient
     //Console.WriteLine("Modulus  = [{0}]", Convert.ToBase64String(rsaParameters.Modulus));   // n                            // modulus
     //Console.WriteLine("P        = [{0}]", Convert.ToBase64String(rsaParameters.P));         // p                            // prime1
     //Console.WriteLine("Q        = [{0}]", Convert.ToBase64String(rsaParameters.Q));         // q                            // prime2
 }
Пример #26
0
        public byte[] Encrypt(byte[] inputDataBytes, out System.Security.Cryptography.RSAParameters rsaParameters)
        {
            byte[] encryptedData;

            using (System.Security.Cryptography.RSACryptoServiceProvider csp = new System.Security.Cryptography.RSACryptoServiceProvider())
            {
                // Return the full set of public and private keys
                rsaParameters = csp.ExportParameters(true);

                //Import only the public key information
                //csp.ImportParameters(csp.ExportParameters(false));

                // Encrypt the passed byte array and specify OAEP padding.
                // OAEP padding is only available on Microsoft Windows XP or later.
                encryptedData = csp.Encrypt(inputDataBytes, false);
            }

            return(encryptedData);
        }
Пример #27
0
        public static void FromXmlString(
            this System.Security.Cryptography.RSACryptoServiceProvider rsa
            , string xmlString
            )
        {
            System.Security.Cryptography.RSAParameters parameters =
                new System.Security.Cryptography.RSAParameters();

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.LoadXml(xmlString);

            if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
            {
                foreach (System.Xml.XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    switch (node.Name)
                    {
                    case "Modulus": parameters.Modulus = System.Convert.FromBase64String(node.InnerText); break;

                    case "Exponent": parameters.Exponent = System.Convert.FromBase64String(node.InnerText); break;

                    case "P": parameters.P = System.Convert.FromBase64String(node.InnerText); break;

                    case "Q": parameters.Q = System.Convert.FromBase64String(node.InnerText); break;

                    case "DP": parameters.DP = System.Convert.FromBase64String(node.InnerText); break;

                    case "DQ": parameters.DQ = System.Convert.FromBase64String(node.InnerText); break;

                    case "InverseQ": parameters.InverseQ = System.Convert.FromBase64String(node.InnerText); break;

                    case "D": parameters.D = System.Convert.FromBase64String(node.InnerText); break;
                    }
                }
            }
            else
            {
                throw new System.Exception("Invalid XML RSA key.");
            }

            rsa.ImportParameters(parameters);
        }
        public static bool _ExportParameters_System_Security_Cryptography_RSACryptoServiceProvider_System_Boolean( )
        {
            //Parameters
            System.Boolean includePrivateParameters = null;

            //ReturnType/Value
            System.Security.Cryptography.RSAParameters returnVal_Real        = null;
            System.Security.Cryptography.RSAParameters returnVal_Intercepted = null;

            //Exception
            Exception exception_Real        = null;
            Exception exception_Intercepted = null;

            InterceptionMaintenance.disableInterception( );

            try
            {
                returnValue_Real = System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(includePrivateParameters);
            }

            catch (Exception e)
            {
                exception_Real = e;
            }


            InterceptionMaintenance.enableInterception( );

            try
            {
                returnValue_Intercepted = System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(includePrivateParameters);
            }

            catch (Exception e)
            {
                exception_Intercepted = e;
            }


            Return((exception_Real.Messsage == exception_Intercepted.Message) && (returnValue_Real == returnValue_Intercepted));
        }
Пример #29
0
        public void RsaEncryptBytesWithKey()
        {
            // Arrange
            CryptographyFactory f = new CryptographyFactory();

            // Act
            byte[] inputDataBytes = System.Text.Encoding.UTF8.GetBytes("Hello world");
            System.Security.Cryptography.RSAParameters rsaParameters = new System.Security.Cryptography.RSAParameters
            {
                Exponent = Convert.FromBase64String("AQAB"),
                Modulus  = Convert.FromBase64String("31BWO1MiwU4cOteyYVbMnfjLa3dqRkpY26LSfqp6gkUBSyW+CBoSKUK/1XmlkiHLhYyjo38dd/zWH4sP6+fiaXfQFsV7dhzyvS023I6hixg6PFl5wy1qkRXp6IVjRfbrd29ebCuHe6AFz3aOiahtmKPKUBwoPvk5Ak7hgxPIEs0=")
            };
            byte[] hashBytes = f.Encrypt(inputDataBytes, rsaParameters);

            string resultBase64 = Convert.ToBase64String(hashBytes);

            Console.WriteLine("Result = [{0}]", resultBase64);

            // Assert
            Assert.NotNull(resultBase64);
        }
Пример #30
0
        public RSAParameters ExportParameters(bool includePrivateParameters)
        {
#if WINDOWS_STORE
            IBuffer export = keyPair.Export(CryptographicPrivateKeyBlobType.BCryptPrivateKey);

            byte[] result;
            CryptographicBuffer.CopyToByteArray(export, out result);

            BCryptRsaKeyBlob header = BCryptRsaKeyBlob.Load(result);
            int offset = Marshal.SizeOf <BCryptRsaKeyBlob>();

            byte[] exponent = result.Skip(offset).Take((int)header.cbPublicExp).ToArray();

            offset += (int)header.cbPublicExp;
            byte[] modulus = result.Skip(offset).Take((int)header.cbModulus).ToArray();

            return(new RSAParameters(exponent, modulus));
#else
            System.Security.Cryptography.RSAParameters parameters =
                provider.ExportParameters(includePrivateParameters);

            return(new RSAParameters(parameters.Exponent, parameters.Modulus));
#endif
        }
Пример #31
0
        private static System.Security.Cryptography.RSAParameters ExtractFromXml(string xml)
        {
            var csp = new System.Security.Cryptography.RSAParameters();

            using (var reader = System.Xml.XmlReader.Create(new StringReader(xml)))
            {
                while (reader.Read())
                {
                    if (reader.NodeType != System.Xml.XmlNodeType.Element)
                    {
                        continue;
                    }

                    var elName = reader.Name;
                    if (elName == "RSAKeyValue")
                    {
                        continue;
                    }

                    do
                    {
                        reader.Read();
                    } while (reader.NodeType != System.Xml.XmlNodeType.Text && reader.NodeType != System.Xml.XmlNodeType.EndElement);

                    if (reader.NodeType == System.Xml.XmlNodeType.EndElement)
                    {
                        continue;
                    }

                    var value = reader.Value;
                    switch (elName)
                    {
                    case "Modulus":
                        csp.Modulus = Convert.FromBase64String(value);
                        break;

                    case "Exponent":
                        csp.Exponent = Convert.FromBase64String(value);
                        break;

                    case "P":
                        csp.P = Convert.FromBase64String(value);
                        break;

                    case "Q":
                        csp.Q = Convert.FromBase64String(value);
                        break;

                    case "DP":
                        csp.DP = Convert.FromBase64String(value);
                        break;

                    case "DQ":
                        csp.DQ = Convert.FromBase64String(value);
                        break;

                    case "InverseQ":
                        csp.InverseQ = Convert.FromBase64String(value);
                        break;

                    case "D":
                        csp.D = Convert.FromBase64String(value);
                        break;
                    }
                }

                return(csp);
            }
        }
Пример #32
0
 public RsaKey()
 {
     iKey = new System.Security.Cryptography.RSACryptoServiceProvider(kKeySizeBits);
     iKeyInfo = iKey.ExportParameters(true);
 }
 public abstract void ImportParameters(System.Security.Cryptography.RSAParameters parameters);
Пример #34
0
        // Send our login info!
        private void craft_login_packet(Socket socket)
        {
            byte[] login_info = new byte[128];

            login_info[0x5B] = 0x24;

            for (int i = 0; i < name.Length; i++)
            {
                login_info[0x5E + i] = (byte)name[i];
            }

            for (int i = 0; i < password.Length; i++)
            {
                login_info[0x6C + i] = (byte)password[i];
            }

            byte[] exponent = { 1, 0, 1 };

            System.Security.Cryptography.RSAParameters RSAKeyInfo = new System.Security.Cryptography.RSAParameters();

            //Set RSAKeyInfo to the public key values.
            RSAKeyInfo.Modulus = RSA_enc_key_;
            RSAKeyInfo.Exponent = exponent;

            RSA_Managed rsa_managed = new RSA_Managed();
            rsa_managed.ImportParameters(RSAKeyInfo);

            byte[] encrypted_bytes = new byte[128];

            encrypted_bytes = rsa_managed.EncryptValue(login_info);

            byte[] login_send = new byte[176];
            byte[] prelogin_send = new byte[176];

            encrypted_bytes.CopyTo(login_send, 1);

            login_send[129] = RSA_key_[0];
            login_send[130] = RSA_key_[1];
            login_send[131] = RSA_key_[2];
            login_send[132] = RSA_key_[3];
            login_send[133] = 0x23;//GG reply start
            login_send[134] = 0x01;
            login_send[135] = 0x00;
            login_send[136] = 0x00;
            login_send[137] = 0x67;//
            login_send[138] = 0x45;
            login_send[139] = 0x00;
            login_send[140] = 0x00;
            login_send[141] = 0xAB;//
            login_send[142] = 0x89;
            login_send[143] = 0x00;
            login_send[144] = 0x00;
            login_send[145] = 0xEF;//
            login_send[146] = 0xCD;
            login_send[147] = 0x00;
            login_send[148] = 0x00;//GG reply stop
            login_send[149] = 0x08;//
            login_send[150] = 0x00;
            login_send[151] = 0x00;
            login_send[152] = 0x00;
            login_send[153] = 0x00;//
            login_send[154] = 0x00;
            login_send[155] = 0x00;
            login_send[156] = 0x00;
            login_send[157] = 0x00;//
            login_send[158] = 0x00;
            login_send[159] = 0x00;

            ulong checksum = Global.CheckSum(login_send, 160);

            login_send[160] = (byte)(checksum & 0xff);
            login_send[161] = (byte)(checksum >> 0x08 & 0xff);
            login_send[163] = (byte)(checksum >> 0x10 & 0xff);
            login_send[163] = (byte)(checksum >> 0x18 & 0xff);

            blowfish_cipher_.init(true, blowfish_key_);
            blowfish_cipher_.processBigBlock(login_send, 0, prelogin_send, 0, 176);

            byte[] login_packet = new byte[178];

            login_packet[0] = 0xB2;
            login_packet[1] = 0x00;

            prelogin_send.CopyTo(login_packet, 2);

            connection_.send_packet(login_packet, 178, socket);
        }
Пример #35
0
        public override void DataReceived(ref byte[] inputPacket)
        {
            if (gameData.Blowfish_Key.Length > 0)
            {
                LoginReader packet = new LoginReader(inputPacket, gameData);
                packet.Decrypt();
                byte opcode = packet.readB();
                switch (opcode)
                {
                    case 0x01:
                        #region Failed Login.
                        byte reason = packet.readB();
                        string message = "";
                        switch (reason)
                        {
                            case 0x01:
                                message = "System Error.";
                                break;
                            case 0x02:
                                message = "Password is incorrect.";
                                break;
                            case 0x03:
                                message = "Username or password incorrect.";
                                break;
                            case 0x04:
                                message = "Access Failed.";
                                break;
                            case 0x07:
                                message = "Account in use.";
                                break;
                            case 0x0f:
                                message = "Server overloaded.";
                                break;
                            case 0x10:
                                message = "Server undergoing maintenance.";
                                break;
                            case 0x11:
                                message = "Temporary password has expired.";
                                break;
                            case 0x23:
                                message = "Dual box detected.";
                                break;
                        }
                        Debug.Error("Unable to login, message from server: " + message);
                        Close();
                        break;
                        #endregion
                    case 0x0B:
                        #region GameGuard Response.
                        string name = UserConfig.Username;
                        string pw = UserConfig.Password;
                        byte[] login_info = new byte[128];
                        login_info[0x5B] = 0x24;
                        for (int i = 0; i < name.Length; i++)
                        {
                            login_info[0x5E + i] = (byte)name[i];
                        }
                        for (int i = 0; i < pw.Length; i++)
                        {
                            login_info[0x6C + i] = (byte)pw[i];
                        }
                        byte[] Exponent = { 1, 0, 1 };
                        System.Security.Cryptography.RSAParameters RSAKeyInfo = new System.Security.Cryptography.RSAParameters();

                        //Set RSAKeyInfo to the public key values. 
                        RSAKeyInfo.Modulus = gameData.RSA_Key;
                        RSAKeyInfo.Exponent = Exponent;

                        RSA poo = new RSA();
                        poo.ImportParameters(RSAKeyInfo);

                        byte[] outb = new byte[128];

                        outb = poo.EncryptValue(login_info);
                        byte[] login_send = new byte[176];
                        byte[] login_sende = new byte[176];
                        outb.CopyTo(login_send, 1);
                        login_send[129] = gameData.Session[0];
                        login_send[130] = gameData.Session[1];
                        login_send[131] = gameData.Session[2];
                        login_send[132] = gameData.Session[3];
                        login_send[133] = 0x23;//gameguard reply start
                        login_send[134] = 0x01;
                        login_send[135] = 0x00;
                        login_send[136] = 0x00;
                        login_send[137] = 0x67;//
                        login_send[138] = 0x45;
                        login_send[139] = 0x00;
                        login_send[140] = 0x00;
                        login_send[141] = 0xAB;//
                        login_send[142] = 0x89;
                        login_send[143] = 0x00;
                        login_send[144] = 0x00;
                        login_send[145] = 0xEF;//
                        login_send[146] = 0xCD;
                        login_send[147] = 0x00;
                        login_send[148] = 0x00;//game guard reply stop
                        login_send[149] = 0x08;//
                        login_send[150] = 0x00;
                        login_send[151] = 0x00;
                        login_send[152] = 0x00;
                        login_send[153] = 0x00;//
                        login_send[154] = 0x00;
                        login_send[155] = 0x00;
                        login_send[156] = 0x00;
                        login_send[157] = 0x00;//
                        login_send[158] = 0x00;
                        login_send[159] = 0x00;

                        ulong chk = General.CheckSum(login_send, 160);

                        login_send[160] = (byte)(chk & 0xff);
                        login_send[161] = (byte)(chk >> 0x08 & 0xff);
                        login_send[163] = (byte)(chk >> 0x10 & 0xff);
                        login_send[163] = (byte)(chk >> 0x18 & 0xff);

                        LoginWriter pck = new LoginWriter(login_send, gameData);
                        pck.Encrypt();
                        Send(pck.Finalize());
                        Debug.Information("Sending login information.");
                        break;
                        #endregion
                    case 0x03:
                        #region LoginOK
                        byte[] send = new byte[32];

                        send[0] = 0x05;
                        send[1] = packet.readB();
                        send[2] = packet.readB();
                        send[3] = packet.readB();
                        send[4] = packet.readB();
                        send[5] = packet.readB();
                        send[6] = packet.readB();
                        send[7] = packet.readB();
                        send[8] = packet.readB();
                        send[9] = 0x04;
                        send[10] = 0x00;
                        send[11] = 0x00;
                        send[12] = 0x00;
                        send[13] = 0x00;
                        send[14] = 0x00;
                        send[15] = 0x00;

                        gameData.LoginOK = new byte[8];
                        gameData.LoginOK[0] = send[1];
                        gameData.LoginOK[1] = send[2];
                        gameData.LoginOK[2] = send[3];
                        gameData.LoginOK[3] = send[4];
                        gameData.LoginOK[4] = send[5];
                        gameData.LoginOK[5] = send[6];
                        gameData.LoginOK[6] = send[7];
                        gameData.LoginOK[7] = send[8];

                        chk = General.CheckSum(send, 16);

                        send[16] = (byte)(chk & 0xff);
                        send[17] = (byte)(chk >> 0x08 & 0xff);
                        send[18] = (byte)(chk >> 0x10 & 0xff);
                        send[19] = (byte)(chk >> 0x18 & 0xff);

                        pck = new LoginWriter(send, gameData);
                        pck.Encrypt();
                        Send(pck.Finalize());
                        break;
                        #endregion
                    case 0x04:
                        #region Server List
                        int serv_num = packet.readB();
                        Servers = new GameServer[serv_num];
                        packet.readB(); // Unknown byte
                        for (int i = 0; i < serv_num; i++)
                        {
                            GameServer server = new GameServer(ref UserConfig, ref gameData, ref Plugins, ref Extensions);
                            server.ID = packet.packetBuffer.ReadByte();
                            server.IP = new IPAddress(BitConverter.GetBytes(packet.packetBuffer.ReadInt32()));
                            server.Port = packet.packetBuffer.ReadInt32();
                            server.AgeLimit = packet.packetBuffer.ReadByte();
                            server.PvP = packet.packetBuffer.ReadByte();
                            server.CurrentPlayers = packet.packetBuffer.ReadInt16();
                            server.MaxPlayers = packet.packetBuffer.ReadInt16();
                            server.Online = packet.packetBuffer.ReadByte();
                            server.ExtraData = packet.packetBuffer.ReadInt32();
                            server.DisplayBrackets = packet.packetBuffer.ReadByte();
                            Servers[i] = server;
                        }
                        try {
                            selectedServer = Int32.Parse(UserConfig.GameServer) - 1;
                        } catch (Exception ex) {
                            Debug.Exception("Unable to parse which gameserver to use.", ex);
                            return;
                        }
                        Debug.Information("Logging into: " + General.ServerNames[selectedServer]);
                        byte[] buff = new byte[32];

                            buff[0] = 0x02;
                            buff[1] = gameData.LoginOK[0];
                            buff[2] = gameData.LoginOK[1];
                            buff[3] = gameData.LoginOK[2];
                            buff[4] = gameData.LoginOK[3];
                            buff[5] = gameData.LoginOK[4];
                            buff[6] = gameData.LoginOK[5];
                            buff[7] = gameData.LoginOK[6];
                            buff[8] = gameData.LoginOK[7];
                            buff[9] = Servers[selectedServer].ID;
                            buff[10] = 0x00;
                            buff[11] = 0x00;
                            buff[12] = 0x00;
                            buff[13] = 0x00;
                            buff[14] = 0x00;
                            buff[15] = 0x00;

                            chk = General.CheckSum(buff, 16);

                            buff[16] = (byte)(chk & 0xff);
                            buff[17] = (byte)(chk >> 0x08 & 0xff);
                            buff[18] = (byte)(chk >> 0x10 & 0xff);
                            buff[19] = (byte)(chk >> 0x18 & 0xff);

                            pck = new LoginWriter(buff, gameData);
                            pck.Encrypt();
                            Send(pck.Finalize());
                        break;
                        #endregion
                    case 0x06:
                        #region PlayFailed
                        message = "";
                        switch (packet.packetBuffer.ReadByte())
                        {
                            case 0x01:
                                message = "System Error.";
                                break;
                            case 0x02:
                                message = "Username or password is wrong.";
                                break;
                            case 0x03:
                                message = "Reason 3.";
                                break;
                            case 0x04:
                                message = "Reason 4.";
                                break;
                            case 0x0F:
                                message = "Too many players connected.";
                                break;
                        }
                        Debug.Error("Unable to connect to the game server, message: " + message);
                        break;
                        #endregion
                    case 0x07:
                        #region PlayOK
                        gameData.PlayOK = new byte[] { packet.readB(), packet.readB(), packet.readB(), packet.readB(), packet.readB(), packet.readB(), packet.readB(), packet.readB() };
                        Debug.Information("Transfering to the GameServer Module.");
                        Close();
                        Servers[selectedServer].StartConnect();
                        break;
                        #endregion
                    default:
                        Debug.Warning("Unknown packet: " + General.Hex(opcode));
                        break;
                }
            }
            else
            {
                Blowfish blowfish = new Blowfish();
                LoginCrypt crypt = new LoginCrypt(General.Hex(UserConfig.BlowfishKey));

                #region Blowfish key stuff
                Debug.Information("Attempting to get Blowfish key.");
                try
                {
                    int size = 180;
                    byte[] raw = inputPacket;
                    raw = crypt.decrypt(raw, 2, size + 4);
                    int key = ByteHelper.ByteIntR(raw, size - 4);

                    int Bloques = (size / 4) - 1;

                    int[] r = new int[Bloques];
                    int t;
                    for (t = Bloques - 1; t > 0; t--)
                    {
                        int p = ByteHelper.ByteIntR(raw, (4 * t)) ^ key;
                        r[Bloques - t - 1] = p;
                        key = ByteHelper.Vuelta(ByteHelper.Vuelta(key) - ByteHelper.Vuelta(p));
                    }
                    r[Bloques - 1] = ByteHelper.ByteIntR(raw, 0);
                    inputPacket = ByteHelper.IntByte(r);

                    int ia;
                    byte[] t2 = new byte[16];
                    for (ia = 0; ia < 16; ia++)
                    {
                        t2[ia] = inputPacket[153 + ia];
                    }

                    gameData.Blowfish_Key = t2;

                    byte[] session = new byte[4];
                    session[0] = inputPacket[1];
                    session[1] = inputPacket[2];
                    session[2] = inputPacket[3];
                    session[3] = inputPacket[4];

                    gameData.Session = session;

                    crypt = new LoginCrypt(gameData.Blowfish_Key);
                }
                catch (Exception ex)
                {
                    Debug.Exception("Error getting Blowfish key", ex);
                    return;
                }
                Debug.Information("Successfully got Blowfish key:");
                Debug.Information(" - " + General.Hex(gameData.Blowfish_Key));
                #endregion

                #region RSA key stuff
                Debug.Information("Attempting to get RSA key.");
                try
                {
                    byte[] enckey = new byte[128];
                    for (int i = 0; i < 128; i++)
                        enckey[i] = inputPacket[9 + i];

                    for (int i = 0; i < 0x40; i++)
                        enckey[0x40 + i] = (byte)(enckey[0x40 + i] ^ enckey[i]);

                    for (int i = 0; i < 4; i++)
                        enckey[0x0d + i] = (byte)(enckey[0x0d + i] ^ enckey[0x34 + i]);

                    for (int i = 0; i < 0x40; i++)
                        enckey[i] = (byte)(enckey[i] ^ enckey[0x40 + i]);

                    for (int i = 0; i < 4; i++)
                    {
                        byte temp = enckey[0x00 + i];
                        enckey[0x00 + i] = enckey[0x4d + i];
                        enckey[0x4d + i] = temp;
                    }

                    gameData.RSA_Key = enckey;
                }
                catch (Exception ex)
                {
                    Debug.Exception("Error getting RSA key", ex);
                    return;
                }
                Debug.Information("Successfully got RSA key:");
                Debug.Information(" - " + General.Hex(gameData.RSA_Key));
                #endregion

                #region GameGuard Login stuff
                try
                {
                    byte[] send = new byte[40];
                    byte[] sende = new byte[40];

                    send[00] = 0x07;
                    send[01] = inputPacket[1];
                    send[02] = inputPacket[2];
                    send[03] = inputPacket[3];
                    send[04] = inputPacket[4];

                    ulong chk = General.CheckSum(send, 24);

                    send[24] = (byte)(chk & 0xff);
                    send[25] = (byte)(chk >> 0x08 & 0xff);
                    send[26] = (byte)(chk >> 0x10 & 0xff);
                    send[27] = (byte)(chk >> 0x18 & 0xff);

                    LoginWriter lw = new LoginWriter(send, gameData);
                    lw.Encrypt();
                    Send(lw.Finalize());
                }
                catch (Exception ex)
                {
                    Debug.Exception("Error sending GameGuard packet.", ex);
                    return;
                }
                Debug.Information("Successfully sent GameGuard packet.");
                #endregion
            }
        }
Пример #36
0
 public void setPrvKey(System.Security.Cryptography.RSAParameters keyInfo)
 {
     //    RSAPrivateKeySpec rsaPrivKeySpec =
     //	new RSAPrivateKeySpec(new BigInteger(n),
     //			      new BigInteger(d));
     //    PrivateKey prvKey = keyFactory.generatePrivate(rsaPrivKeySpec);
     //    signature.initSign(prvKey);
     RSAKeyInfo = keyInfo;
 }
Пример #37
0
        public static void OOG_Login()
        {
#if !DEBUG
			try
			{
#endif
            Globals.l2net_home.Add_Text("Starting login Process", Globals.Red, TextType.BOT);

            OpenLoginServerConnection();

            byte[] buff = new byte[Globals.BUFFER_PACKET];
            byte[] dec_buff = new byte[Globals.BUFFER_PACKET];
            int cnt;

            byte[] sess = new byte[4];
            byte[] enckey = new byte[128];

            //global stream now

            BlowfishEngine bfeng = new BlowfishEngine();

            #region RSA Packet
            cnt = Globals.Login_GameSocket.Receive(buff, 0, Globals.BUFFER_PACKET, System.Net.Sockets.SocketFlags.None);

            //need to unblowfish
            bfeng.init(false, Globals.gamedata.blow_key);
            bfeng.processBigBlock(buff, 2, dec_buff, 0, cnt - 2);

            //need to unxor this shit
            NewCrypt.decXORPass(dec_buff, 0, cnt - 2, System.BitConverter.ToInt32(dec_buff, cnt - 10));

            sess[0] = dec_buff[1];
            sess[1] = dec_buff[2];
            sess[2] = dec_buff[3];
            sess[3] = dec_buff[4];

            //	if(cnt != 155)
            //	{
            //		Globals.l2net_home.Add_Error("packet of wrong size, possible wrong server type...going to continue to attempt login");
            //	}

            //9 thru 136
            //need to decode the RSA key
            for (int i = 0; i < 128; i++)
            {
                enckey[i] = dec_buff[9 + i];
            }

            //got the encoded key in enckey
            // step 4 : xor last 0x40 bytes with  first 0x40 bytes
            for (int i = 0; i < 0x40; i++)
            {
                enckey[0x40 + i] = (byte)(enckey[0x40 + i] ^ enckey[i]);
            }
            // step 3 : xor bytes 0x0d-0x10 with bytes 0x34-0x38
            for (int i = 0; i < 4; i++)
            {
                enckey[0x0d + i] = (byte)(enckey[0x0d + i] ^ enckey[0x34 + i]);
            }
            // step 2 : xor first 0x40 bytes with  last 0x40 bytes 
            for (int i = 0; i < 0x40; i++)
            {
                enckey[i] = (byte)(enckey[i] ^ enckey[0x40 + i]);
            }
            // step 1 : 0x4d-0x50 <-> 0x00-0x04
            for (int i = 0; i < 4; i++)
            {
                byte temp = enckey[0x00 + i];
                enckey[0x00 + i] = enckey[0x4d + i];
                enckey[0x4d + i] = temp;
            }

            Globals.l2net_home.Add_Text("Got RSA key", Globals.Red, TextType.BOT);

            byte[] tmp_bf = new byte[16];

            for (int i = 0; i < 16; i++)
            {
                tmp_bf[i] = dec_buff[153 + i];
            }
            Globals.gamedata.SetBlowfishKey(tmp_bf);
            Globals.l2net_home.Add_Text("Got Blowfish key", Globals.Red, TextType.BOT);


            /*****************game guard shit*******************/
            Globals.l2net_home.Add_Text("lol gameguard", Globals.Red, TextType.BOT);

            //42byte thingy
            byte[] send = new byte[40];
            byte[] sende = new byte[40];

            send[00] = 0x07;
            send[01] = sess[0];
            send[02] = sess[1];
            send[03] = sess[2];
            send[04] = sess[3];
            LoginServer.Set_GG(send, 5);

            NewCrypt.appendChecksum(send, 0, 28);

            bfeng.init(true, Globals.gamedata.blow_key);
            bfeng.processBigBlock(send, 0, sende, 0, 40);

            byte[] pack_out = new byte[42];

            pack_out[0] = 0x2A;
            pack_out[1] = 0x0;
            sende.CopyTo(pack_out, 2);

            Globals.Login_GameSocket.Send(pack_out, 0, 42, System.Net.Sockets.SocketFlags.None);
            #endregion

            bool oog_login = true;
            while (oog_login)
            {
                cnt = Globals.Login_GameSocket.Receive(buff, 0, Globals.BUFFER_PACKET, System.Net.Sockets.SocketFlags.None);

                bfeng.init(false, Globals.gamedata.blow_key);
                bfeng.processBigBlock(buff, 2, dec_buff, 0, cnt - 2);

                switch (dec_buff[0])
                {
                    case 0x00://RSA Packet
                        //handled above
                        break;
                    case 0x01://login fail or security card
                        if (dec_buff[1] == 0x1F)
                        {
                            #region security card
                            Globals.l2net_home.Add_Text("Sending security card packet", Globals.Red, TextType.BOT);

                            string seccard = "06 00 00 00 00 38 8C F0 41 6F 69 D6 25 EA A7 F8 82 64 18 81 EB E0 33 30 73 E4 92 75 1C 7F FF 71 26 36 99 BB CD AD 79 AC CA 27 F0 47 0C 5E 12 72 AD 42 24 96 86 52 82 63 C4 77 AC 5C FF B9 95 82 8B 64 3F F8 AC 52 61 90 AF 5E 26 AF A7 29 1B 71 49 9E 70 E8 CD B2 13 31 5D 70 32 9B 7C 98 BA 8A D1 B9 28 4D 86 CD F6 BE 85 15 E4 29 6D 5A 75 1A F5 D9 CE 32 1D C3 11 57 E1 4E 9E E9 CF 34 BD 3E E9 E5 A8 EC CD 00 00 00 8E A5 0A 82 00 00 00 00 00 00 00 00 00 00 00 00";
                            seccard = seccard.Replace(" ", "");

                            byte[] Bytes = new byte[seccard.Length / 2];
                            int[] HexValue = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D,
                                 0x0E, 0x0F };

                            for (int x = 0, i = 0; i < seccard.Length; i += 2, x += 1)
                            {
                                Bytes[x] = (byte)(HexValue[Char.ToUpper(seccard[i + 0]) - '0'] << 4 |
                                                  HexValue[Char.ToUpper(seccard[i + 1]) - '0']);
                            }


                            byte[] sec_unenc = new byte[152];

                            bfeng.init(true, Globals.gamedata.blow_key);
                            bfeng.processBigBlock(Bytes, 0, sec_unenc, 0, 152);

                            byte[] sec = new byte[154];
                            sec[0] = 0x9A;
                            sec[1] = 0x00;
                            sec_unenc.CopyTo(sec, 2);
                            Globals.Login_GameSocket.Send(sec, 0, 154, System.Net.Sockets.SocketFlags.None);
                            break;
                            #endregion
                        }
                        else if (dec_buff[1] == 0x02)
                        {
                            Globals.l2net_home.Add_Text("Wrong password", Globals.Red, TextType.BOT);
                            oog_login = false;
                            Globals.Login_State = 0;
                            break;
                        }
                        else if (dec_buff[1] == 0x03)
                        {
                            Globals.l2net_home.Add_Text("Wrong username or password", Globals.Red, TextType.BOT);
                            oog_login = false;
                            Globals.Login_State = 0;
                            break;
                        }
                        else if (dec_buff[1] == 0x07)
                        {
                            Globals.l2net_home.Add_Text("Account already in use", Globals.Red, TextType.BOT);
                            oog_login = false;
                            Globals.Login_State = 0;
                            break;
                        }
                        else if (dec_buff[1] == 0x0F)
                        {
                            Globals.l2net_home.Add_Text("Server overloaded", Globals.Red, TextType.BOT);
                            oog_login = false;
                            Globals.Login_State = 0;
                            break;
                        }
                        else if (dec_buff[1] == 0x10)
                        {
                            Globals.l2net_home.Add_Text("Server maintenance", Globals.Red, TextType.BOT);
                            oog_login = false;
                            Globals.Login_State = 0;
                            break;
                        }
                        else
                        {
                            Globals.l2net_home.Add_Text("login fail", Globals.Red, TextType.BOT);
                            oog_login = false;
                            Globals.Login_State = 0;
                            break;
                        }
                    case 0x03://login ok
                        Globals.l2net_home.Add_Text("login ok", Globals.Red, TextType.BOT);
                        #region LoginOK
                        send = new byte[32];

                        send[0] = 0x05;
                        send[1] = dec_buff[1];
                        send[2] = dec_buff[2];
                        send[3] = dec_buff[3];
                        send[4] = dec_buff[4];
                        send[5] = dec_buff[5];
                        send[6] = dec_buff[6];
                        send[7] = dec_buff[7];
                        send[8] = dec_buff[8];
                        send[9] = 0x04;
                        send[10] = 0x00;
                        send[11] = 0x00;
                        send[12] = 0x00;
                        send[13] = 0x00;
                        send[14] = 0x00;
                        send[15] = 0x00;

                        Globals.gamedata.login_ok[0] = dec_buff[1];
                        Globals.gamedata.login_ok[1] = dec_buff[2];
                        Globals.gamedata.login_ok[2] = dec_buff[3];
                        Globals.gamedata.login_ok[3] = dec_buff[4];
                        Globals.gamedata.login_ok[4] = dec_buff[5];
                        Globals.gamedata.login_ok[5] = dec_buff[6];
                        Globals.gamedata.login_ok[6] = dec_buff[7];
                        Globals.gamedata.login_ok[7] = dec_buff[8];

                        NewCrypt.appendChecksum(send, 0, 20);

                        sende = new byte[32];

                        bfeng.init(true, Globals.gamedata.blow_key);
                        bfeng.processBigBlock(send, 0, sende, 0, 32);

                        pack_out = new byte[34];

                        pack_out[0] = 0x22;
                        pack_out[1] = 0x00;
                        sende.CopyTo(pack_out, 2);

                        Globals.Login_GameSocket.Send(pack_out, 0, 34, System.Net.Sockets.SocketFlags.None);
                        break;
                        #endregion
                    case 0x04://serverlist
                        #region ServerList
                        //parse the 208 packet
                        int Login_ServerCount = dec_buff[1];//number of servers

                        int offset = 3;

                        int m = 21;

                        Globals.Login_Servers = new System.Collections.SortedList();
                        Globals.Login_Servers.Capacity = Login_ServerCount;

                        for (int i = 0; i < Login_ServerCount; i++)
                        {
                            try
                            {
                                Server n_sev = new Server();
                                n_sev.ID = (uint)System.Convert.ToSByte(dec_buff[0 + offset + i * m]);
                                n_sev.IP = ((int)dec_buff[1 + offset + i * m]).ToString() + "." + ((int)dec_buff[2 + offset + i * m]).ToString() + "." + ((int)dec_buff[3 + offset + i * m]).ToString() + "." + ((int)dec_buff[4 + offset + i * m]).ToString();
                                n_sev.Port = System.BitConverter.ToInt32(dec_buff, 5 + offset + i * m);
                                Globals.Login_Servers.Add(n_sev.ID, n_sev);
                            }
                            catch
                            {

                            }
                        }

                        Globals.login_window.FillServerInfo(dec_buff);
                        break;
                        #endregion
                    case 0x06://play fail
                        oog_login = false;
                        Globals.Login_State = 0;
                        break;
                    case 0x07://play ok
                        #region PlayOK
                        Globals.gamedata.play_ok[0] = dec_buff[1];
                        Globals.gamedata.play_ok[1] = dec_buff[2];
                        Globals.gamedata.play_ok[2] = dec_buff[3];
                        Globals.gamedata.play_ok[3] = dec_buff[4];
                        Globals.gamedata.play_ok[4] = dec_buff[5];
                        Globals.gamedata.play_ok[5] = dec_buff[6];
                        Globals.gamedata.play_ok[6] = dec_buff[7];
                        Globals.gamedata.play_ok[7] = dec_buff[8];
                        //Globals.gamedata.play_ok[8] = dec_buff[9];

                        oog_login = false;
                        Util.KillServerLoginConnections();

                        LoginServer.PlayOKProcess();
                        break;
                        #endregion
                    case 0x0B://gameguard check verified from server
                        //Query (42): 0B 5B 87 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9E 9D 66 76 E6 16 6D BD 45 C9 28 C0 66 C5 27 A1 BC 4E C6 
                        //Reply (322): 00 7C 78 FD 38 EA AB B6 AE 4F CC 91 C5 24 18 3B 5D 8A 42 BD D0 3B F5 F1 BE 5D 7C 75 4F 08 48 60 E4 23 C7 B0 11 59 CA D5 1C 2A 62 3D D9 50 C8 43 27 0B E1 54 F2 1D EF 8C F5 F2 D2 F4 39 1A 87 5E 9A FA D4 04 10 46 12 8C B5 59 84 AA 23 1C FD 89 69 58 FB 8D 16 3F C8 FA CE 5D BD 36 57 A7 15 B8 BE 91 8D CF 82 D9 FF 83 B9 AB 55 0F 1F 7F 2E 54 A7 55 E1 D8 F4 D4 A5 12 2C 70 19 1D 87 F5 65 1E 4D 5B F4 42 2C 14 F3 DD 16 F3 F0 B5 53 F8 F2 5E 53 99 DC A7 1F 1F BD DE A8 51 94 AD 6E 65 A3 B7 18 54 1B 32 61 CD CF E8 1B E2 0E 24 5A 2B 16 1E 32 61 77 CB 91 C9 A6 A2 2B 80 AA 44 9A BC 22 62 E4 7C A0 45 61 CD F0 60 73 D3 D0 CC C3 44 70 B0 FE FF 6D B8 BD 83 74 8D 53 26 D6 93 50 B3 7B AA 05 8A B7 95 C3 56 4A DC 7C 9C 2E EB A4 6F 42 4D E0 F5 C4 68 E0 7A 9D D7 D7 FD FF 6D D7 21 A3 9A 02 5B 87 04 00 23 01 00 00 67 45 00 00 AB 89 00 00 EF CD 00 00 08 00 00 00 00 00 00 40 4F 61 4F 21 19 3C 62 2A 6A 8B 2A 3B 3D 7C EF 00 00 00 00 03 C8 56 FB 00 00 00 00 00 00 00 00 00 00 00 00 
                        #region GameGuard verify reply
                        Globals.l2net_home.Add_Text("login info - packing username/pw", Globals.Red, TextType.BOT);

                        byte[] login_info_user = new byte[128];
                        byte[] login_info_pass = new byte[128];

                        if (Globals.UserName.Length > 50)
                            Globals.l2net_home.Add_Error("username is too long");
                        if (Globals.Password.Length > 16)
                            Globals.l2net_home.Add_Error("password is too long");

                        //24 thingy?
                        //login_info[0x5B] = 0x24;

                        //pack the username
                        for (int i = 0; i < Globals.UserName.Length; i++)
                        {
                            login_info_user[0x4E + i] = (byte)Globals.UserName[i];
                        }
                        //pack the password
                        for (int i = 0; i < Globals.Password.Length; i++)
                        {
                            login_info_pass[0x5C + i] = (byte)Globals.Password[i];
                        }

                        Globals.l2net_home.Add_Text("login info - rsa start", Globals.Red, TextType.BOT);

                        byte[] Exponent = { 1, 0, 1 };

                        //Create a new instance of RSAParameters.
                        System.Security.Cryptography.RSAParameters RSAKeyInfo = new System.Security.Cryptography.RSAParameters();

                        //Set RSAKeyInfo to the public key values. 
                        RSAKeyInfo.Modulus = enckey;
                        RSAKeyInfo.Exponent = Exponent;

                        RSAManaged poo = new RSAManaged();
                        poo.ImportParameters(RSAKeyInfo);

                        byte[] outb = new byte[128];
                        byte[] outc = new byte[128];

                        outb = poo.EncryptValue(login_info_user);
                        outc = poo.EncryptValue(login_info_pass);

                        Globals.l2net_home.Add_Text("login info - rsa end", Globals.Red, TextType.BOT);

                        byte[] login_send = new byte[320];
                        byte[] login_sende = new byte[320];

                        outb.CopyTo(login_send, 1);
                        outc.CopyTo(login_send, 129);
                        //need to put the other 40bytes here

                        Globals.l2net_home.Add_Text("login info - gameguard start", Globals.Red, TextType.BOT);

                        //old
                        //45 00 01 1E 37 A2 F5 00 00 00 00 00 00 00 00 00
                        //new (TODO need to check this)
                        //23 92 90 4D 18 30 B5 7C 96 61 41 47 05 07 96 FB
                        //23 01 00 00 67 45 00 00 AB 89 00 00 EF CD 00 00 - 1057
                        login_send[257] = sess[0];
                        login_send[258] = sess[1];
                        login_send[259] = sess[2];
                        login_send[260] = sess[3];

                        byte[] query = new byte[16];
                        for (int ii = 0; ii < 16; ii++)
                        {
                            query[ii] = dec_buff[5 + ii];
                        }

                        string gg = "";
                        for (int i = 0; i < query.Length; i++)
                        {
                            gg += query[i].ToString("X2");
                        }

                        if (Globals.GG_List.ContainsKey(gg))
                        {
                            Globals.l2net_home.Add_Text("login info - gameguard known query... sending known reply", Globals.Red, TextType.BOT);
                            byte[] reply = (byte[])Globals.GG_List[gg];

                            //start at byte 5 is the gg query
                            login_send[261] = reply[0];//gameguard reply start
                            login_send[262] = reply[1];
                            login_send[263] = reply[2];
                            login_send[264] = reply[3];
                            login_send[265] = reply[4];//
                            login_send[266] = reply[5];
                            login_send[267] = reply[6];
                            login_send[268] = reply[7];
                            login_send[269] = reply[8];//
                            login_send[270] = reply[9];
                            login_send[271] = reply[10];
                            login_send[272] = reply[11];
                            login_send[273] = reply[12];//
                            login_send[274] = reply[13];
                            login_send[275] = reply[14];
                            login_send[276] = reply[15];//game guard reply stop
                        }
                        else
                        {
                            Globals.l2net_home.Add_Text("login info - gameguard UNknown query... sending reply for blank query...", Globals.Red, TextType.BOT);

                            login_send[261] = 0x23;//gameguard reply start
                            login_send[262] = 0x01;
                            login_send[263] = 0x00;
                            login_send[264] = 0x00;
                            login_send[265] = 0x67;//
                            login_send[266] = 0x45;
                            login_send[267] = 0x00;
                            login_send[268] = 0x00;
                            login_send[269] = 0xAB;//
                            login_send[270] = 0x89;
                            login_send[271] = 0x00;
                            login_send[272] = 0x00;
                            login_send[273] = 0xEF;//
                            login_send[274] = 0xCD;
                            login_send[275] = 0x00;
                            login_send[276] = 0x00;//game guard reply stop
                        }

                        login_send[277] = 0x08;//08 00 00 00 00 00 00

                        //40 4F 61 4F 21 19 3C 62 
                        login_send[284] = 0x40;
                        login_send[285] = 0x4F;
                        login_send[286] = 0x61;
                        login_send[287] = 0x4F;
                        login_send[288] = 0x21;
                        login_send[289] = 0x19;
                        login_send[290] = 0x3C;
                        login_send[291] = 0x62;


                        //2A 6A 8B 2A 3B 3D 7C EF 
                        login_send[292] = 0x2A;
                        login_send[293] = 0x6A;
                        login_send[294] = 0x8B;
                        login_send[295] = 0x2A;
                        login_send[296] = 0x3B;
                        login_send[297] = 0x3D;
                        login_send[298] = 0x7C;
                        login_send[299] = 0xEF;
                        //00 00 00 00 
                        //03 C8 56 FB 
                        login_send[304] = 0x03;
                        login_send[305] = 0xC8;
                        login_send[306] = 0x56;
                        login_send[307] = 0xFB;
                        //00 00 00 00 00 00 00 00 00 00 00 00 

                        //login_send[150] = 0x00;
                        //login_send[151] = 0x00;
                        //login_send[152] = 0x00;
                        //login_send[153] = 0x00;//
                        //login_send[154] = 0x00;
                        //login_send[155] = 0x00;
                        //login_send[156] = 0x00;
                        //login_send[157] = 0x00;//
                        //login_send[158] = 0x00;
                        //login_send[159] = 0x00;

                        Globals.l2net_home.Add_Text("login info - gameguard end/checksum start", Globals.Red, TextType.BOT);

                        NewCrypt.appendChecksum(login_send, 0, 308);

                        Globals.l2net_home.Add_Text("login info - checksum end", Globals.Red, TextType.BOT);

                        //need to encode with blowfish
                        bfeng.init(true, Globals.gamedata.blow_key);
                        bfeng.processBigBlock(login_send, 0, login_sende, 0, 320);

                        Globals.l2net_home.Add_Text("login info - blowfish done", Globals.Red, TextType.BOT);

                        byte[] login_send2 = new byte[322];
                        login_send2[0] = 0x42;
                        login_send2[1] = 0x01;

                        login_sende.CopyTo(login_send2, 2);

                        Globals.l2net_home.Add_Text("login info - sending login info", Globals.Red, TextType.BOT);

                        //this line sends the login data
                        Globals.Login_GameSocket.Send(login_send2, 0, 322, System.Net.Sockets.SocketFlags.None);

                        Globals.l2net_home.Add_Text("login info - login info sent", Globals.Red, TextType.BOT);
                        break;
                        #endregion
                }
            }
#if !DEBUG
			}
			catch
			{
				Globals.l2net_home.Add_Error("crash: OOG Login thread");
			}
#endif
        }
Пример #38
0
        /// <summary>
        /// Returns a set of <see cref="System.Security.Cryptography.RSAParameters">RSA
        /// Parameters</see> precomputed with values from the RFC 7515 Appendix A.2.1
        /// sample.
        /// </summary>
        public static System.Security.Cryptography.RSAParameters GetRsaParamsForRfc7515Example_A_2_1()
        {
            // JSON Web Key (JWK) symmetric key from the RFC example Appendix A.2
            // (with line breaks within values for display purposes only):
            //   {"kty":"RSA",
            //    "n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx
            //         HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs
            //         D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH
            //         SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV
            //         MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8
            //         NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",
            //    "e":"AQAB",
            //    "d":"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I
            //         jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0
            //         BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn
            //         439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT
            //         CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh
            //         BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ",
            //    "p":"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi
            //         YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG
            //         BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc",
            //    "q":"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa
            //         ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA
            //         -njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc",
            //    "dp":"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q
            //         CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb
            //         34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0",
            //    "dq":"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa
            //         7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky
            //         NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU",
            //    "qi":"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o
            //         y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU
            //         W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U"
            //   }

            // With help from:
            //   https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters(v=vs.110).aspx

            var wsRegex = new Regex("\\s+");
            var rsaKeyPartN = wsRegex.Replace(@"
                    ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx
                    HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs
                    D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH
                    SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV
                    MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8
                    NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ", "");
            var rsaKeyPartE = wsRegex.Replace(@"AQAB", "");
            var rsaKeyPartD = wsRegex.Replace(@"
                    Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I
                    jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0
                    BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn
                    439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT
                    CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh
                    BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ", "");
            var rsaKeyPartP = wsRegex.Replace(@"
                    4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi
                    YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG
                    BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc", "");
            var rsaKeyPartQ = wsRegex.Replace(@"
                    uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa
                    ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA
                    -njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc", "");
            var rsaKeyPartDP = wsRegex.Replace(@"
                    BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q
                    CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb
                    34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0", "");
            var rsaKeyPartDQ = wsRegex.Replace(@"
                    h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa
                    7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky
                    NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU", "");
            var rsaKeyPartQI = wsRegex.Replace(@"
                    IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o
                    y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU
                    W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U", "");

            var rsaKeyParams = new System.Security.Cryptography.RSAParameters
            {
                Exponent = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartE),
                Modulus = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartN),
                D = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartD),
                P = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartP),
                Q = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartQ),
                DP = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartDP),
                DQ = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartDQ),
                InverseQ = JOSE.JwsHelper.Base64UrlDecode(rsaKeyPartQI)
            };

            return rsaKeyParams;
        }