コード例 #1
0
        public bool DeleteRSAKey(RSAKeys keysToDelete)
        {
            bool         statusToReturn   = false;
            const string defaultXMLValues = "<RSAKeyValue></RSAKeyValue>";

            try
            {
                string privateKeyString = CheckForFile(_rsaPrivateKeyXMLPath);
                string publicKeyString  = CheckForFile(_rsaPublicKeyXMLPath);

                if (String.IsNullOrEmpty(privateKeyString) || String.IsNullOrEmpty(publicKeyString))
                {
                    return(statusToReturn);
                }

                File.WriteAllText(_rsaPrivateKeyXMLPath, defaultXMLValues);
                File.WriteAllText(_rsaPublicKeyXMLPath, defaultXMLValues);

                statusToReturn = true;
            }
            catch (Exception ex)
            {
                return(statusToReturn);
            }

            return(statusToReturn);
        }
コード例 #2
0
        public bool AddRSAKey(RSAKeys keysToAdd)
        {
            bool statusToReturn = false;

            try
            {
                string privateKeyString = CheckForFile(_rsaPrivateKeyXMLPath);
                string publicKeyString  = CheckForFile(_rsaPublicKeyXMLPath);

                if (String.IsNullOrEmpty(privateKeyString) || String.IsNullOrEmpty(publicKeyString))
                {
                    return(statusToReturn);
                }

                File.WriteAllText(_rsaPrivateKeyXMLPath, keysToAdd.RsaPrivateKey);
                File.WriteAllText(_rsaPublicKeyXMLPath, keysToAdd.RsaPublicKey);

                statusToReturn = true;
            }
            catch (Exception ex)
            {
                return(statusToReturn);
            }

            return(statusToReturn);
        }
コード例 #3
0
 public static RSAParameters MapToRsaParameters(this RSAKeys rsaKeys)
 {
     return(new RSAParameters
     {
         D = rsaKeys.D,
         DP = rsaKeys.DP,
         DQ = rsaKeys.DQ,
         Exponent = rsaKeys.Exponent,
         InverseQ = rsaKeys.InverseQ,
         Modulus = rsaKeys.Modulus,
         P = rsaKeys.P,
         Q = rsaKeys.Q
     });
 }
コード例 #4
0
        static async Task Main(string[] args)
        {
            var rsaProvider = new RSACryptoServiceProvider();

            rsaProvider.ImportParameters(new RSAParameters
            {
                Exponent = Base64UrlEncoder.DecodeBytes("AQAB"),
                Modulus  = Base64UrlEncoder.DecodeBytes("14WuP4Q4BQ6rkwHBIrO8PjArlbSPlPwLRVw4tfzA80I_VOPdqeRmQ_xV2HznKGQCG37sKCq3G-UhIaitYUyW-I_xbqIPKPvPbp9iWd0mg96j0UizdkMj2sc_z5dxTPK9tkoeOrwAm6JSyGG8ksEtpivM_CWCdMslBAsjXmtrQpJ2CE5SunrURGl_uKrSZzV6g_nltrdH3Mi4ebQrLYkfHLbwe0OiTXellyeLrH0C3wYkEnftz8yQ8g4n9VYGaNaclWk2CfjcO5hApQfSJsv7xrmdH2C0Qn5yE85LUqsqlR9cNo7T5A_1d-V1iBxiBXgYr4jB_VwZdLUXr1pScJ_w9Q")
            });

            var publicKey = RSAKeys.ExportPublicKey(rsaProvider);

            Console.WriteLine("\n\n");
        }
コード例 #5
0
    public static RSAKeys generateKeys()
    {
        int N_BITS = 2048;

        //lets take a new CryptoServiceProvider (RSA) with a new N_BIT bit rsa key pair
        var RSA = new RSACryptoServiceProvider(N_BITS);

        RSAKeys return_obj = new RSAKeys();

        return_obj.publicKeyXml  = RSA.ToXmlString(false); //getting public key parameters XML
        return_obj.privateKeyXml = RSA.ToXmlString(true);  //getting private key parameters XMS

        return(return_obj);
    }
コード例 #6
0
        private void buttonEncrypte_Click(object sender, EventArgs e)
        {
            #region --[this should be replaced after testing all key 1111111 output 1]--
            //try
            //{
            //    EncryptionRSA rsa = new EncryptionRSA();

            //    string CompositeKey = rsa.KeyGenrator();
            //    string[] stArray = CompositeKey.Split('.');
            //    textBoxPublicKey.Text = stArray[0];
            //    textBoxPrivateKeyE.Text = stArray[1];
            //}
            //catch
            //{
            //    MessageBox.Show("Exeption Occured");
            //}
            #endregion


            try
            {
                EncryptionRSA  rsa      = new EncryptionRSA();
                TypeConversion tp       = new TypeConversion();
                RSAKeys        rks      = new RSAKeys();
                int            keyPropf = rks.ValidatePublicKeys(textBoxPublicKey.Text);
                if (keyPropf == 0)
                {
                    int b, a = 1;
                    b = a / keyPropf;
                }
                richTextBoxEncrypte.Text             = rsa.Encryptor(tp.CharToInt(richTextBoxEncrypte.Text), textBoxPublicKey.Text);
                buttonEncrypte.Enabled               = false;
                buttonProceedeForCompression.Enabled = true;
            }
            catch (Exception ex)
            {
                buttonProceedeForCompression.Enabled = false;
                buttonEncrypte.Enabled = true;
                MessageBox.Show("System Can Not Encryte Your Message \n\nExplanation:\nPlease there must be text in the main text and\nGenrate/Enter correct Public Key in the respected text box\n\nExeption  :  " + ex.Message + "\n\nPress OK to proceede");
            }
            #region --[this aslo belong to region 1111 output and work with it]--
            //buttonSaveEncryptedCompressedText.Visible = true;
            //buttonEncrypte.Visible = false;
            #endregion
        }
コード例 #7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddControllersWithViews();

            JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
            services
            .AddAuthentication(options =>
            {
                options.DefaultScheme          = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority            = "http://localhost:5000";
                options.RequireHttpsMetadata = false;
                options.ClientId             = "mvc";
                options.ClientSecret         = "secret";
                options.ResponseType         = "code";

                options.UseTokenLifetime = true;

                options.SaveTokens                = true;
                options.UseTokenLifetime          = true;
                options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    RequireExpirationTime    = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new RsaSecurityKey(RSAKeys.ImportPublicKey(File.ReadAllText("public.pem"))),
                };

                options.Scope.Add("email");
                options.Scope.Add("location");
                //options.Scope.Add("api2");
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("location", policy => policy.RequireClaim("location"));
            });

            services.AddMvc();
        }
コード例 #8
0
        /// <summary>
        /// 创建RSA密钥对
        /// </summary>
        private static void GenerateRSAKey()
        {
            _privateKeyRsaProvider = _publicKeyRsaProvider = null;

            //创建RSA对象
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            _privateKeyRsaProvider = rsa;

            //生成RSA[公钥私钥]
            _rsakeys = new RSAKeys();
            _rsakeys.GenerateKeys(rsa);

            //将密钥写入指定路径
            File.WriteAllText(privateKeyPath, _rsakeys.PrivateKey);
            File.WriteAllText(publicKeyPath, _rsakeys.PublicKey);

            // 设置公钥RSA加密对象(不可转成私钥对象)
            _publicKeyRsaProvider = new RSACryptoServiceProvider();
            _publicKeyRsaProvider.FromXmlString(_rsakeys.PublicKey);
        }
コード例 #9
0
        public override bool Execute()
        {
            string text = "Hello!";

            RSAKeys keys = new RSAKeys();

            byte[] encryptedData = RSA.Encrypt(text.GetBytes(), keys);

            if (encryptedData.GetString() == text)
            {
                return(false);
            }

            byte[] decryptedData = RSA.Decrypt(encryptedData, keys);

            if (decryptedData.GetString() != text)
            {
                return(false);
            }

            return(true);
        }
コード例 #10
0
        public RSAKeys GetRSAKeys()
        {
            RSAKeys rsaKeysToReturn = new RSAKeys();

            try
            {
                string privateKeyString = CheckForFile(_rsaPrivateKeyXMLPath);
                string publicKeyString  = CheckForFile(_rsaPublicKeyXMLPath);

                if (String.IsNullOrEmpty(privateKeyString) || String.IsNullOrEmpty(publicKeyString))
                {
                    return(null);
                }

                rsaKeysToReturn.RsaPrivateKey = privateKeyString;
                rsaKeysToReturn.RsaPublicKey  = publicKeyString;
            }
            catch (Exception ex)
            {
                return(null);
            }

            return(rsaKeysToReturn);
        }
コード例 #11
0
 public static void Initialize(RSAKeys keys)
 {
     Rsa           = RSAKey.ParsePrivateKey(keys.N, keys.E, keys.D);
     DiffieHellman = new DiffieHellman();
 }
コード例 #12
0
ファイル: RSAKeys.cs プロジェクト: scottstamp/TanjiCore
 /// <summary>
 /// Returns a value that indicates whether the current structure contains the same public key values as the compared structure.
 /// </summary>
 /// <param name="keys"></param>
 /// <returns></returns>
 public bool EqualsPublic(RSAKeys keys)
 {
     return(Modulus.Equals(keys.Modulus) &&
            Exponent.Equals(keys.Exponent));
 }
コード例 #13
0
ファイル: RSAKeys.cs プロジェクト: scottstamp/TanjiCore
 public bool Equals(RSAKeys keys)
 {
     return(this == keys);
 }