public ActionResult publicKey([FromHeader] string key)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("Couldn't get the public key");
            }
            if (UserDatabaseAccess.keyCheck(key))
            {
                byte[]        dataToChange = Encoding.ASCII.GetBytes(key);
                byte[]        encrpyt;
                byte[]        decryptData;
                RSAParameters publicKey;
                RSAParameters privateKey;
                string        str;
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                {
                    rsa.PersistKeyInCsp = true;
                    publicKey           = rsa.ExportParameters(false);
                    privateKey          = rsa.ExportParameters(true);

                    encrpyt     = RSAInternal.RSAEncrypt(dataToChange, publicKey);
                    str         = RSACryptoExtensions.ToXmlStringCore22(rsa, false);
                    decryptData = RSAInternal.RSADecrypt(encrpyt, privateKey);
                    RSACryptoExtensions.FromXmlStringCore22(rsa, str);
                }
                return(Ok(str));
            }
            else
            {
                return(Ok("ApiKey invalid"));
            }
        }
Example #2
0
        // Encrypt data using the public key.

        /*static public byte[] RSAEncrypt(byte[] DataToEncrypt, string key)
         * {
         *  try
         *  {
         *      byte[] encryptedData; using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
         *      {
         *          // Convert string to keyParameter
         *          encryptedData = RSA.Encrypt(DataToEncrypt, false);
         *      }
         *      return encryptedData;
         *  }
         *  catch (CryptographicException e) { Console.WriteLine(e.Message); return null; }
         * }*/

        // Decrypt data using the private key.

        /*static public byte[] RSADecrypt(byte[] DataToDecrypt,string key)
         * {
         *  try
         *  {
         *      byte[] decryptedData;
         *      using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
         *      {
         *          RSACryptoExtensions.FromXmlStringCore22(RSA, key);
         *          decryptedData = RSA.Decrypt(DataToDecrypt, true);
         *          RSA.VerifyData()
         *      }
         *      return decryptedData;
         *  }
         *  catch (CryptographicException e)
         *  {
         *      Console.WriteLine(e.ToString());
         *      return null;
         *  }
         * }*/

        static public bool RSAVerify(byte[] data, byte[] signature, string key)
        {
            try
            {
                bool   verified;
                byte[] decryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSACryptoExtensions.FromXmlStringCore22(RSA, key);
                    verified = RSA.VerifyData(data, SHA1.Create(), signature);
                }
                return(verified);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }
        }