本文整理汇总了C#中System.Security.SecureString.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# SecureString.ToByteArray方法的具体用法?C# SecureString.ToByteArray怎么用?C# SecureString.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.SecureString
的用法示例。
在下文中一共展示了SecureString.ToByteArray方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
public static byte[] Decrypt(byte[] encryptionPayload, SecureString passPhrase)
{
var passwordBytes = passPhrase.ToByteArray();
var output = Decrypt(encryptionPayload, passwordBytes);
passwordBytes.ClearByteArray();
return output;
}
示例2: SecureStringExtensions_ToByteArray_EmptyInput
public void SecureStringExtensions_ToByteArray_EmptyInput()
{
SecureString input = new SecureString();
byte[] output = input.ToByteArray();
byte[] expected = new byte[0];
CollectionAssert.AreEqual(expected, output);
}
示例3: Encrypt
public static string Encrypt(SecureString input)
{
Validator.AssertNotNull(input, "input");
if(input.Length == 0)
{
return String.Empty;
}
byte[] bytes = input.ToByteArray();
try
{
// Encrypt
var aes = new AesCryptoServiceProvider();
// Set nulled IV
aes.IV = new byte[aes.IV.Length];
aes.Key = AesKey;
ICryptoTransform aesEncryptor = aes.CreateEncryptor();
// Decrypt
byte[] encryptedBytes = aesEncryptor.TransformFinalBlock(bytes, 0, bytes.Length);
// Convert to Base64
string base64 = Convert.ToBase64String(encryptedBytes);
// Remove Base64 padding
string result = RemoveBase64Padding(base64);
return result;
}
finally
{
bytes.ZeroFill();
}
}
示例4: Encrypt_Private_v1
private static byte[] Encrypt_Private_v1(byte[] unencryptedBytes, SecureString passPhrase, int iterations)
{
var passPhraseAsBytes = passPhrase.ToByteArray();
var payload = Encrypt_Private_v1(unencryptedBytes, passPhraseAsBytes, iterations);
passPhraseAsBytes.ClearByteArray(); // modify the byte array
return payload;
}
示例5: EncryptToBase64StringAsync
//[Obsolete]
//public static string EncryptToBase64String(SecureString passwordToProtect, string keyId, RSACryptoServiceProvider publicKey)
//{
// return EncryptToBase64String(passwordToProtect, keyId, publicKey, null, null);
//}
///// <summary>
///// This may be used to secure a password for symmetric encryption.
///// </summary>
///// <param name="passwordToProtect"></param>
///// <param name="publicKey"></param>
///// <returns></returns>
//[Obsolete("Use the IPublicKey infrastructure")]
//public static string EncryptToBase64String(SecureString passwordToProtect, string key1Id, RSACryptoServiceProvider publicKey1, string key2Id, RSACryptoServiceProvider publicKey2)
//{
// // Use a 4-byte array to fill it with random bytes and convert it then
// // to an integer value.
// byte[] plainBytes;
// //byte[] encryptedBytes = null;
// plainBytes = passwordToProtect.ToByteArray();
// var asymEnc = new AsymmetricEncryptor(AsymmetricStrategyOption.Aes256_1000);
// var asymObj = asymEnc.EncryptObject(plainBytes, key1Id, publicKey1, key2Id, publicKey2);
// var json = Serializer.SerializeToJson(asymObj);
// var bytes = Encoding.UTF8.GetBytes(json);
// return Convert.ToBase64String(bytes);
//}
/// <summary>
/// This may be used to secure a password for symmetric encryption.
/// </summary>
/// <param name="passwordToProtect"></param>
/// <param name="publicKey"></param>
/// <returns></returns>
public static async Task<string> EncryptToBase64StringAsync(SecureString passwordToProtect, IPublicKey publicKey1, IPublicKey publicKey2)
{
// Use a 4-byte array to fill it with random bytes and convert it then
// to an integer value.
byte[] plainBytes;
//byte[] encryptedBytes = null;
plainBytes = passwordToProtect.ToByteArray();
var asymEnc = new AsymmetricEncryptor(AsymmetricStrategyOption.Aes256_1000);
var asymObj = await asymEnc.EncryptObject_PrivateAsync(plainBytes, publicKey1, publicKey2);
var json = Serializer.SerializeToJson(asymObj);
var bytes = Encoding.UTF8.GetBytes(json);
return Convert.ToBase64String(bytes);
}