本文整理汇总了C#中EncryptionType.HasFlag方法的典型用法代码示例。如果您正苦于以下问题:C# EncryptionType.HasFlag方法的具体用法?C# EncryptionType.HasFlag怎么用?C# EncryptionType.HasFlag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EncryptionType
的用法示例。
在下文中一共展示了EncryptionType.HasFlag方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
public static byte[] Decrypt(byte[] encrypted, EncryptionType encType, string userPassword)
{
if (encType == EncryptionType.None)
return encrypted;
// Decrypt second pass (AES/PBKDF2)
byte[] secondPassDecrypt;
if (encType.HasFlag(EncryptionType.Password))
{
try
{
secondPassDecrypt = AESDecrypt(encrypted, userPassword);
}
catch (Exception) { return null; }
}
else
{
secondPassDecrypt = encrypted;
}
// Decrypt first pass (Machine/User Lock)
byte[] decrypted;
if (encType.HasFlag(EncryptionType.LocalUser) || encType.HasFlag(EncryptionType.LocalMachine))
{
try
{
decrypted = MachineDecrypt(secondPassDecrypt, encType);
}
catch (Exception) { return null; }
}
else
{
decrypted = secondPassDecrypt;
}
// Verify token signature
if (decrypted.Length != 30)
return null;
byte[] token = new byte[20];
Array.Copy(decrypted, token, 20);
byte[] signature = new byte[10];
Array.Copy(decrypted, 20, signature, 0, 10);
byte[] expectedSignature = SignToken(token);
if (expectedSignature.SequenceEqual(signature))
return token;
return null;
}
示例2: MachineEncrypt
private static byte[] MachineEncrypt(byte[] plainText, EncryptionType encType)
{
DataProtectionScope scope = (encType.HasFlag(EncryptionType.LocalUser)) ? DataProtectionScope.CurrentUser : DataProtectionScope.LocalMachine;
return ProtectedData.Protect(plainText, WINBMA_SECRET, scope);
}
示例3: Encrypt
public static byte[] Encrypt(byte[] token, EncryptionType encType, string userPassword)
{
if (encType == EncryptionType.None)
return token;
// Sign Token (for verification at decryption time)
byte[] signature = SignToken(token);
byte[] plainText = new byte[30];
Array.Copy(token, plainText, 20);
Array.Copy(signature, 0, plainText, 20, 10);
// Encrypt first pass (Machine/User Lock)
byte[] firstPassEncrypted;
if (encType.HasFlag(EncryptionType.LocalUser) || encType.HasFlag(EncryptionType.LocalMachine))
{
firstPassEncrypted = MachineEncrypt(plainText, encType);
}
else
{
firstPassEncrypted = plainText;
}
// Encrypt second pass (AES/PBKDF2)
byte[] encrypted = null;
if (encType.HasFlag(EncryptionType.Password))
{
encrypted = AESEncrypt(firstPassEncrypted, userPassword);
}
else
{
return firstPassEncrypted;
}
return encrypted;
}