Пример #1
0
        public IHttpActionResult Get(string domain)
        {
            if (string.IsNullOrEmpty(domain))
            {
                return(BadRequest());
            }

            LicenseData license = LicenseRepository.Instance.LicenseTable[domain] as LicenseData;

            if (license == null)
            {
                return(NotFound());
            }

            var clientData = DataRepository.Instance.ClienDataList.SingleOrDefault(data => data.Domain == domain);

            if (clientData == null)
            {
                return(NotFound());
            }

            var publicKey = clientData.RemoteKey;
//			var licenseData = Encoding.UTF8.GetString(ObjectSerializer.Serialize(license));

//			byte[] encryptedLicense = AsymmetricEncryption.Encrypt(ObjectSerializer.Serialize(license), 1024, clientData.Key.PublicKey);
            var encryptedLicense = AsymmetricEncryption.Encrypt(ObjectSerializer.Serialize(license), 4096, publicKey);

            string encryptedLicenseStr           = Convert.ToBase64String(encryptedLicense);
            Dictionary <string, string> dataDict = new Dictionary <string, string> {
                { "data", encryptedLicenseStr }
            };

            return(Ok(dataDict));
        }
Пример #2
0
        public void BytecodeApi_Cryptography_AsymmetricEncryption()
        {
            byte[] data = MathEx.RandomNumberGenerator.GetBytes(32);
            AsymmetricEncryption.GenerateKeyPair(out RSAParameters publicKey, out RSAParameters privateKey);

            Assert.IsTrue(AsymmetricEncryption.Decrypt(AsymmetricEncryption.Encrypt(data, Resources.RSAPublicKey), Resources.RSAPrivateKey).Compare(data));
            Assert.IsTrue(AsymmetricEncryption.Decrypt(AsymmetricEncryption.Encrypt(data, publicKey), privateKey).Compare(data));
        }
Пример #3
0
        public void Can_Encrypt_Data()
        {
            byte[] rawData = Random.GetNumbers(128);

            byte[] encryptedData = AsymmetricEncryption.Encrypt(rawData, publicKey);

            Assert.NotNull(encryptedData);
            Assert.AreNotEqual(rawData, encryptedData);
        }
Пример #4
0
        public static void Main(string[] args)
        {
            string text          = "123456";
            RSA    rsa           = RSA.Create();
            string encryptedText = AsymmetricEncryption.Encrypt(text, rsa);
            string decryptedText = AsymmetricEncryption.Decrypt(encryptedText, rsa);

            Console.WriteLine("haha");
            CreateWebHostBuilder(args).Build().Run();
            //Test secure password
        }
        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);
        }
        public void EncryptedData_DecryptWithPublicKey_IsNotSuccessfullyDecrypted()
        {
            // Arrange
            var message = "my super secret message";
            var key     = AsymmetricEncryption.GenerateKey(32);

            var encryptedData = AsymmetricEncryption.Encrypt(message, key.PublicKey);

            // Act / Assert
            Assert.ThrowsAny <System.Security.Cryptography.CryptographicException>(() => { AsymmetricEncryption.DecryptValue(encryptedData.EncryptedData, key.PublicKey); });
        }
        public void EncryptedData_WithKey_IsSuccessfullyDecrypted()
        {
            // Arrange
            var message = "my super secret message";
            var key     = AsymmetricEncryption.GenerateKey(32);

            // Act
            var encryptedData = AsymmetricEncryption.Encrypt(message, key.PublicKey);
            var decryptedData = AsymmetricEncryption.DecryptValue(encryptedData.EncryptedData, key.PrivateKey);

            // Assert
            Assert.Equal(message, decryptedData);
            Assert.Equal("RSA", encryptedData.Method);
            Assert.NotEqual(encryptedData.EncryptedData, System.Text.Encoding.UTF8.GetBytes(message));
        }
Пример #8
0
        public void DecryptWithoutPrivateKeyTest()
        {
            var encryption = new AsymmetricEncryption(_publicKey, null);

            Assert.Throws(typeof(CryptographicException), () => { encryption.DecryptToString(encryption.Encrypt("aw3lrifos83fusoi3fjsofisjfo")); });
        }
Пример #9
0
        public void EncryptWithoutPublicKeyTest()
        {
            var encryption = new AsymmetricEncryption(null, _privateKey);

            Assert.Throws(typeof(CryptographicException), () => { encryption.Encrypt("aw3lrifos83fusoi3fjsofisjfo"); });
        }
Пример #10
0
        public void EncryptionStringEqualsTest()
        {
            var encryption = new AsymmetricEncryption(_publicKey, _privateKey);

            Assert.AreEqual("aw3lrifos83fusoi3fjsofisjfo", encryption.DecryptToString(encryption.Encrypt("aw3lrifos83fusoi3fjsofisjfo")));
        }
Пример #11
0
        /// <summary>
        /// Encrypt the content to be sent to the audience, using audience's public key
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Encrypt(string keyName, string content)
        {
            var audience = keys[keyName];

            return(AsymmetricEncryption.Encrypt(content, audience.PublicKey));
        }