Unmodified KeepassLib from KeePass Desktop v2.20
This commit is contained in:
242
src/KeePassLib2Android/Security/ProtectedBinary.cs
Normal file
242
src/KeePassLib2Android/Security/ProtectedBinary.cs
Normal file
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Diagnostics;
|
||||
|
||||
using KeePassLib.Cryptography;
|
||||
using KeePassLib.Utility;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
namespace KeePassLib.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a protected binary, i.e. a byte array that is encrypted
|
||||
/// in memory. A <c>ProtectedBinary</c> object is immutable and
|
||||
/// thread-safe.
|
||||
/// </summary>
|
||||
public sealed class ProtectedBinary : IEquatable<ProtectedBinary>
|
||||
{
|
||||
private const int PmBlockSize = 16;
|
||||
|
||||
// In-memory protection is supported only on Windows 2000 SP3 and
|
||||
// higher.
|
||||
private static bool m_bProtectionSupported;
|
||||
|
||||
private byte[] m_pbData; // Never null
|
||||
|
||||
// The real length of the data. This value can be different than
|
||||
// m_pbData.Length, as the length of m_pbData always is a multiple
|
||||
// of PmBlockSize (required for fast in-memory protection).
|
||||
private uint m_uDataLen;
|
||||
|
||||
private bool m_bProtected;
|
||||
|
||||
private object m_objSync = new object();
|
||||
|
||||
/// <summary>
|
||||
/// A flag specifying whether the <c>ProtectedBinary</c> object has
|
||||
/// turned on in-memory protection or not.
|
||||
/// </summary>
|
||||
public bool IsProtected
|
||||
{
|
||||
get { return m_bProtected; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Length of the stored data.
|
||||
/// </summary>
|
||||
public uint Length
|
||||
{
|
||||
get { return m_uDataLen; }
|
||||
}
|
||||
|
||||
static ProtectedBinary()
|
||||
{
|
||||
try // Test whether ProtectedMemory is supported
|
||||
{
|
||||
byte[] pbDummy = new byte[PmBlockSize * 2];
|
||||
ProtectedMemory.Protect(pbDummy, MemoryProtectionScope.SameProcess);
|
||||
m_bProtectionSupported = true;
|
||||
}
|
||||
catch(Exception) // Windows 98 / ME
|
||||
{
|
||||
m_bProtectionSupported = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new, empty protected binary data object. Protection
|
||||
/// is disabled.
|
||||
/// </summary>
|
||||
public ProtectedBinary()
|
||||
{
|
||||
Init(false, new byte[0]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new protected binary data object.
|
||||
/// </summary>
|
||||
/// <param name="bEnableProtection">If this paremeter is <c>true</c>,
|
||||
/// the data will be encrypted in memory. If it is <c>false</c>, the
|
||||
/// data is stored in plain-text in the process memory.</param>
|
||||
/// <param name="pbData">Value of the protected object.
|
||||
/// The input parameter is not modified and
|
||||
/// <c>ProtectedBinary</c> doesn't take ownership of the data,
|
||||
/// i.e. the caller is responsible for clearing it.</param>
|
||||
public ProtectedBinary(bool bEnableProtection, byte[] pbData)
|
||||
{
|
||||
Init(bEnableProtection, pbData);
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
|
||||
private void Init(bool bEnableProtection, byte[] pbData)
|
||||
{
|
||||
if(pbData == null) throw new ArgumentNullException("pbData");
|
||||
|
||||
m_bProtected = bEnableProtection;
|
||||
m_uDataLen = (uint)pbData.Length;
|
||||
|
||||
int nBlocks = (int)m_uDataLen / PmBlockSize;
|
||||
if((nBlocks * PmBlockSize) < (int)m_uDataLen) ++nBlocks;
|
||||
Debug.Assert((nBlocks * PmBlockSize) >= (int)m_uDataLen);
|
||||
|
||||
m_pbData = new byte[nBlocks * PmBlockSize];
|
||||
Array.Copy(pbData, m_pbData, (int)m_uDataLen);
|
||||
|
||||
// Data size must be > 0, otherwise 'Protect' throws
|
||||
if(m_bProtected && m_bProtectionSupported && (m_uDataLen > 0))
|
||||
ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a copy of the protected data as a byte array.
|
||||
/// Please note that the returned byte array is not protected and
|
||||
/// can therefore been read by any other application.
|
||||
/// Make sure that your clear it properly after usage.
|
||||
/// </summary>
|
||||
/// <returns>Unprotected byte array. This is always a copy of the internal
|
||||
/// protected data and can therefore be cleared safely.</returns>
|
||||
public byte[] ReadData()
|
||||
{
|
||||
if(m_uDataLen == 0) return new byte[0];
|
||||
|
||||
byte[] pbReturn = new byte[m_uDataLen];
|
||||
|
||||
if(m_bProtected && m_bProtectionSupported)
|
||||
{
|
||||
lock(m_objSync)
|
||||
{
|
||||
ProtectedMemory.Unprotect(m_pbData, MemoryProtectionScope.SameProcess);
|
||||
Array.Copy(m_pbData, pbReturn, (int)m_uDataLen);
|
||||
ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess);
|
||||
}
|
||||
}
|
||||
else Array.Copy(m_pbData, pbReturn, (int)m_uDataLen);
|
||||
|
||||
return pbReturn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the protected data and return it protected with a sequence
|
||||
/// of bytes generated by a random stream.
|
||||
/// </summary>
|
||||
/// <param name="crsRandomSource">Random number source.</param>
|
||||
/// <returns>Protected data.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public byte[] ReadXorredData(CryptoRandomStream crsRandomSource)
|
||||
{
|
||||
Debug.Assert(crsRandomSource != null);
|
||||
if(crsRandomSource == null) throw new ArgumentNullException("crsRandomSource");
|
||||
|
||||
byte[] pbData = ReadData();
|
||||
uint uLen = (uint)pbData.Length;
|
||||
|
||||
byte[] randomPad = crsRandomSource.GetRandomBytes(uLen);
|
||||
Debug.Assert(randomPad.Length == uLen);
|
||||
|
||||
for(uint i = 0; i < uLen; ++i)
|
||||
pbData[i] ^= randomPad[i];
|
||||
|
||||
return pbData;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int h = (m_bProtected ? 0x7B11D289 : 0);
|
||||
|
||||
byte[] pb = ReadData();
|
||||
unchecked
|
||||
{
|
||||
for(int i = 0; i < pb.Length; ++i)
|
||||
h = (h << 3) + h + (int)pb[i];
|
||||
}
|
||||
MemUtil.ZeroByteArray(pb);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as ProtectedBinary);
|
||||
}
|
||||
|
||||
public bool Equals(ProtectedBinary other)
|
||||
{
|
||||
if(other == null) return false; // No assert
|
||||
|
||||
if(m_bProtected != other.m_bProtected) return false;
|
||||
if(m_uDataLen != other.m_uDataLen) return false;
|
||||
|
||||
byte[] pbL = ReadData();
|
||||
byte[] pbR = other.ReadData();
|
||||
bool bEq = MemUtil.ArraysEqual(pbL, pbR);
|
||||
MemUtil.ZeroByteArray(pbL);
|
||||
MemUtil.ZeroByteArray(pbR);
|
||||
|
||||
#if DEBUG
|
||||
if(bEq) { Debug.Assert(GetHashCode() == other.GetHashCode()); }
|
||||
#endif
|
||||
|
||||
return bEq;
|
||||
}
|
||||
}
|
||||
}
|
253
src/KeePassLib2Android/Security/ProtectedString.cs
Normal file
253
src/KeePassLib2Android/Security/ProtectedString.cs
Normal file
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
using KeePassLib.Cryptography;
|
||||
using KeePassLib.Utility;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
// SecureString objects are limited to 65536 characters, don't use
|
||||
|
||||
namespace KeePassLib.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an in-memory encrypted string.
|
||||
/// <c>ProtectedString</c> objects are immutable and thread-safe.
|
||||
/// </summary>
|
||||
#if (DEBUG && !KeePassLibSD)
|
||||
[DebuggerDisplay(@"{ReadString()}")]
|
||||
#endif
|
||||
public sealed class ProtectedString
|
||||
{
|
||||
// Exactly one of the following will be non-null
|
||||
private ProtectedBinary m_pbUtf8 = null;
|
||||
private string m_strPlainText = null;
|
||||
|
||||
private bool m_bIsProtected;
|
||||
|
||||
private static ProtectedString m_psEmpty = new ProtectedString();
|
||||
public static ProtectedString Empty
|
||||
{
|
||||
get { return m_psEmpty; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A flag specifying whether the <c>ProtectedString</c> object
|
||||
/// has turned on in-memory protection or not.
|
||||
/// </summary>
|
||||
public bool IsProtected
|
||||
{
|
||||
get { return m_bIsProtected; }
|
||||
}
|
||||
|
||||
public bool IsEmpty
|
||||
{
|
||||
get
|
||||
{
|
||||
ProtectedBinary pBin = m_pbUtf8; // Local ref for thread-safety
|
||||
if(pBin != null) return (pBin.Length == 0);
|
||||
|
||||
Debug.Assert(m_strPlainText != null);
|
||||
return (m_strPlainText.Length == 0);
|
||||
}
|
||||
}
|
||||
|
||||
private int m_nCachedLength = -1;
|
||||
public int Length
|
||||
{
|
||||
get
|
||||
{
|
||||
if(m_nCachedLength >= 0) return m_nCachedLength;
|
||||
|
||||
ProtectedBinary pBin = m_pbUtf8; // Local ref for thread-safety
|
||||
if(pBin != null)
|
||||
{
|
||||
byte[] pbPlain = pBin.ReadData();
|
||||
m_nCachedLength = StrUtil.Utf8.GetCharCount(pbPlain);
|
||||
MemUtil.ZeroByteArray(pbPlain);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Assert(m_strPlainText != null);
|
||||
m_nCachedLength = m_strPlainText.Length;
|
||||
}
|
||||
|
||||
return m_nCachedLength;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new protected string object. Protection is
|
||||
/// disabled.
|
||||
/// </summary>
|
||||
public ProtectedString()
|
||||
{
|
||||
Init(false, string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new protected string. The string is initialized
|
||||
/// to the value supplied in the parameters.
|
||||
/// </summary>
|
||||
/// <param name="bEnableProtection">If this parameter is <c>true</c>,
|
||||
/// the string will be protected in-memory (encrypted). If it
|
||||
/// is <c>false</c>, the string will be stored as plain-text.</param>
|
||||
/// <param name="strValue">The initial string value. This
|
||||
/// parameter won't be modified.</param>
|
||||
public ProtectedString(bool bEnableProtection, string strValue)
|
||||
{
|
||||
Init(bEnableProtection, strValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new protected string. The string is initialized
|
||||
/// to the value supplied in the parameters (UTF-8 encoded string).
|
||||
/// </summary>
|
||||
/// <param name="bEnableProtection">If this parameter is <c>true</c>,
|
||||
/// the string will be protected in-memory (encrypted). If it
|
||||
/// is <c>false</c>, the string will be stored as plain-text.</param>
|
||||
/// <param name="vUtf8Value">The initial string value, encoded as
|
||||
/// UTF-8 byte array. This parameter won't be modified; the caller
|
||||
/// is responsible for clearing it.</param>
|
||||
public ProtectedString(bool bEnableProtection, byte[] vUtf8Value)
|
||||
{
|
||||
Init(bEnableProtection, vUtf8Value);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if(xbProtected == null) throw new ArgumentNullException("xbProtected");
|
||||
|
||||
byte[] pb = xbProtected.ReadPlainText();
|
||||
Init(bEnableProtection, pb);
|
||||
MemUtil.ZeroByteArray(pb);
|
||||
}
|
||||
|
||||
private void Init(bool bEnableProtection, string str)
|
||||
{
|
||||
if(str == null) throw new ArgumentNullException("str");
|
||||
|
||||
m_bIsProtected = bEnableProtection;
|
||||
|
||||
// The string already is in memory and immutable,
|
||||
// protection would be useless
|
||||
m_strPlainText = str;
|
||||
}
|
||||
|
||||
private void Init(bool bEnableProtection, byte[] pbUtf8)
|
||||
{
|
||||
if(pbUtf8 == null) throw new ArgumentNullException("pbUtf8");
|
||||
|
||||
m_bIsProtected = bEnableProtection;
|
||||
|
||||
if(bEnableProtection)
|
||||
m_pbUtf8 = new ProtectedBinary(true, pbUtf8);
|
||||
else
|
||||
m_strPlainText = StrUtil.Utf8.GetString(pbUtf8, 0, pbUtf8.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert the protected string to a normal string object.
|
||||
/// Be careful with this function, the returned string object
|
||||
/// isn't protected anymore and stored in plain-text in the
|
||||
/// process memory.
|
||||
/// </summary>
|
||||
/// <returns>Plain-text string. Is never <c>null</c>.</returns>
|
||||
public string ReadString()
|
||||
{
|
||||
if(m_strPlainText != null) return m_strPlainText;
|
||||
|
||||
byte[] pb = ReadUtf8();
|
||||
string str = ((pb.Length == 0) ? string.Empty :
|
||||
StrUtil.Utf8.GetString(pb, 0, pb.Length));
|
||||
// No need to clear pb
|
||||
|
||||
// As the text is now visible in process memory anyway,
|
||||
// there's no need to protect it anymore
|
||||
m_strPlainText = str;
|
||||
m_pbUtf8 = null; // Thread-safe order
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read out the string and return a byte array that contains the
|
||||
/// string encoded using UTF-8. The returned string is not protected
|
||||
/// anymore!
|
||||
/// </summary>
|
||||
/// <returns>Plain-text UTF-8 byte array.</returns>
|
||||
public byte[] ReadUtf8()
|
||||
{
|
||||
ProtectedBinary pBin = m_pbUtf8; // Local ref for thread-safety
|
||||
if(pBin != null) return pBin.ReadData();
|
||||
|
||||
return StrUtil.Utf8.GetBytes(m_strPlainText);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the protected string and return it protected with a sequence
|
||||
/// of bytes generated by a random stream.
|
||||
/// </summary>
|
||||
/// <param name="crsRandomSource">Random number source.</param>
|
||||
/// <returns>Protected string.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public byte[] ReadXorredString(CryptoRandomStream crsRandomSource)
|
||||
{
|
||||
Debug.Assert(crsRandomSource != null); if(crsRandomSource == null) throw new ArgumentNullException("crsRandomSource");
|
||||
|
||||
byte[] pbData = ReadUtf8();
|
||||
uint uLen = (uint)pbData.Length;
|
||||
|
||||
byte[] randomPad = crsRandomSource.GetRandomBytes(uLen);
|
||||
Debug.Assert(randomPad.Length == uLen);
|
||||
|
||||
for(uint i = 0; i < uLen; ++i)
|
||||
pbData[i] ^= randomPad[i];
|
||||
|
||||
return pbData;
|
||||
}
|
||||
|
||||
public ProtectedString WithProtection(bool bProtect)
|
||||
{
|
||||
if(bProtect == m_bIsProtected) return this;
|
||||
|
||||
byte[] pb = ReadUtf8();
|
||||
ProtectedString ps = new ProtectedString(bProtect, pb);
|
||||
MemUtil.ZeroByteArray(pb);
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
}
|
116
src/KeePassLib2Android/Security/XorredBuffer.cs
Normal file
116
src/KeePassLib2Android/Security/XorredBuffer.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace KeePassLib.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an object that is encrypted using a XOR pad until
|
||||
/// it is read. <c>XorredBuffer</c> objects are immutable and
|
||||
/// thread-safe.
|
||||
/// </summary>
|
||||
public sealed class XorredBuffer
|
||||
{
|
||||
private byte[] m_pbData; // Never null
|
||||
private byte[] m_pbXorPad; // Always valid for m_pbData
|
||||
|
||||
/// <summary>
|
||||
/// Length of the protected data in bytes.
|
||||
/// </summary>
|
||||
public uint Length
|
||||
{
|
||||
get { return (uint)m_pbData.Length; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new XOR-protected object using a protected byte array
|
||||
/// and a XOR pad that decrypts the protected data. The
|
||||
/// <paramref name="pbProtectedData" /> byte array must have the same size
|
||||
/// as the <paramref name="pbXorPad" /> byte array.
|
||||
/// The <c>XorredBuffer</c> object takes ownership of the two byte
|
||||
/// arrays, i.e. the caller must not use or modify them afterwards.
|
||||
/// </summary>
|
||||
/// <param name="pbProtectedData">Protected data (XOR pad applied).</param>
|
||||
/// <param name="pbXorPad">XOR pad that can be used to decrypt the
|
||||
/// <paramref name="pbProtectedData" /> parameter.</param>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if one of the input
|
||||
/// parameters is <c>null</c>.</exception>
|
||||
/// <exception cref="System.ArgumentException">Thrown if the byte arrays are
|
||||
/// of different size.</exception>
|
||||
public XorredBuffer(byte[] pbProtectedData, byte[] pbXorPad)
|
||||
{
|
||||
if(pbProtectedData == null) { Debug.Assert(false); throw new ArgumentNullException("pbProtectedData"); }
|
||||
if(pbXorPad == null) { Debug.Assert(false); throw new ArgumentNullException("pbXorPad"); }
|
||||
|
||||
Debug.Assert(pbProtectedData.Length == pbXorPad.Length);
|
||||
if(pbProtectedData.Length != pbXorPad.Length) throw new ArgumentException();
|
||||
|
||||
m_pbData = pbProtectedData;
|
||||
m_pbXorPad = pbXorPad;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a copy of the plain-text. The caller is responsible
|
||||
/// for clearing the byte array safely after using it.
|
||||
/// </summary>
|
||||
/// <returns>Unprotected plain-text byte array.</returns>
|
||||
public byte[] ReadPlainText()
|
||||
{
|
||||
byte[] pbPlain = new byte[m_pbData.Length];
|
||||
|
||||
for(int i = 0; i < pbPlain.Length; ++i)
|
||||
pbPlain[i] = (byte)(m_pbData[i] ^ m_pbXorPad[i]);
|
||||
|
||||
return pbPlain;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
for(int i = 0; i < m_pbData.Length; ++i)
|
||||
{
|
||||
byte bt1 = (byte)(m_pbData[i] ^ m_pbXorPad[i]);
|
||||
byte bt2 = (byte)(xb.m_pbData[i] ^ xb.m_pbXorPad[i]);
|
||||
|
||||
if(bt1 != bt2) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool EqualsValue(byte[] pb)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); }
|
||||
|
||||
if(pb.Length != m_pbData.Length) return false;
|
||||
|
||||
for(int i = 0; i < m_pbData.Length; ++i)
|
||||
{
|
||||
if((byte)(m_pbData[i] ^ m_pbXorPad[i]) != pb[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} */
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user