예제 #1
0
        private UserKeys GenerateNewKeyPair()
        {
            var rsa = new Chilkat.Rsa();

            rsa.GenerateKey(RsaKeySize);

            var publicKey  = rsa.ExportPublicKey();
            var privateKey = rsa.ExportPrivateKey();

            var symetricKey = EncryptionManager.GenerateSymmetricKey();

            var rsaEncryptor = new Chilkat.Rsa {
                EncodingMode = EncodingMode
            };

            rsaEncryptor.ImportPrivateKey(privateKey);

            // Encrypted with private so that public key can get access to symmetric key
            var encryptedSymetricKey = rsaEncryptor.EncryptStringENC(symetricKey, true);

            // Encrypted with application key so that the application can manage access to private keys
            var encryptedPrivateKey = EncryptionManager.Encrypt(privateKey, string.Empty);

            return(UserKeys.Create(encryptedPrivateKey, publicKey, encryptedSymetricKey));
        }
예제 #2
0
        public static Tuple <string, string> GenerateRsaKeyPairStandalone(out string error)
        {
            Tuple <string, string> keyPair;

            try
            {
                Chilkat.Rsa rsa = new Chilkat.Rsa();

                // Generate a 1024-bit RSA key pair.
                if (!rsa.GenerateKey(1024))
                {
                    throw new Exception(rsa.LastErrorText);
                }

                keyPair = new Tuple <string, string>(item1: rsa.ExportPrivateKey(), item2: rsa.ExportPublicKey());

                error = null;
                return(keyPair);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(new Tuple <string, string>(null, null));
            }
        }
예제 #3
0
        public PublicPrivateKey GetKeys()
        {
            Chilkat.Rsa rsa = new Chilkat.Rsa();
            rsa.GenerateKey(1024);

            string publicKey  = rsa.ExportPublicKey();
            string privateKey = rsa.ExportPrivateKey();

            var data = new PublicPrivateKey()
            {
                PrivateKey = privateKey,
                PublicKey  = publicKey
            };

            return(data);
        }
예제 #4
0
 /// <summary>
 /// Decrypt using a Given Private Key
 /// </summary>
 /// <param name="StringToDecrypt">w to Decrypt</param>
 /// <param name="PrivateKey"></param>
 /// <param name="EncodingMode">base64, hex</param>
 /// <returns></returns>
 public static string Decrypt(string StrToDecrypt, string PrivateKey, string EncodingMode)
 {
     try
     {
         /// First we get the RSA Public Key
         string RSAPrivateKey = PrivateKey;
         /// rsa object
         Chilkat.Rsa rsa = new Chilkat.Rsa();
         /// bool for success
         bool success;
         /// unlock component
         success = rsa.UnlockComponent("VIENTORSA_TbpfVVr01Or6");
         /// private key
         string privateKey;
         ///we now export the private key
         privateKey = rsa.ExportPrivateKey();
         //  Now decrypt:
         Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa();
         /// we encode in 64 base bits
         rsaDecryptor.EncodingMode = EncodingMode;
         ///rsa import private
         rsaDecryptor.ImportPrivateKey(RSAPrivateKey);
         /// decrypted string
         string decryptedStr;
         ///we now decript the string
         decryptedStr = rsaDecryptor.DecryptStringENC(StrToDecrypt, true);
         /// return the string
         return decryptedStr;
     }
     catch
     {
         return null;
     }
     finally
     {
         GC.Collect();
     }
 }
예제 #5
0
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(TbLastName.Text) ||
                string.IsNullOrWhiteSpace(TbFirstName.Text) ||
                string.IsNullOrWhiteSpace(TbLogin.Text) ||
                string.IsNullOrWhiteSpace(TbPassword.Text) ||
                string.IsNullOrWhiteSpace(TbPhone.Text) ||
                string.IsNullOrWhiteSpace(TbEmail.Text))
            {
                MessageBox.Show("Пустые поля!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            DeliveryPoint point = CmbPoints.SelectedItem as DeliveryPoint;

            if (i == 0)
            {
                Person person = new Person()
                {
                    Email      = TbEmail.Text,
                    LastName   = TbLastName.Text,
                    FirstName  = TbFirstName.Text,
                    Login      = TbLogin.Text,
                    Password   = TbPassword.Text,
                    MiddleName = TbMiddleName.Text,
                    Phone      = TbPhone.Text,
                };
                Helper.context.People.Add(person);
                Helper.context.SaveChanges();
                Chilkat.Rsa rsa = new Chilkat.Rsa();
                rsa.GenerateKey(1024);
                string publicKey  = rsa.ExportPublicKey();
                string privateKey = rsa.ExportPrivateKey();
                Client client     = new Client()
                {
                    PersonId   = person.Id,
                    Wallet     = Convert.ToDecimal(TbWallet.Text),
                    PublicKey  = publicKey,
                    PrivateKey = privateKey,
                };
                Helper.context.Clients.Add(client);
                Helper.context.SaveChanges();
                ClientPoint clientPoint = new ClientPoint()
                {
                    ClientId        = client.Id,
                    DeliveryPointId = point.Id,
                };
                Helper.context.ClientPoints.Add(clientPoint);
                Helper.context.SaveChanges();
                var message = MessageBox.Show("Добавить еще одну точку забора, данному клиенту?", "Точка забора", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (message == MessageBoxResult.Yes)
                {
                    i++;
                }
                else
                {
                    i = 0;
                    Close();
                }
            }
            else if (i > 0)
            {
                var         lastClient  = Helper.context.Clients.OrderByDescending(x => x.Id).FirstOrDefault();
                ClientPoint clientPoint = new ClientPoint()
                {
                    ClientId        = lastClient.Id,
                    DeliveryPointId = point.Id,
                };
                Helper.context.ClientPoints.Add(clientPoint);
                Helper.context.SaveChanges();
                var message = MessageBox.Show("Добавить еще одну точку забора, данному клиенту?", "Точка забора", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (message == MessageBoxResult.Yes)
                {
                    i++;
                }
                else
                {
                    i = 0;
                    Close();
                }
            }
        }
예제 #6
0
        public static void performRSA(string text)
        {
            Chilkat.Rsa rsa = new Chilkat.Rsa();

            bool success = rsa.UnlockComponent("Anything for 30-day trial");

            if (success != true)
            {
                Console.WriteLine("RSA component unlock failed");
                return;
            }

//  This example also generates the public and private
//  keys to be used in the RSA encryption.
//  Normally, you would generate a key pair once,
//  and distribute the public key to your partner.
//  Anything encrypted with the public key can be
//  decrypted with the private key.  The reverse is
//  also true: anything encrypted using the private
//  key can be decrypted using the public key.

//  Generate a 1024-bit key.  Chilkat RSA supports
//  key sizes ranging from 512 bits to 4096 bits.
            success = rsa.GenerateKey(1024);
            if (success != true)
            {
                Console.WriteLine(rsa.LastErrorText);
                return;
            }

//  Keys are exported in XML format:
            string publicKey  = rsa.ExportPublicKey();
            string privateKey = rsa.ExportPrivateKey();

            string plainText = "Encrypting and decrypting should be easy!";

            plainText = text;
//  Start with a new RSA object to demonstrate that all we
//  need are the keys previously exported:
            Chilkat.Rsa rsaEncryptor = new Chilkat.Rsa();

//  Encrypted output is always binary.  In this case, we want
//  to encode the encrypted bytes in a printable string.
//  Our choices are "hex", "base64", "url", "quoted-printable".
            rsaEncryptor.EncodingMode = "hex";

//  We'll encrypt with the public key and decrypt with the private
//  key.  It's also possible to do the reverse.
            success = rsaEncryptor.ImportPublicKey(publicKey);

            bool   usePrivateKey = false;
            string encryptedStr  = rsaEncryptor.EncryptStringENC(plainText, usePrivateKey);

//Console.WriteLine(encryptedStr);

//  Now decrypt:
            Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa();

            rsaDecryptor.EncodingMode = "hex";
            success = rsaDecryptor.ImportPrivateKey(privateKey);

            usePrivateKey = true;
            string decryptedStr = rsaDecryptor.DecryptStringENC(encryptedStr, usePrivateKey);

//Console.WriteLine(decryptedStr);
        }
예제 #7
0
 /// <summary>
 /// Method to Generate new keys according Possition
 /// { "keys" :
 /// { "0" : private key,
 ///   "1" : public key }
 /// }
 /// </summary>
 /// <returns></returns>
 public static List<string> GenerateNewRSAKeyPair()
 {
     /// rsa object from library
     Chilkat.Rsa rsa = new Chilkat.Rsa();
     /// unlock component
     bool success = rsa.UnlockComponent("VIENTORSA_TbpfVVr01Or6");
     /// we generate the amount key from amount of bits
     success = rsa.GenerateKey(2048);
     /// this is the public key
     string publicKey = rsa.ExportPublicKey();
     /// this is the private key
     string privateKey = rsa.ExportPrivateKey();
     /// we return the new elements
     return new List<string>() { privateKey, publicKey };
 }