/// <summary>
        /// Construct a new protected binary data object. Copy the data from
        /// a <c>XorredBuffer</c> object.
        /// </summary>
        /// <param name="bEnableProtection">Enable protection or not.</param>
        /// <param name="xbProtected"><c>XorredBuffer</c> object used to
        /// initialize the <c>ProtectedBinary</c> object.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the input
        /// parameter is <c>null</c>.</exception>
        public ProtectedBinary(bool bEnableProtection, XorredBuffer xbProtected)
        {
            Debug.Assert(xbProtected != null); if (xbProtected == null)
            {
                throw new ArgumentNullException("xbProtected");
            }

            m_bDoProtect  = bEnableProtection;
            m_xbEncrypted = xbProtected;
        }
示例#2
0
        /// <summary>
        /// Construct a new protected binary data object. Copy the data from
        /// a <c>XorredBuffer</c> object.
        /// </summary>
        /// <param name="bEnableProtection">Enable protection or not.</param>
        /// <param name="xbProtected"><c>XorredBuffer</c> object used to
        /// initialize the <c>ProtectedBinary</c> object.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the input
        /// parameter is <c>null</c>.</exception>
        public ProtectedBinary(bool bEnableProtection, XorredBuffer xbProtected)
        {
            Debug.Assert(xbProtected != null); if (xbProtected == null)
            {
                throw new ArgumentNullException("xbProtected");
            }

            byte[] pb = xbProtected.ReadPlainText();
            Init(bEnableProtection, pb);
            MemUtil.ZeroByteArray(pb);
        }
示例#3
0
        /// <summary>
        /// Construct a new protected string. The string is initialized
        /// to the value passed in the <c>XorredBuffer</c> object.
        /// </summary>
        /// <param name="bEnableProtection">Enable protection or not.</param>
        /// <param name="xbProtected"><c>XorredBuffer</c> object containing the
        /// string in UTF-8 representation. The UTF-8 string must not
        /// be <c>null</c>-terminated.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the input
        /// parameter is <c>null</c>.</exception>
        public ProtectedString(bool bEnableProtection, XorredBuffer xbProtected)
        {
            Debug.Assert(xbProtected != null);
            if (xbProtected == null)
            {
                throw new ArgumentNullException("xbProtected");
            }

            try { m_secString = new SecureString(); }
            catch (NotSupportedException) { }            // Windows 98 / ME

            m_bIsProtected = bEnableProtection;
            m_xbEncrypted  = xbProtected;
        }
示例#4
0
        /// <summary>
        /// Construct a new protected binary data object.
        /// Copy the data from a <c>XorredBuffer</c> object.
        /// </summary>
        /// <param name="bEnableProtection">Enable protection or not.</param>
        /// <param name="xb"><c>XorredBuffer</c> object containing the data.</param>
        public ProtectedBinary(bool bEnableProtection, XorredBuffer xb)
        {
            if (xb == null)
            {
                Debug.Assert(false); throw new ArgumentNullException("xb");
            }

            byte[] pb = xb.ReadPlainText();
            try { Init(bEnableProtection, pb, 0, pb.Length); }
            finally { if (bEnableProtection)
                      {
                          MemUtil.ZeroByteArray(pb);
                      }
            }
        }
示例#5
0
        /// <summary>
        /// Clear the string. Doesn't change the protection level.
        /// </summary>
        public void Clear()
        {
            if (m_secString != null)
            {
                m_secString.Clear();
            }
            else
            {
                m_strAlternativeSecString = string.Empty;
            }

            m_strPlainText = string.Empty;

            m_xbEncrypted = null;
        }
示例#6
0
        public bool EqualsValue(XorredBuffer xb)
        {
            if (xb == null)
            {
                Debug.Assert(false); throw new ArgumentNullException("xb");
            }

            if (xb.m_pbData.Length != m_pbData.Length)
            {
                return(false);
            }

            bool bDecThis  = (m_pbData.Length == m_pbXorPad.Length);
            bool bDecOther = (xb.m_pbData.Length == xb.m_pbXorPad.Length);

            for (int i = 0; i < m_pbData.Length; ++i)
            {
                byte bt1 = m_pbData[i];
                if (bDecThis)
                {
                    bt1 ^= m_pbXorPad[i];
                }

                byte bt2 = xb.m_pbData[i];
                if (bDecOther)
                {
                    bt2 ^= xb.m_pbXorPad[i];
                }

                if (bt1 != bt2)
                {
                    return(false);
                }
            }

            return(true);
        }
        public void ChangeKey()
        {
            // arrange
            var data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            var firstPad = new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
            var protectedData = new byte[data.Length];
            for (var i = 0; i < data.Length; i++)
            {
                protectedData[i] = (byte) (data[i] ^ firstPad[i]);
            }
            var xb = new XorredBuffer(protectedData, firstPad);
            var secondPad = new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };

            // act
            var actual = xb.ChangeKey(secondPad);

            // assert
            Assert.AreEqual(data.Length, actual.Length);
            Assert.AreSame(protectedData, actual);
            for (var i = 0; i < data.Length; i++ )
            {
                Assert.AreEqual(0x20, actual[i]);
            }
        }
		/// <summary>
		/// Clear the string. Doesn't change the protection level.
		/// </summary>
		public void Clear()
		{
			if(m_secString != null) m_secString.Clear();
			else m_strAlternativeSecString = string.Empty;

			m_strPlainText = string.Empty;

			m_xbEncrypted = null;
		}
		/// <summary>
		/// Construct a new protected string. The string is initialized
		/// to the value passed in the <c>XorredBuffer</c> object.
		/// </summary>
		/// <param name="bEnableProtection">Enable protection or not.</param>
		/// <param name="xbProtected"><c>XorredBuffer</c> object containing the
		/// string in UTF-8 representation. The UTF-8 string must not
		/// be <c>null</c>-terminated.</param>
		/// <exception cref="System.ArgumentNullException">Thrown if the input
		/// parameter is <c>null</c>.</exception>
		public ProtectedString(bool bEnableProtection, XorredBuffer xbProtected)
		{
			Debug.Assert(xbProtected != null);
			if(xbProtected == null) throw new ArgumentNullException("xbProtected");

			try { m_secString = new SecureString(); }
			catch(NotSupportedException) { } // Windows 98 / ME

			m_bIsProtected = bEnableProtection;
			m_xbEncrypted = xbProtected;
		}
示例#10
0
		/// <summary>
		/// Construct a new protected string. The string is initialized
		/// to the value passed in the <c>XorredBuffer</c> object.
		/// </summary>
		/// <param name="bEnableProtection">Enable protection or not.</param>
		/// <param name="xbProtected"><c>XorredBuffer</c> object containing the
		/// string in UTF-8 representation. The UTF-8 string must not
		/// be <c>null</c>-terminated.</param>
		public ProtectedString(bool bEnableProtection, XorredBuffer xbProtected)
		{
			Debug.Assert(xbProtected != null);
			if(xbProtected == null) throw new ArgumentNullException("xbProtected");

			byte[] pb = xbProtected.ReadPlainText();
			Init(bEnableProtection, pb);

			if(bEnableProtection) MemUtil.ZeroByteArray(pb);
		}
		private XorredBuffer ProcessNode(XmlReader xr)
		{
			// Debug.Assert(xr.NodeType == XmlNodeType.Element);

			XorredBuffer xb = null;
			if(xr.HasAttributes)
			{
				if(xr.MoveToAttribute(AttrProtected))
				{
					if(xr.Value == ValTrue)
					{
						xr.MoveToElement();
						string strEncrypted = ReadStringRaw(xr);

						byte[] pbEncrypted;
						if(strEncrypted.Length > 0)
							pbEncrypted = Convert.FromBase64String(strEncrypted);
						else pbEncrypted = new byte[0];

						byte[] pbPad = m_randomStream.GetRandomBytes((uint)pbEncrypted.Length);

						xb = new XorredBuffer(pbEncrypted, pbPad);
					}
				}
			}

			return xb;
		}
		/// <summary>
		/// Clear the protected data object. Doesn't change the protection level.
		/// </summary>
		public void Clear()
		{
			m_pbData = new byte[0];
			m_uDataLen = 0;

			m_xbEncrypted = null;
		}
		/// <summary>
		/// Construct a new protected binary data object. Copy the data from
		/// a <c>XorredBuffer</c> object.
		/// </summary>
		/// <param name="bEnableProtection">Enable protection or not.</param>
		/// <param name="xbProtected"><c>XorredBuffer</c> object used to
		/// initialize the <c>ProtectedBinary</c> object.</param>
		/// <exception cref="System.ArgumentNullException">Thrown if the input
		/// parameter is <c>null</c>.</exception>
		public ProtectedBinary(bool bEnableProtection, XorredBuffer xbProtected)
		{
			Debug.Assert(xbProtected != null); if(xbProtected == null) throw new ArgumentNullException("xbProtected");

			m_bDoProtect = bEnableProtection;
			m_xbEncrypted = xbProtected;
		}
示例#14
0
        public bool EqualsValue(XorredBuffer xb)
        {
            if(xb == null) { Debug.Assert(false); throw new ArgumentNullException("xb"); }

            if(xb.m_pbData.Length != m_pbData.Length) return false;

            bool bDecThis = (m_pbData.Length == m_pbXorPad.Length);
            bool bDecOther = (xb.m_pbData.Length == xb.m_pbXorPad.Length);
            for(int i = 0; i < m_pbData.Length; ++i)
            {
                byte bt1 = m_pbData[i];
                if(bDecThis) bt1 ^= m_pbXorPad[i];

                byte bt2 = xb.m_pbData[i];
                if(bDecOther) bt2 ^= xb.m_pbXorPad[i];

                if(bt1 != bt2) return false;
            }

            return true;
        }
示例#15
0
        /// <summary>
        /// Set protected data. This function also clears the internal
        /// <c>XorredBuffer</c> object.
        /// </summary>
        /// <param name="pbNew">Data to store in the protected object. The input
        /// byte array will not be modified, the data is copied to an internal
        /// buffer of the protected object. This parameter must not be <c>null</c>;
        /// if you want to clear the object, call the <c>Clear</c> member
        /// function.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the input
        /// parameter is <c>null</c>.</exception>
        public void SetData(byte[] pbNew)
        {
            Debug.Assert(pbNew != null);
            if(pbNew == null) throw new ArgumentNullException("pbNew");

            // Clear();
            m_xbEncrypted = null;

            m_uDataLen = (uint)pbNew.Length;

            if(m_bDoProtect && m_bProtectionSupported)
            {
                int nAllocatedMem = (((int)m_uDataLen / 16) + 1) * 16;
                m_pbData = new byte[nAllocatedMem];
                Array.Clear(m_pbData, nAllocatedMem - 16, 16); // Clear the rest
                Array.Copy(pbNew, m_pbData, (int)m_uDataLen);

                ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess);
            }
            else // !m_bDoProtect
            {
                m_pbData = new byte[m_uDataLen];
                pbNew.CopyTo(m_pbData, 0);
            }
        }