本文整理汇总了C#中EncryptionAlgorithm.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# EncryptionAlgorithm.ToString方法的具体用法?C# EncryptionAlgorithm.ToString怎么用?C# EncryptionAlgorithm.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EncryptionAlgorithm
的用法示例。
在下文中一共展示了EncryptionAlgorithm.ToString方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateAuthenticatedEncryptor_RoundTripsData_CngGcmImplementation
public void CreateAuthenticatedEncryptor_RoundTripsData_CngGcmImplementation(EncryptionAlgorithm encryptionAlgorithm)
{
// Parse test input
int keyLengthInBits = Int32.Parse(Regex.Match(encryptionAlgorithm.ToString(), @"^AES_(?<keyLength>\d{3})_GCM$").Groups["keyLength"].Value, CultureInfo.InvariantCulture);
// Arrange
var masterKey = Secret.Random(512 / 8);
var control = new GcmAuthenticatedEncryptor(
keyDerivationKey: masterKey,
symmetricAlgorithmHandle: CachedAlgorithmHandles.AES_GCM,
symmetricAlgorithmKeySizeInBytes: (uint)(keyLengthInBits / 8));
var test = CreateDescriptor(encryptionAlgorithm, ValidationAlgorithm.HMACSHA256 /* unused */, masterKey).CreateEncryptorInstance();
// Act & assert - data round trips properly from control to test
byte[] plaintext = new byte[] { 1, 2, 3, 4, 5 };
byte[] aad = new byte[] { 2, 4, 6, 8, 0 };
byte[] ciphertext = control.Encrypt(new ArraySegment<byte>(plaintext), new ArraySegment<byte>(aad));
byte[] roundTripPlaintext = test.Decrypt(new ArraySegment<byte>(ciphertext), new ArraySegment<byte>(aad));
Assert.Equal(plaintext, roundTripPlaintext);
}
示例2: _Zip64_Over65534Entries
void _Zip64_Over65534Entries(Zip64Option z64option,
EncryptionAlgorithm encryption,
Ionic.Zlib.CompressionLevel compression)
{
// Emitting a zip file with > 65534 entries requires the use of ZIP64 in
// the central directory.
int numTotalEntries = _rnd.Next(4616)+65534;
//int numTotalEntries = _rnd.Next(461)+6534;
//int numTotalEntries = _rnd.Next(46)+653;
string enc = encryption.ToString();
if (enc.StartsWith("WinZip")) enc = enc.Substring(6);
else if (enc.StartsWith("Pkzip")) enc = enc.Substring(0,5);
string zipFileToCreate = String.Format("Zip64.ZF_Over65534.{0}.{1}.{2}.zip",
z64option.ToString(),
enc,
compression.ToString());
_testTitle = String.Format("ZipFile #{0} 64({1}) E({2}), C({3})",
numTotalEntries,
z64option.ToString(),
enc,
compression.ToString());
_txrx = TestUtilities.StartProgressMonitor(zipFileToCreate,
_testTitle,
"starting up...");
_txrx.Send("pb 0 max 4"); // 3 stages: AddEntry, Save, Verify
_txrx.Send("pb 0 value 0");
string password = Path.GetRandomFileName();
string statusString = String.Format("status Encrypt:{0} Compress:{1}...",
enc,
compression.ToString());
int numSaved = 0;
var saveProgress = new EventHandler<SaveProgressEventArgs>( (sender, e) => {
switch (e.EventType)
{
case ZipProgressEventType.Saving_Started:
_txrx.Send("status saving...");
_txrx.Send("pb 1 max " + numTotalEntries);
numSaved= 0;
break;
case ZipProgressEventType.Saving_AfterWriteEntry:
numSaved++;
if ((numSaved % 128) == 0)
{
_txrx.Send("pb 1 value " + numSaved);
_txrx.Send(String.Format("status Saving entry {0}/{1} ({2:N0}%)",
numSaved, numTotalEntries,
numSaved / (0.01 * numTotalEntries)
));
}
break;
case ZipProgressEventType.Saving_Completed:
_txrx.Send("status Save completed");
_txrx.Send("pb 1 max 1");
_txrx.Send("pb 1 value 1");
break;
}
});
string contentFormatString =
"This is the content for entry #{0}.\r\n\r\n" +
"AAAAAAA BBBBBB AAAAA BBBBB AAAAA BBBBB AAAAA\r\n"+
"AAAAAAA BBBBBB AAAAA BBBBB AAAAA BBBBB AAAAA\r\n";
_txrx.Send(statusString);
int dirCount= 0;
using (var zip = new ZipFile())
{
_txrx.Send(String.Format("pb 1 max {0}", numTotalEntries));
_txrx.Send("pb 1 value 0");
zip.Password = password;
zip.Encryption = encryption;
zip.CompressionLevel = compression;
zip.SaveProgress += saveProgress;
zip.UseZip64WhenSaving = z64option;
// save space when saving the file:
zip.EmitTimesInWindowsFormatWhenSaving = false;
zip.EmitTimesInUnixFormatWhenSaving = false;
// add files:
for (int m=0; m<numTotalEntries; m++)
{
if (_rnd.Next(7)==0)
{
string entryName = String.Format("{0:D5}", m);
zip.AddDirectoryByName(entryName);
dirCount++;
}
else
{
string entryName = String.Format("{0:D5}.txt", m);
if (_rnd.Next(12)==0)
{
string contentBuffer = String.Format(contentFormatString, m);
//.........这里部分代码省略.........
示例3: _Internal_ZOS_Create
private void _Internal_ZOS_Create(string[] files,
EncryptionAlgorithm crypto,
bool seekable,
int cycle,
string format,
int fileOutputOption)
{
int BufferSize = 2048;
for (int k = 0; k < compLevels.Length; k++)
{
string zipFileToCreate = Path.Combine(TopLevelDir, String.Format(format, compLevels[k].ToString()));
string password = Path.GetRandomFileName();
TestContext.WriteLine("=================================");
TestContext.WriteLine("Creating {0}...", Path.GetFileName(zipFileToCreate));
TestContext.WriteLine("Encryption({0}) Compression({1}) pw({2})",
crypto.ToString(), compLevels[k].ToString(), password);
using (ZipOutputStream output = GetZipOutputStream(seekable, fileOutputOption, zipFileToCreate))
{
if (crypto != EncryptionAlgorithm.None)
{
output.Password = password;
output.Encryption = crypto;
}
output.CompressionLevel = compLevels[k];
byte[] buffer = new byte[BufferSize];
int n;
foreach (var file in files)
{
TestContext.WriteLine("file: {0}", file);
output.PutNextEntry(file);
using (var input = File.OpenRead(file))
{
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, n);
}
}
}
}
BasicVerifyZip(zipFileToCreate, password);
Assert.AreEqual<int>(files.Length, TestUtilities.CountEntries(zipFileToCreate),
"Trial ({0},{1}): The zip file created has the wrong number of entries.", cycle, k);
}
}
示例4: E
private void _ZOS_z64Over65534Entries
(Zip64Option z64option,
EncryptionAlgorithm encryption,
Ionic.Zlib.CompressionLevel compression)
{
TestContext.WriteLine("_ZOS_z64Over65534Entries hello: {0}",
DateTime.Now.ToString("G"));
int fileCount = _rnd.Next(14616) + 65536;
//int fileCount = _rnd.Next(146) + 5536;
TestContext.WriteLine("entries: {0}", fileCount);
var txrxLabel =
String.Format("ZOS #{0} 64({3}) E({1}) C({2})",
fileCount,
encryption.ToString(),
compression.ToString(),
z64option.ToString());
TestContext.WriteLine("label: {0}", txrxLabel);
string zipFileToCreate =
String.Format("ZOS.Zip64.over65534.{0}.{1}.{2}.zip",
z64option.ToString(), encryption.ToString(),
compression.ToString());
TestContext.WriteLine("zipFileToCreate: {0}", zipFileToCreate);
_txrx = TestUtilities.StartProgressMonitor(zipFileToCreate,
txrxLabel, "starting up...");
TestContext.WriteLine("generating {0} entries ", fileCount);
_txrx.Send("pb 0 max 3"); // 2 stages: Write, Count, Verify
_txrx.Send("pb 0 value 0");
string password = Path.GetRandomFileName();
string statusString = String.Format("status Encryption:{0} Compression:{1}",
encryption.ToString(),
compression.ToString());
_txrx.Send(statusString);
int dirCount = 0;
using (FileStream fs = File.Create(zipFileToCreate))
{
using (var output = new ZipOutputStream(fs))
{
_txrx.Send("test " + txrxLabel);
System.Threading.Thread.Sleep(400);
_txrx.Send("pb 1 max " + fileCount);
_txrx.Send("pb 1 value 0");
output.Password = password;
output.Encryption = encryption;
output.CompressionLevel = compression;
output.EnableZip64 = z64option;
for (int k = 0; k < fileCount; k++)
{
if (_rnd.Next(7) == 0)
{
// make it a directory
string entryName = String.Format("{0:D4}/", k);
output.PutNextEntry(entryName);
dirCount++;
}
else
{
string entryName = String.Format("{0:D4}.txt", k);
output.PutNextEntry(entryName);
// only a few entries are non-empty
if (_rnd.Next(18) == 0)
{
var block = TestUtilities.GenerateRandomAsciiString();
string content = String.Format("This is the content for entry #{0}.\n", k);
int n = _rnd.Next(4) + 1;
for (int j=0; j < n; j++)
content+= block;
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content);
output.Write(buffer, 0, buffer.Length);
}
}
if (k % 1024 == 0)
_txrx.Send(String.Format("status saving ({0}/{1}) {2:N0}%",
k, fileCount,
((double)k) / (0.01 * fileCount)));
else if (k % 256 == 0)
_txrx.Send("pb 1 value " + k);
}
}
}
_txrx.Send("pb 1 max 1");
_txrx.Send("pb 1 value 1");
_txrx.Send("pb 0 step");
System.Threading.Thread.Sleep(400);
TestContext.WriteLine("Counting entries ... " + DateTime.Now.ToString("G"));
_txrx.Send("status Counting entries...");
//.........这里部分代码省略.........
示例5: _Internal_AddEntry_WriteDelegate
private void _Internal_AddEntry_WriteDelegate(string[] files,
EncryptionAlgorithm crypto,
bool seekable,
int cycle,
string format,
int ignored)
{
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
int n;
for (int k = 0; k < compLevels.Length; k++)
{
string zipFileToCreate = Path.Combine(TopLevelDir, String.Format(format, compLevels[k].ToString()));
string password = TestUtilities.GenerateRandomPassword();
using (var zip = new ZipFile())
{
TestContext.WriteLine("=================================");
TestContext.WriteLine("Creating {0}...", Path.GetFileName(zipFileToCreate));
TestContext.WriteLine("Encryption({0}) Compression({1}) pw({2})",
crypto.ToString(), compLevels[k].ToString(), password);
zip.Password = password;
zip.Encryption = crypto;
zip.CompressionLevel = compLevels[k];
foreach (var file in files)
{
zip.AddEntry(file, (name, output) =>
{
using (var input = File.OpenRead(name))
{
while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, n);
}
}
});
}
if (!seekable)
{
// conditionally use a non-seekable output stream
using (var raw = File.Create(zipFileToCreate))
{
using (var ns = new Ionic.Zip.Tests.NonSeekableOutputStream(raw))
{
zip.Save(ns);
}
}
}
else
zip.Save(zipFileToCreate);
}
BasicVerifyZip(Path.GetFileName(zipFileToCreate), password);
Assert.AreEqual<int>(files.Length, TestUtilities.CountEntries(zipFileToCreate),
"Trial ({0},{1}): The zip file created has the wrong number of entries.", cycle, k);
}
}
示例6: CreateAuthenticatedEncryptor_RoundTripsData_ManagedImplementation
public void CreateAuthenticatedEncryptor_RoundTripsData_ManagedImplementation(EncryptionAlgorithm encryptionAlgorithm, ValidationAlgorithm validationAlgorithm)
{
// Parse test input
int keyLengthInBits = Int32.Parse(Regex.Match(encryptionAlgorithm.ToString(), @"^AES_(?<keyLength>\d{3})_CBC$").Groups["keyLength"].Value, CultureInfo.InvariantCulture);
// Arrange
var masterKey = Secret.Random(512 / 8);
var control = new ManagedAuthenticatedEncryptor(
keyDerivationKey: masterKey,
symmetricAlgorithmFactory: () => new AesCryptoServiceProvider(),
symmetricAlgorithmKeySizeInBytes: keyLengthInBits / 8,
validationAlgorithmFactory: () => KeyedHashAlgorithm.Create(validationAlgorithm.ToString()));
var test = CreateDescriptor(encryptionAlgorithm, validationAlgorithm, masterKey).CreateEncryptorInstance();
// Act & assert - data round trips properly from control to test
byte[] plaintext = new byte[] { 1, 2, 3, 4, 5 };
byte[] aad = new byte[] { 2, 4, 6, 8, 0 };
byte[] ciphertext = control.Encrypt(new ArraySegment<byte>(plaintext), new ArraySegment<byte>(aad));
byte[] roundTripPlaintext = test.Decrypt(new ArraySegment<byte>(ciphertext), new ArraySegment<byte>(aad));
Assert.Equal(plaintext, roundTripPlaintext);
}
示例7: GetCryptoServiceProvider
/// <summary>
/// Gets an instance of the SymmetricAlgorithm class for the specified algorithm.
/// </summary>
/// <param name="algorithm">The EncryptionAlgorithm value specifying the type of crypto service provider to create.</param>
/// <returns>A SymmetricAlgorithm instance for the specified algorithm preset with the specified Key and IV.</returns>
private static SymmetricAlgorithm GetCryptoServiceProvider(EncryptionAlgorithm algorithm)
{
SymmetricAlgorithm crypto = null;
switch (algorithm)
{
case EncryptionAlgorithm.Des:
crypto = new DESCryptoServiceProvider();
break;
case EncryptionAlgorithm.Rc2:
crypto = new RC2CryptoServiceProvider();
break;
case EncryptionAlgorithm.Rijndael:
crypto = new RijndaelManaged();
break;
case EncryptionAlgorithm.TripleDes:
crypto = new TripleDESCryptoServiceProvider();
break;
default:
throw new NotSupportedException(String.Concat("The EncryptionAlgorithm value specified '",
algorithm.ToString(), "' is not supported."));
}
return crypto;
}