Пример #1
0
 public byte[] Encrypt(string text, Encoding encoding, RSAParameters rsaParameters)
 {
     if (!string.IsNullOrWhiteSpace(text) &&
         !rsaParameters.Equals(default(RSAParameters)))
     {
         try
         {
             using (var rsa = new RSACryptoServiceProvider())
             {
                 var encryptedByteArray = encoding.GetBytes(text);
                 rsa.ImportParameters(rsaParameters);
                 return(rsa.Encrypt(encryptedByteArray, true));
             }
         }
         catch (Exception e)
         {
             Debug.WriteLine($"RSAEncryption exception: {e.Message}");
             return(default(byte[]));
         }
     }
     else
     {
         return(default(byte[]));
     }
 }
        private RSA GetProveedorRSA()
        {
            var rsa = RSA.Create();

            if (claveRSA.Equals(new RSAParameters()))
            {
                try
                {
                    using (var ficheroClave = File.OpenRead(fichero))
                    {
                        using (var pem = new PemReader(ficheroClave))
                        {
                            claveRSA = pem.ReadRsaKey();
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error($"ERR SERVICIOSEGURIDAD (GetProveedorRSA) - {ex.Message}");
                }
            }

            rsa.ImportParameters(claveRSA);
            return(rsa);
        }
        public RSAKeyPairModel(RSAParameters privateKey, RSAParameters publicKey)
        {
            if (privateKey.Equals(default(RSAParameters)) || publicKey.Equals(default(RSAParameters)))
            {
                throw new ArgumentException();
            }

            this._privateKey = RSAParametersModel.ConvertFromRSAParameters(privateKey);
            this._publicKey  = RSAParametersModel.ConvertFromRSAParameters(publicKey);
        }
Пример #4
0
 public override void ExtractParameters()
 {
     if (parameters.Equals(default(RSAParameters)))
     {
         if (blob[0] == 0x30 && blob[1] == 0x82)
         {
             parameters = ExtractParameters(blob);
         }
     }
 }
Пример #5
0
 public override void ExtractParameters()
 {
     if (parameters.Equals(default(RSAParameters)))
     {
         if ((blob[0] << 24 | blob[1] << 16 | blob[2] << 8 | blob[3]) == 7)
         {
             parameters = ExtractParameters(blob);
         }
     }
 }
Пример #6
0
        /// <summary>
        /// Get Parameters
        /// </summary>
        /// <param name="pp">Export private parameters?</param>
        /// <returns></returns>
        public static RSAParameters GenerateKeys(bool pp)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

            if (param.Equals(new RSAParameters()))
            {
                param = RSA.ExportParameters(true);
            }
            RSA.ImportParameters(param);
            return(RSA.ExportParameters(pp));
        }
Пример #7
0
 public byte[] Decrypt(byte[] data, RSAParameters rsaParameters)
 {
     if (data.Length > 0 &&
         !rsaParameters.Equals(default(RSAParameters)))
     {
         try
         {
             using (var rsa = new RSACryptoServiceProvider())
             {
                 rsa.ImportParameters(rsaParameters);
                 return(rsa.Decrypt(data, true));
             }
         }
         catch (Exception e)
         {
             Debug.WriteLine($"RSADecryption exception: {e.Message}");
             return(default(byte[]));
         }
     }
     return(default(byte[]));
 }
Пример #8
0
        public void Asymmetrical_Key_Regeneration_Attempt()
        {
            Server s = new Server();

            ThreadPool.QueueUserWorkItem(s.ImServer.StartServer, null);
            Thread.Sleep(20);

            ImClientHandler client =
                new ImClientHandler(s.ImServer.IpAddress.ToString(), s.ImServer.PortAddress);

            client.Login(_clients[0]);
            Thread.Sleep(200);

            RSAParameters oldKey = client.PublicKey;

            IMClient.utils.Cryptography.DeleteKeyFromContainer();
            IMClient.utils.Cryptography.GenerateKeys(out client.PublicKey);

            Thread.Sleep(1000);

            Assert.IsFalse(oldKey.Equals(client.PublicKey));
            s.ImServer.StopServer();
        }