Exemple #1
0
        public void CanGenerateKey()
        {
            var protector = new DataProtector();
            var key       = protector.GenerateKey(1);

            Assert.NotNull(key);
        }
Exemple #2
0
        public void CanProtectValue()
        {
            var protector      = new DataProtector();
            var protectedValue = protector.Protect("a value");

            Assert.NotNull(protectedValue);
            Assert.AreNotEqual("a value", protectedValue);
        }
Exemple #3
0
        public void TestProtectEmptyString()
        {
            var dt          = new DataProtector(_cert);
            var bytes       = dt.ProtectBuffer(String.Empty);
            var unencrypted = dt.UnprotectBuffer(bytes);

            Assert.Equal(String.Empty, unencrypted);
        }
Exemple #4
0
        public void TestProtectNullString()
        {
            var dt          = new DataProtector(_cert);
            var bytes       = dt.ProtectBuffer(null);
            var unencrypted = dt.UnprotectBuffer(bytes);

            Assert.Null(unencrypted);
        }
Exemple #5
0
        public void TestProtectUnProtect(string clearText)
        {
            var dt          = new DataProtector(_cert);
            var bytes       = dt.ProtectBuffer(clearText);
            var unencrypted = dt.UnprotectBuffer(bytes);

            Assert.Equal(clearText, unencrypted);
        }
        public void DataProtectorWithDifferentEntropy_CannotDecrypt()
        {
            var entropy2 = new byte[] {7, 8, 9, 10};
              var protector2 = new DataProtector(entropy2);

              var cypher = _sut.Protect(_userData);

              Assert.Throws<CryptographicException>(() => protector2.Unprotect(cypher));
        }
Exemple #7
0
        public void CanUnprotectValue()
        {
            var protector      = new DataProtector();
            var protectedValue = protector.Protect("a value");

            var unprotectedValue = protector.Unprotect(protectedValue);

            Assert.NotNull(protectedValue);
            Assert.That(unprotectedValue, Is.EqualTo("a value"));
        }
Exemple #8
0
        internal static IEnumerable <byte[]> Encrypt(this IEnumerable source, EncryptionSettings settings)
        {
            DataProtector encryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(settings.DataEncryptionKey, settings.EncryptionType);
            ISerializer   serializer          = settings.GetSerializer();

            foreach (var item in source)
            {
                byte[] serializedData = serializer.Serialize(item);
                yield return(encryptionAlgorithm.Encrypt(serializedData));
            }
        }
Exemple #9
0
        public static string EncryptString(string encryptedString)
        {
            // Create an instance of the encryption API
            // We assume the key has been encrypted on this machine and not by a user
            DataProtector dp = new DataProtector(Store.Machine);

            // Use the API to encrypt the connection string
            // API works with bytes so we need to convert to and from byte arrays
            byte[] dataBytes = Encoding.ASCII.GetBytes(encryptedString);
            byte[] encryptedBytes = dp.Encrypt(dataBytes, null);

            // Return the encyrpted data to the string
            return Convert.ToBase64String(encryptedBytes);
        }
Exemple #10
0
        public static string DecryptString(string txtString)
        {
            // If the variable is blank, return the input
            if (txtString.Equals(string.Empty))
            {
                return txtString;
            }

            // Create an instance of the encryption API
            // We assume the key has been encrypted on this machine and not by a user
            DataProtector dp = new DataProtector(Store.Machine);

            // Use the API to decrypt the connection string
            // API works with bytes so we need to convert to and from byte arrays
            byte[] decryptedData = dp.Decrypt(Convert.FromBase64String(txtString), null);

            // Return the decyrpted data to the string
            return Encoding.ASCII.GetString(decryptedData);
        }
 public virtual void SetUp()
 {
     _entropy = new byte[] {1, 2, 3};
       _userData = new byte[] {4, 5, 6};
       _sut = new DataProtector(_entropy);
 }
 public void ConstructWithNullEntropy_IsAllowed()
 {
     var actual = new DataProtector(null);
       Assert.That(actual, Is.Not.Null);
 }
Exemple #13
0
		private void simpleButton_Register_Click(object sender, System.EventArgs e)
		{

#if SoftwareRegister
			SoftwareRegisterForm softwareRegisterForm = new SoftwareRegisterForm();
			softwareRegisterForm.StartPosition = FormStartPosition.CenterScreen;
			softwareRegisterForm.ShowDialog();
			string getSn = SoftwareRegisterForm.GetSn;

			DataProtector dp = new DataProtector(DataProtector.Store.USE_MACHINE_STORE);
			try
			{
				byte[] dataToDecrypt = Convert.FromBase64String(getSn);
				byte[] key = Encoding.ASCII.GetBytes(DataProtector.SetRegisterKeyString());

				string getRetrivedSn = Encoding.ASCII.GetString(dp.RegisterSnDecrypt(dataToDecrypt,key));
				string getInitSn = new DataProtector().InitInfoDecryp(getRetrivedSn);
				string getRetrivedProvince = getInitSn.Substring(0,getInitSn.IndexOf(",",0));
				string getRetrivedUser = getInitSn.Substring(getInitSn.IndexOf("#",0)+1,getInitSn.IndexOf("@",0)-getInitSn.IndexOf("#",0)-1);

				if ( getRetrivedProvince.Equals(CPTT.SystemFramework.Util.PROVINCE_INFO) )
				{
					try
					{
						RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software",true);
						RegistryKey winSysDataKey = softwareKey.CreateSubKey("WindowsDataSystem");
						RegistryKey ctppKey = winSysDataKey.CreateSubKey("Corporation");

						if ( getRetrivedUser.Equals(CPTT.SystemFramework.Util.IS_AGENT_INFO) )
						{
							ctppKey.SetValue("CreateDate",(object)DateTime.Now.Date);
							ctppKey.SetValue("RegisterUser",(object)0);
							ctppKey.SetValue("RegisterDays",(object)254);

							MessageBox.Show("序列号更新成功!\n您已经成为VIP用户,将享有软件所提供的服务,感谢您使用本软件,祝您使用愉快!","系统信息!",
								MessageBoxButtons.OK,MessageBoxIcon.Information);
						}
						else
						{
							if ( ctppKey.GetValue("RegisterUser") == null )
							{
								ctppKey.SetValue("CreateDate",(object)DateTime.Now);
								ctppKey.SetValue("RegisterUser",(object)1);
								ctppKey.SetValue("RegisterDays",(object)1);

								MessageBox.Show("序列号更新成功!\n感谢您使用本软件,祝您使用愉快!","系统信息!",
									MessageBoxButtons.OK,MessageBoxIcon.Information);
							}
							else 
							{
								MessageBox.Show("该序列号所扮演的角色不比当前系统注册角色新,序列号更新失败!","系统信息!",
									MessageBoxButtons.OK,MessageBoxIcon.Information);
								return;
							}
						}
					}
					catch(Exception ex)
					{
						MessageBox.Show("注册时出现未知错误,请与供应商联系!","系统信息!",
							MessageBoxButtons.OK,MessageBoxIcon.Warning);

						CPTT.SystemFramework.Util.WriteLog(ex.Message,CPTT.SystemFramework.Util.EXCEPTION_LOG_TITLE);

						return;
					}
				}
				else MessageBox.Show("您所填写的序列号是非法序列号,请与供应商联系!","系统信息!",
						 MessageBoxButtons.OK,MessageBoxIcon.Warning);
			}
			catch(Exception ex)
			{
				MessageBox.Show("您所填写的序列号是非法序列号,请与供应商联系!","系统信息!",
					MessageBoxButtons.OK,MessageBoxIcon.Warning);

				CPTT.SystemFramework.Util.WriteLog(ex.Message,CPTT.SystemFramework.Util.EXCEPTION_LOG_TITLE);

				return;
			}
#endif
		}