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"));
            }
        }
Exemple #2
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="resData">byte[]类型的加密字符串</param>
        /// <param name="privateKey">xml格式的私钥</param>
        /// <param name="isOaep">是否使用oaep填充方式和,ture使用oaep,false使用Pkcs1方式</param>
        /// <returns>明文</returns>
        public static byte[] Decrypt(byte[] resData, string xmlPrivateKey, bool isOaep)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            RSACryptoExtensions.FromXmlString(rsa, xmlPrivateKey);
            //rsa.FromXmlString(xmlPrivateKey);
            return(rsa.Decrypt(resData, isOaep));
        }
Exemple #3
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="resData">byte[]类型的需要加密的字符串</param>
        /// <param name="publicKey">xml格式的公钥</param>
        /// <param name="isOaep">是否使用oaep填充方式和,ture使用oaep,false使用Pkcs1方式</param>
        /// <returns>加密后的数据</returns>
        public static string Encrypt(byte[] resData, string xmlPublicKey, bool isOaep)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            RSACryptoExtensions.FromXmlString(rsa, xmlPublicKey);
            //rsa.FromXmlString(xmlPublicKey);
            return(Convert.ToBase64String(rsa.Encrypt(resData, isOaep)));
        }
Exemple #4
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);
            }
        }
Exemple #5
0
        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="content">byte[]类型的待签名字符串</param>
        /// <param name="xml格式的privateKey">xml格式的私钥</param>
        /// <param name="signAlgorithm">签名算法,SHA256/SHA1</param>
        /// <returns>签名后字符串</returns>
        public static byte[] Sign(byte[] content, string xmlPrivateKey, string signAlgorithm)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            RSACryptoExtensions.FromXmlString(rsa, xmlPrivateKey);
            //rsa.FromXmlString(xmlPrivateKey);
            if (Constants.CMBLIFE_SIGN_ALGORITHM_SHA256.Equals(signAlgorithm))
            {
                return(rsa.SignData(content, new SHA256CryptoServiceProvider()));
            }
            else if (Constants.CMBLIFE_SIGN_ALGORITHM_SHA1.Equals(signAlgorithm))
            {
                return(rsa.SignData(content, new SHA1CryptoServiceProvider()));
            }
            else
            {
                throw new ArgumentException("签名算法不合法!");
            }
        }
Exemple #6
0
        static public string getPublicKey()
        {
            string key = RSACryptoExtensions.ToXmlStringCore22(rsa, false);

            return(key);
        }