本文整理汇总了C#中System.Security.Cryptography.Aes.CreateEncryptor方法的典型用法代码示例。如果您正苦于以下问题:C# Aes.CreateEncryptor方法的具体用法?C# Aes.CreateEncryptor怎么用?C# Aes.CreateEncryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.Aes
的用法示例。
在下文中一共展示了Aes.CreateEncryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestKnownEnc
static Boolean TestKnownEnc(Aes aes, Byte[] Key, Byte[] IV, Byte[] Plain, Byte[] Cipher)
{
Byte[] CipherCalculated;
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(Plain);
Console.WriteLine("With the following Key:");
PrintByteArray(Key);
Console.WriteLine("and IV:");
PrintByteArray(IV);
Console.WriteLine("Expecting this ciphertext:");
PrintByteArray(Cipher);
ICryptoTransform sse = aes.CreateEncryptor(Key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(Plain,0,Plain.Length);
cs.FlushFinalBlock();
CipherCalculated = ms.ToArray();
cs.Close();
Console.WriteLine("Computed this cyphertext:");
PrintByteArray(CipherCalculated);
if (!Compare(Cipher, CipherCalculated)) {
Console.WriteLine("ERROR: result is different from the expected");
return false;
}
Console.WriteLine("OK");
return true;
}
示例2: init
public override void init(int mode, byte[] key, byte[] iv)
{
if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) throw new ArgumentOutOfRangeException();
ms = new PipedMemoryStream();
aesm = AesManaged.Create();
aesm.BlockSize = blockSize * 8;
aesm.Padding = PaddingMode.None;
ICryptoTransform ict;
if (key.Length > blockSize)
{
byte[] tmp = new byte[blockSize];
Array.Copy(key, 0, tmp, 0, tmp.Length);
key = tmp;
}
if (iv.Length > ivSize)
{
byte[] tmp = new byte[ivSize];
Array.Copy(iv, 0, tmp, 0, tmp.Length);
iv = tmp;
}
if (mode == ENCRYPT_MODE)
{
ict = aesm.CreateEncryptor(key, iv);
}
else
{
ict = aesm.CreateDecryptor(key, iv);
}
cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
}
示例3: DBCrypto
static DBCrypto()
{
aesAlg = Aes.Create();
aesAlg.Key = AES_CBC_KEY;
aesAlg.IV = AES_CBC_IV;
aesAlg.Padding = PaddingMode.PKCS7;
decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
}
示例4: SimpleAes
public SimpleAes(byte[] key)
{
aes = Aes.Create();
aes.GenerateIV();
aes.Key = key;
encryptor = aes.CreateEncryptor(key, aes.IV);
decryptor = aes.CreateDecryptor(key, aes.IV);
}
示例5: AesCtrCryptoTransform
/// <summary>ctor</summary>
public AesCtrCryptoTransform(byte[] key, ArraySegment<byte> counterBufferSegment, Func<Aes> aesFactory = null)
{
if (counterBufferSegment.Count != AesConstants.AES_BLOCK_SIZE)
throw new ArgumentException("counterBufferSegment.Count must be " + AesConstants.STR_AES_BLOCK_SIZE + ".");
this.aes = aesFactory == null ? AesFactories.Aes() : aesFactory();
this.aes.Mode = CipherMode.ECB;
this.aes.Padding = PaddingMode.None;
Utils.BlockCopy(counterBufferSegment.Array, counterBufferSegment.Offset, counterBuffer, 0, AesConstants.AES_BLOCK_SIZE);
this.cryptoTransform = aes.CreateEncryptor(rgbKey: key, rgbIV: null);
}// ctor
示例6: AesCtrCryptoTransform
/// <summary>
/// ctor
/// </summary>
/// <param name="key"></param>
/// <param name="counterBufferSegment"></param>
/// <param name="aesFactory"></param>
/// <exception cref="ArgumentException">
/// <paramref name="counterBufferSegment"/> needs to have the same length as <see cref="AesConstants.STR_AES_BLOCK_SIZE"/>.
/// </exception>
public AesCtrCryptoTransform(byte[] key, ArraySegment<byte> counterBufferSegment, Func<Aes> aesFactory = null)
{
if (counterBufferSegment.Count != AesConstants.AES_BLOCK_SIZE)
throw new ArgumentException($"{nameof(counterBufferSegment)}.Count must be {AesConstants.STR_AES_BLOCK_SIZE}.", nameof(counterBufferSegment));
_aes = aesFactory?.Invoke() ?? CipherFactory.Aes();
_aes.Mode = CipherMode.ECB;
_aes.Padding = PaddingMode.None;
Buffer.BlockCopy(counterBufferSegment.Array, counterBufferSegment.Offset, _counterBuffer.Value, 0, AesConstants.AES_BLOCK_SIZE);
_cryptoTransform = _aes.CreateEncryptor(rgbKey: key, rgbIV: null);
}
示例7: CryptoTransform
private string CryptoTransform(string input, bool base64in, bool base64out, Aes cipher, CMode mode)
{
byte[] bytes;
if (base64in)
bytes = decode64(input);
else
bytes = Encoding.UTF8.GetBytes(input);
using (var c = mode == CMode.ENCRYPT ? cipher.CreateEncryptor() : cipher.CreateDecryptor()) {
var buf = c.TransformFinalBlock(bytes, 0, bytes.Length);
return base64out ? encode64(buf) : Encoding.UTF8.GetString(buf);
}
}
示例8: Test
static Boolean Test(Aes aes, CipherMode md)
{
Byte[] PlainText = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
Byte[] Key = {1, 1, 1, 1, 1, 1, 1, 1,2,2,2,2,2,2,2,2};
Byte[] IV = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115};
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(PlainText);
aes.Mode = md;
Console.WriteLine("AES default key size = " + aes.KeySize);
ICryptoTransform sse = aes.CreateEncryptor(Key, IV);
Console.WriteLine("SSE mode = " + aes.Mode);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(PlainText,0,PlainText.Length);
cs.FlushFinalBlock();
byte[] CipherText = ms.ToArray();
cs.Close();
Console.WriteLine("Cyphertext:");
PrintByteArray(CipherText);
Console.WriteLine("Decrypting...");
ICryptoTransform ssd = aes.CreateDecryptor(Key, IV);
Console.WriteLine("SSD mode = " + aes.Mode);
cs = new CryptoStream(new MemoryStream(CipherText), ssd, CryptoStreamMode.Read);
byte[] NewPlainText = new byte[PlainText.Length];
cs.Read(NewPlainText,0,PlainText.Length);
PrintByteArray(NewPlainText);
if (!Compare(PlainText, NewPlainText)) {
Console.WriteLine("ERROR: roundtrip failed");
return false;
}
return true;
}
示例9: AesCbcHmacSha2Encryptor
internal AesCbcHmacSha2Encryptor( string name, byte[] key, byte[] iv, byte[] associatedData )
{
// Split the key to get the AES key, the HMAC key and the HMAC object
byte[] aesKey;
GetAlgorithmParameters( name, key, out aesKey, out _hmac_key, out _hmac );
// Create the AES provider
_aes = Aes.Create();
_aes.Mode = CipherMode.CBC;
_aes.Padding = PaddingMode.PKCS7;
_aes.KeySize = aesKey.Length * 8;
_aes.Key = aesKey;
_aes.IV = iv;
_inner = _aes.CreateEncryptor();
_associated_data_length = ConvertToBigEndian( associatedData.Length * 8 );
// Prime the hash.
_hmac.TransformBlock( associatedData, 0, associatedData.Length, associatedData, 0 );
_hmac.TransformBlock( iv, 0, iv.Length, iv, 0 );
}
示例10: GetPaddingBytes
public static byte[] GetPaddingBytes(Aes aes, PaddingMode mode, int offset)
{
byte[] originalData = new byte[BlockSizeBytes + offset];
new Random().NextBytes(originalData);
aes.Padding = mode;
byte[] encryptedData = aes.CreateEncryptor().TransformFinalBlock(originalData, 0, originalData.Length);
aes.Padding = PaddingMode.None;
byte[] decryptedData = aes.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
byte[] paddingBytes = new byte[decryptedData.Length - BlockSizeBytes - offset];
Array.Copy(decryptedData, BlockSizeBytes + offset, paddingBytes, 0, decryptedData.Length - BlockSizeBytes - offset);
return paddingBytes;
}
示例11: TestTransform
public static bool TestTransform(Aes aes)
{
CipherMode[] modes;
// AES allows CBC, ECB, and CFB modes. Due to bug in RijndaelManaged, AesManaged does not
// allow CFB.
//
if (aes is AesManaged)
modes = new CipherMode[] {CipherMode.CBC, CipherMode.ECB};
else
modes = new CipherMode[] {CipherMode.CBC, CipherMode.ECB, CipherMode.CFB};
ICryptoTransform encryptor;
ICryptoTransform decryptor;
foreach (CipherMode mode in modes)
{
aes.Mode = mode;
encryptor = aes.CreateEncryptor();
decryptor = aes.CreateDecryptor();
if (encryptor.CanReuseTransform != true)
{
Console.WriteLine("Error - encryptor CanReuseTransform not true");
return false;
}
if (decryptor.CanReuseTransform != true)
{
Console.WriteLine("Error - decryptor CanReuseTransform not true");
return false;
}
if (encryptor.CanTransformMultipleBlocks != true)
{
Console.WriteLine("Error - encryptor CanTransformMultipleBlocks not true");
return false;
}
if (decryptor.CanTransformMultipleBlocks != true)
{
Console.WriteLine("Error - decryptor CanTransformMultipleBlocks not true");
return false;
}
if (encryptor.InputBlockSize != 16)
{
Console.WriteLine("Error - encryptor InputBlockSize not 16");
return false;
}
if (decryptor.InputBlockSize != 16)
{
Console.WriteLine("Error - decryptor InputBlockSize not 16");
return false;
}
if (encryptor.OutputBlockSize != 16)
{
Console.WriteLine("Error - encryptor OutputBlockSize not 16");
return false;
}
if (decryptor.OutputBlockSize != 16)
{
Console.WriteLine("Error - decryptor OutputBlockSize not 16");
return false;
}
}
return true;
}
示例12: Encrypt
/// <summary>
/// Encrypts data.
/// </summary>
/// <param name="aes">Aes object.</param>
/// <param name="content">Content to be encrypted.</param>
/// <param name="key">Encryption key.</param>
/// <param name="iv">Initial vector.</param>
/// <returns>Encrypted data.</returns>
public static byte[] Encrypt(Aes aes, byte[] content, byte[] key, byte[] iv)
{
var encryptor = aes.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cs.Write(content, 0, content.Length);
}
return ms.ToArray();
}
}
示例13: Encrypt
/// <summary>
/// Encrypt a byte array by using the AES algorightm.
/// <para>The input is: [data]</para>
/// <para>The output is: [Magic Number (4 bytes)] [AES IV (16 bytes)] [AES(data) (xx bytes)] [HMAC(IV || AES(data)) (32 bytes)]</para>
/// </summary>
public static byte[] Encrypt(byte[] content, Aes aes)
{
using (MemoryStream outputStream = new MemoryStream()) {
// Write the magic number
outputStream.Write (MAGIC_NUMBER, 0, 4);
using (MemoryStream cypherStream = new MemoryStream()) {
// Write the IV
cypherStream.Write (aes.IV, 0, 16);
// Write the AES encrypted content
using (ICryptoTransform transform = aes.CreateEncryptor ()) {
using (CryptoStream cryptoStream = new CryptoStream(cypherStream, transform, CryptoStreamMode.Write)) {
cryptoStream.Write (content, 0, content.Length);
cryptoStream.Flush ();
cryptoStream.Close ();
}
}
// Collect cypher result
cypherStream.Flush ();
cypherStream.Close ();
byte[] cypherBytes = cypherStream.ToArray ();
// Write the cypher
outputStream.Write (cypherBytes, 0, cypherBytes.Length);
// Compute the HMAC of the cypher
using (HMACSHA256 hmac = new HMACSHA256 (DeriveKey (aes.Key))) {
byte[] hmacBytes = hmac.ComputeHash (cypherBytes);
// Write the HMAC
outputStream.Write (hmacBytes, 0, hmacBytes.Length);
}
}
outputStream.Flush ();
outputStream.Close ();
byte[] outputBytes = outputStream.ToArray ();
return outputBytes;
}
}
示例14: WriteMessageStream
public static void WriteMessageStream(MemoryStream outStream, Message outMessage, string sessionKey, Aes aes, bool encrypt)
{
outMessage.ReplaceControlValue(Message.TimestampControlValueName, DateTime.Now.ToUniversalTime().ToString());
outMessage.ReplaceControlValue(Message.SessionKeyControlValueName, sessionKey);
if (encrypt)
{
aes.GenerateIV();
StreamUtilities.WriteStringToMemoryStream(outStream, aes.IV.Length + "!");
outStream.Write(aes.IV, 0, aes.IV.Length);
StreamUtilities.RunCryptoTransform(outMessage.Serialize(), aes.CreateEncryptor(), outStream, true);
}
else
{
outMessage.Serialize(outStream);
}
}
示例15: TestTransform
public static bool TestTransform(Aes aes)
{
ICryptoTransform encryptor;
ICryptoTransform decryptor;
encryptor = aes.CreateEncryptor();
decryptor = aes.CreateDecryptor();
if (encryptor.CanReuseTransform != true)
{
Console.WriteLine("Error - encryptor CanReuseTransform not true");
return false;
}
if (decryptor.CanReuseTransform != true)
{
Console.WriteLine("Error - decryptor CanReuseTransform not true");
return false;
}
if (encryptor.CanTransformMultipleBlocks != true)
{
Console.WriteLine("Error - encryptor CanTransformMultipleBlocks not true");
return false;
}
if (decryptor.CanTransformMultipleBlocks != true)
{
Console.WriteLine("Error - decryptor CanTransformMultipleBlocks not true");
return false;
}
if (encryptor.InputBlockSize != 16)
{
Console.WriteLine("Error - encryptor InputBlockSize not 16");
return false;
}
if (decryptor.InputBlockSize != 16)
{
Console.WriteLine("Error - decryptor InputBlockSize not 16");
return false;
}
if (encryptor.OutputBlockSize != 16)
{
Console.WriteLine("Error - encryptor OutputBlockSize not 16");
return false;
}
if (decryptor.OutputBlockSize != 16)
{
Console.WriteLine("Error - decryptor OutputBlockSize not 16");
return false;
}
return true;
}