本文整理汇总了C#中DataProtectionScope类的典型用法代码示例。如果您正苦于以下问题:C# DataProtectionScope类的具体用法?C# DataProtectionScope怎么用?C# DataProtectionScope使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DataProtectionScope类属于命名空间,在下文中一共展示了DataProtectionScope类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecryptDataFromStream
public byte[] DecryptDataFromStream(byte[] entropy, DataProtectionScope scope, Stream stream, int length)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (length <= 0)
throw new ArgumentException("Length");
if (entropy == null)
throw new ArgumentNullException("entropy");
if (entropy.Length <= 0)
throw new ArgumentException("Entropy");
var inBuffer = new byte[length];
byte[] outBuffer;
// Read the encrypted data from a stream.
if (stream.CanRead)
{
stream.Read(inBuffer, 0, length);
outBuffer = ProtectedData.Unprotect(inBuffer, entropy, scope);
}
else
{
throw new IOException("Could not read the stream.");
}
// Return the length that was written to the stream.
return outBuffer;
}
示例2: EncryptDataToStream
public int EncryptDataToStream(byte[] buffer, byte[] entropy, DataProtectionScope scope, Stream stream)
{
if (buffer.Length <= 0)
throw new ArgumentException("Buffer");
if (buffer == null)
throw new ArgumentNullException("buffer");
if (entropy.Length <= 0)
throw new ArgumentException("Entropy");
if (entropy == null)
throw new ArgumentNullException("entropy");
if (stream == null)
throw new ArgumentNullException("stream");
var length = 0;
// Encrypt the data in memory. The result is stored in the same same array as the original data.
var encrptedData = ProtectedData.Protect(buffer, entropy, scope);
// Write the encrypted data to a stream.
if (stream.CanWrite)
{
stream.Write(encrptedData, 0, encrptedData.Length);
length = encrptedData.Length;
}
// Return the length that was written to the stream.
return length;
}
示例3: ProtectOrUnprotect
private static byte[] ProtectOrUnprotect(byte[] inputData, byte[] optionalEntropy, DataProtectionScope scope, bool protect)
{
unsafe
{
fixed (byte* pInputData = inputData, pOptionalEntropy = optionalEntropy)
{
DATA_BLOB userDataBlob = new DATA_BLOB((IntPtr)pInputData, (uint)(inputData.Length));
DATA_BLOB optionalEntropyBlob = default(DATA_BLOB);
if (optionalEntropy != null)
{
optionalEntropyBlob = new DATA_BLOB((IntPtr)pOptionalEntropy, (uint)(optionalEntropy.Length));
}
// For desktop compat, we ignore unknown bits in the "scope" value rather than throwing.
CryptProtectDataFlags flags = CryptProtectDataFlags.CRYPTPROTECT_UI_FORBIDDEN;
if (scope == DataProtectionScope.LocalMachine)
{
flags |= CryptProtectDataFlags.CRYPTPROTECT_LOCAL_MACHINE;
}
DATA_BLOB outputBlob = default(DATA_BLOB);
try
{
bool success = protect ?
Interop.Crypt32.CryptProtectData(ref userDataBlob, null, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob) :
Interop.Crypt32.CryptUnprotectData(ref userDataBlob, IntPtr.Zero, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob);
if (!success)
{
int lastWin32Error = Marshal.GetLastWin32Error();
if (protect && ErrorMayBeCausedByUnloadedProfile(lastWin32Error))
throw new CryptographicException(SR.Cryptography_DpApi_ProfileMayNotBeLoaded);
else
throw lastWin32Error.ToCryptographicException();
}
// In some cases, the API would fail due to OOM but simply return a null pointer.
if (outputBlob.pbData == IntPtr.Zero)
throw new OutOfMemoryException();
int length = (int)(outputBlob.cbData);
byte[] outputBytes = new byte[length];
Marshal.Copy(outputBlob.pbData, outputBytes, 0, length);
return outputBytes;
}
finally
{
if (outputBlob.pbData != IntPtr.Zero)
{
int length = (int)(outputBlob.cbData);
byte* pOutputData = (byte*)(outputBlob.pbData);
for (int i = 0; i < length; i++)
{
pOutputData[i] = 0;
}
Marshal.FreeHGlobal(outputBlob.pbData);
}
}
}
}
}
示例4: Protect
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
if (userData == null)
throw new ArgumentNullException(nameof(userData));
return ProtectOrUnprotect(userData, optionalEntropy, scope, protect: true);
}
示例5: Unprotect
public static byte[] Unprotect(byte[] encryptedData, byte[] optionalEntropy, DataProtectionScope scope)
{
if (encryptedData == null)
throw new ArgumentNullException(nameof(encryptedData));
return ProtectOrUnprotect(encryptedData, optionalEntropy, scope, protect: false);
}
示例6: Unprotect
// FIXME [DataProtectionPermission (SecurityAction.Demand, UnprotectData = true)]
public static byte[] Unprotect (byte[] encryptedData, byte[] optionalEntropy, DataProtectionScope scope)
{
if (encryptedData == null)
throw new ArgumentNullException ("encryptedData");
// on Windows this is supported only under 2000 and later OS
Check (scope);
switch (impl) {
#if !MOBILE
case DataProtectionImplementation.ManagedProtection:
try {
return ManagedProtection.Unprotect (encryptedData, optionalEntropy, scope);
}
catch (Exception e) {
string msg = Locale.GetText ("Data unprotection failed.");
throw new CryptographicException (msg, e);
}
case DataProtectionImplementation.Win32CryptoProtect:
try {
return NativeDapiProtection.Unprotect (encryptedData, optionalEntropy, scope);
}
catch (Exception e) {
string msg = Locale.GetText ("Data unprotection failed.");
throw new CryptographicException (msg, e);
}
#endif
default:
throw new PlatformNotSupportedException ();
}
}
示例7: SymmetricAlgorithmProviderData
/// <summary>
/// <para>Initializes a new instance of <see cref="SymmetricAlgorithmProviderData"/> class.</para>
/// </summary>
/// <param name="name"><para>The name for the provider.</para></param>
/// <param name="algorithmType"><para>The type name of the hash algorithm.</para></param>
/// <param name="protectedKeyFilename">File name where key is stored</param>
/// <param name="protectedKeyProtectionScope">DPAPI protection scope used to store key</param>
public SymmetricAlgorithmProviderData(string name, Type algorithmType, string protectedKeyFilename, DataProtectionScope protectedKeyProtectionScope)
: base(name, typeof(SymmetricAlgorithmProvider))
{
AlgorithmType = algorithmType;
ProtectedKeyProtectionScope = protectedKeyProtectionScope;
ProtectedKeyFilename = protectedKeyFilename;
}
示例8: GenerateKey
ProtectedKey GenerateKey(KeyedHashAlgorithm algorithm, DataProtectionScope dataProtectionScope)
{
using (algorithm)
{
return ProtectedKey.CreateFromPlaintextKey(algorithm.Key, dataProtectionScope);
}
}
示例9: Read
/// <overloads>
/// Reads an encrypted key from an input stream. This method is not intended to allow keys to be transferred
/// from another machine.
/// </overloads>
/// <summary>
/// Reads an encrypted key from an input stream. This method is not intended to allow keys to be transferred
/// from another machine.
/// </summary>
/// <param name="inputStream"><see cref="Stream"/> from which DPAPI-protected key is to be read.</param>
/// <param name="dpapiProtectionScope"><see cref="DataProtectionScope"/> used to protect the key on disk. </param>
/// <returns>Key read from stream, encapsulated in a <see cref="ProtectedKey"></see>.</returns>
public static ProtectedKey Read(Stream inputStream, DataProtectionScope dpapiProtectionScope)
{
IKeyReader reader = new KeyReaderWriter();
ProtectedKey key = reader.Read(inputStream, dpapiProtectionScope);
return key;
}
示例10: EncryptDataToStream
public static int EncryptDataToStream(byte[] Buffer, byte[] Entropy, DataProtectionScope Scope, Stream S)
{
if (Buffer.Length <= 0)
throw new ArgumentException("Buffer");
if (Buffer == null)
throw new ArgumentNullException("Buffer");
if (Entropy.Length <= 0)
throw new ArgumentException("Entropy");
if (Entropy == null)
throw new ArgumentNullException("Entropy");
if (S == null)
throw new ArgumentNullException("S");
int length = 0;
// Encrypt the data in memory. The result is stored in the same same array as the original data.
byte[] encrptedData = ProtectedData.Protect(Buffer, Entropy, Scope);
// Write the encrypted data to a stream.
if (S.CanWrite && encrptedData != null)
{
S.Write(encrptedData, 0, encrptedData.Length);
length = encrptedData.Length;
}
// Return the length that was written to the stream.
return length;
}
示例11: Protect
// Summary:
// Protects the userData parameter and returns a byte array.
//
// Parameters:
// userData:
// A byte array containing data to protect.
//
// optionalEntropy:
// An additional byte array used to encrypt the data.
//
// scope:
// One of the System.Security.Cryptography.DataProtectionScope values.
//
// Returns:
// A byte array representing the encrypted data.
//
// Exceptions:
// System.ArgumentNullException:
// The userData parameter is null.
//
// System.Security.Cryptography.CryptographicException:
// The cryptographic protection failed.
//
// System.PlatformNotSupportedException:
// The operating system does not support this method.
//
// System.OutOfMemoryException:
// The system ran out of memory while encrypting the data.
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)
{
Contract.Requires(userData != null);
Contract.Ensures(Contract.Result<byte[]>() != null);
return default(byte[]);
}
示例12: DpapiSymmetricCryptoProvider
/// <summary>
/// <para>Initialize a new instance of the <see cref="DpapiSymmetricCryptoProvider"/></para>
/// </summary>
/// <param name="scope"><para>One of the <see cref="DataProtectionScope"/> values.</para></param>
/// <param name="entropy"><para>The entropy to salt the phrase.</para></param>
/// <param name="instrumentationProvider">Instrumentation provider to use.</param>
public DpapiSymmetricCryptoProvider(DataProtectionScope scope, byte[] entropy, ISymmetricAlgorithmInstrumentationProvider instrumentationProvider)
{
if (instrumentationProvider == null) throw new ArgumentNullException("instrumentationProvider");
this.protectionScope = scope;
this.entropy = entropy;
this.instrumentationProvider = instrumentationProvider;
}
示例13: Unprotect
public static string Unprotect(string encryptedText, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.CurrentUser)
{
if (encryptedText == null)
return ""; // if there is no text, then return an empty string
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
string clearString = GetUnprotectedStringFromBytes(encryptedBytes, optionalEntropy, scope);
return clearString;
}
示例14: GetUnprotectedStringFromBytes
public static string GetUnprotectedStringFromBytes(byte[] encryptedBytes, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.CurrentUser)
{
byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
? null
: Encoding.UTF8.GetBytes(optionalEntropy);
byte[] clearBytes = ProtectedData.Unprotect(encryptedBytes, entropyBytes, scope);
return Encoding.UTF8.GetString(clearBytes);
}
示例15: Protect
public static byte[] Protect(byte[] userData, byte[] optionalEntropy,
DataProtectionScope scope)
{
byte[] pb = new byte[userData.Length];
Array.Copy(userData, pb, userData.Length);
ProtectedMemory.Protect(pb, MemoryProtectionScope.SameProcess);
return pb;
}