public void Encrypt()
        {
            var data = "P@ssw0rd";

            var keys      = AsymmetricEncryption.GenerateKeys();
            var encrypted = AsymmetricEncryption.Encrypt(data, keys.PublicKey);
            var decrypted = AsymmetricEncryption.Decrypt(encrypted, keys.KeysPair);

            Console.WriteLine(encrypted);
            Assert.AreEqual(data, decrypted);
        }
Beispiel #2
0
        private static void ForTempSave(string userName)
        {
            const int keySize = 2048;
            string    publicAndPrivateKey;
            string    publicKey;
            var       root = new XElement("Root");

            root.SetAttributeValue(Constants._ID, Guid.NewGuid().ToString());
            AsymmetricEncryption.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);
            Console.WriteLine(@"public key:" + publicKey);
            Console.WriteLine(@"public & private key:" + publicAndPrivateKey);
            root.SetAttributeValue("PublicKey", publicKey);
            root.SetAttributeValue("PublicAndPrivateKey", publicAndPrivateKey);
            var productid = AsymmetricEncryption.GetProductId();

            root.SetAttributeValue("ProductId", productid);

            //string userName = "******";
            var text      = userName + productid;
            var encrypted = AsymmetricEncryption.EncryptText(text, keySize, publicKey);

            Console.WriteLine(@"Encrypted: {0}", encrypted);

            //send encrypted data to service
            File.WriteAllText(userName + ".pem",
                              @"UserName:
"******"
Public Key:" + publicKey + @"
Public and Private Key:
" +
                              publicAndPrivateKey + @"Secrect:
" + encrypted + @"
For Test:
" + productid);
            var decrypted = AsymmetricEncryption.DecryptText(encrypted, keySize, publicAndPrivateKey);

            //service person do below
            Console.WriteLine(@"Decrypted: {0}", decrypted);

            //            Configuration.Set("UserName", userName);
            //            Configuration.Set("PublicKey", publicKey);
            //            Configuration.SaveSettings();
            DBFactory.GetData().Save(root);
            Console.WriteLine(DBFactory.GetData().Read("a02cf4ad-ba0c-4c69-9f7c-e7d73a8fecad"));
        }
Beispiel #3
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string path = @"D://CertificateAuthorityPublicAndPrivateKey.txt";


            bool flag;

            // This text is added only once to the file.
            if (!File.Exists(path))
            {
                // Create a file to write to.
                int keySize = Convert.ToInt32(1024);
                RSACryptoServiceProvider rsaProvider = AsymmetricEncryption.GenerateKeys(keySize);
                publicAndprivateKey = rsaProvider.ToXmlString(true);
                pu = rsaProvider.ExportParameters(true);
                Stream     stream    = new FileStream(path, FileMode.Create);
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, pu);

                stream.Close();
                flag = true;
            }
            else
            {
                Stream     stream    = new FileStream(path, FileMode.Open);
                IFormatter formatter = new BinaryFormatter();
                stream.Seek(0, SeekOrigin.Begin);
                pu   = (RSAParameters)formatter.Deserialize(stream);
                flag = true;
                stream.Close();
            }


            if (flag)
            {
                Application.Run(new CAForm());
            }
            else
            {
                MessageBox.Show("Error in generate key.");
            }
        }
        public ActionResult Register(RegisterData data)
        {
            //			SymmetricEncryption se = new SymmetricEncryption();
            DataRepository.Instance.RegisterList.Add(data);

            string publicKey;
            string privateKey;

            AsymmetricEncryption.GenerateKeys(4096, out publicKey, out privateKey);

            ClientData clientData = new ClientData
            {
                Domain = data.Domain,
                Key    = new LocalKey
                {
                    PrivateKey = privateKey,
                    PublicKey  = publicKey
                }
            };

            DataRepository.Instance.ClienDataList.Add(clientData);

            return(View(clientData));
        }
        public async Task <ActionResult> Create(DomainData data)
        {
            SymmetricEncryption se = new SymmetricEncryption();

            DataRepository.Instance.Domain    = data.Domain;
            DataRepository.Instance.RemoteKey = se.Decrypt(data.Key, data.Domain);

            string privateKey;
            string publicKey;

            AsymmetricEncryption.GenerateKeys(4096, out publicKey, out privateKey);

            DataRepository.Instance.Key = new LocalKey
            {
                PrivateKey = privateKey,
                PublicKey  = publicKey
            };

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:4427/");
//				client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                string encryptedPublicKey         = se.Encrypt(DataRepository.Instance.Key.PublicKey, DataRepository.Instance.Domain);
                Dictionary <string, string> pData = new Dictionary <string, string>
                {
                    { "Key", encryptedPublicKey },
                    { "Domain", DataRepository.Instance.Domain }
                };

                HttpResponseMessage response1 = await client.PostAsJsonAsync("api/ClientConnection", pData);

                if (!response1.IsSuccessStatusCode)
                {
                    return(View((object)response1.ToString()));
                }

                var response2 = await client.GetAsync("api/Licenses?domain=" + DataRepository.Instance.Domain);

                if (!response2.IsSuccessStatusCode)
                {
                    return(View((object)response2.ToString()));
                }

                var responseContent = await response2.Content.ReadAsAsync <Dictionary <string, string> >();

                LicenseFile.Write(responseContent["data"]);

/*
 *                              byte[] encryptedData = Convert.FromBase64String(responseContent["data"]);
 *
 *                              var licenseData = AsymmetricEncryption.Decrypt(encryptedData, 4096, DataRepository.Instance.Key.PrivateKey);
 *                              LicenseData license = ObjectSerializer.Deserialize(licenseData) as LicenseData;
 */

                //string result = await response.Content.ReadAsStringAsync();
//				return View((object)license);
                return(View("ReadLicense"));
            }

            //return View((object)DataRepository.Instance.RemoteKey);
        }
Beispiel #6
0
 static HomeController()
 {
     AsymmetricEncryption.GenerateKeys(KEYSIZE, out _publicKey, out _publicAndPrivateKey);
 }
Beispiel #7
0
        public Form1()
        {
            InitializeComponent();

            AsymmetricEncryption.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);
        }