本文整理汇总了C#中System.Security.Cryptography.Aes.CreateDecryptor方法的典型用法代码示例。如果您正苦于以下问题:C# Aes.CreateDecryptor方法的具体用法?C# Aes.CreateDecryptor怎么用?C# Aes.CreateDecryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.Aes
的用法示例。
在下文中一共展示了Aes.CreateDecryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: 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);
}
示例3: 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);
}
示例4: 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);
}
}
示例5: Decrypt
/// <summary>
/// Decrypts data.
/// </summary>
/// <param name="aes">Aes object.</param>
/// <param name="encrypted">Encrypted data to be decrypted.</param>
/// <param name="key">Encryption key.</param>
/// <param name="iv">Initial vector.</param>
/// <returns>Decrypted data.</returns>
public static byte[] Decrypt(Aes aes, byte[] encrypted, byte[] key, byte[] iv)
{
byte[] buffer = new byte[encrypted.Length];
var decryptor = aes.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(encrypted))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
int length = cs.Read(buffer, 0, encrypted.Length);
return buffer.Take(length).ToArray();
}
}
}
示例6: Decrypt
/// <summary>
/// Encrypt a byte array by using the AES algorightm.
/// <para>The input is: [Magic Number (4 bytes)] [AES IV (16 bytes)] [AES(data) (xx bytes)] [HMAC(IV || AES(data)) (32 bytes)]</para>
/// <para>The output is: [data]</para>
/// </summary>
public static byte[] Decrypt(byte[] content, Aes aes)
{
byte[] headerBytes = new byte[4];
byte[] ivBytes = new byte[16];
byte[] dataBytes = new byte[content.Length - 4 - 16 - 32];
byte[] cypherBytes = new byte[ivBytes.Length + dataBytes.Length];
byte[] hmacBytes = new byte[32];
Array.Copy (content, 0, headerBytes, 0, headerBytes.Length);
Array.Copy (content, 4, ivBytes, 0, ivBytes.Length);
Array.Copy (content, 4 + 16, dataBytes, 0, dataBytes.Length);
Array.Copy (content, 4, cypherBytes, 0, cypherBytes.Length);
Array.Copy (content, content.Length - 32, hmacBytes, 0, hmacBytes.Length);
// Compute the HMAC of the cypher
using (HMACSHA256 hmac = new HMACSHA256 (DeriveKey (aes.Key))) {
byte[] newHmacBytes = hmac.ComputeHash (cypherBytes);
// Check for HMAC equality
for (int i = 0; i < newHmacBytes.Length; i++) {
if (newHmacBytes [i] != hmacBytes [i]) {
throw new CryptographicException ("Content has been tampered. HMAC don't match.");
}
}
}
using (MemoryStream outputStream = new MemoryStream()) {
// Report the IV
aes.IV = ivBytes;
// Write the AES decrypted content
using (ICryptoTransform transform = aes.CreateDecryptor ()) {
using (CryptoStream cryptoStream = new CryptoStream(outputStream, transform, CryptoStreamMode.Write)) {
cryptoStream.Write (dataBytes, 0, dataBytes.Length);
}
}
// Collect cypher result
outputStream.Flush ();
outputStream.Close ();
byte[] outputBytes = outputStream.ToArray ();
return outputBytes;
}
}
示例7: TestRequestVerifier
private bool TestRequestVerifier(Request r, Aes aes, string key)
{
var success = false;
var crypted = decode64(r.Verifier);
aes.Key = decode64(key);
aes.IV = decode64(r.Nonce);
using (var dec = aes.CreateDecryptor())
{
try {
var buf = dec.TransformFinalBlock(crypted, 0, crypted.Length);
var value = Encoding.UTF8.GetString(buf);
success = value == r.Nonce;
} catch (CryptographicException) { } // implicit failure
}
return success;
}
示例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: TestKnownEnc
static Boolean TestKnownEnc(Aes aes, Byte[] Key, Byte[] IV, Byte[] Cipher, Byte[] Plain)
{
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.CreateDecryptor(Key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(Plain,0,Plain.Length);
try
{
cs.FlushFinalBlock();
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
}
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;
}
示例10: AesCbcHmacSha2Decryptor
internal AesCbcHmacSha2Decryptor( 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.CreateDecryptor();
_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 );
}
示例11: 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;
}
示例12: getDecryptor
public static ICryptoTransform getDecryptor(Aes aes)
{
return aes.CreateDecryptor(aes.Key, aes.IV);
}
示例13: SegmentReader
public SegmentReader(Stream _bsstr, Aes rdale, int _fragsize)
{
_rdale = rdale as AesManaged;
basestream = _bsstr;
fragsize = _fragsize;
reader = rdale.CreateDecryptor();
}
示例14: ToMessage
public static Message ToMessage(this MemoryStream inStream, Aes aes = null)
{
if (inStream.Length == 0)
throw new Exceptions.CommunicationException("read stream blank!");
MemoryStream plainStream;
inStream.Seek(0, SeekOrigin.Begin);
//TODO: clean this up
string beforeGreaterThan = StreamUtilities.ReadUntilChar(inStream, '<', 5, true);
string beforeExclaimation = StreamUtilities.ReadUntilChar(inStream, '!', 5, true);
bool streamEncrypted;
if (beforeGreaterThan != null && beforeExclaimation == null)
streamEncrypted = false;
else if (beforeGreaterThan == null && beforeExclaimation != null)
streamEncrypted = true;
else if (beforeGreaterThan.Length < beforeExclaimation.Length)
streamEncrypted = false;
else
streamEncrypted = true;
if (streamEncrypted)
{
if (aes == null)
throw new Exceptions.CommunicationException("oops! Got an encrypted response, but don't know the key.");
string numBytesForIvString = StreamUtilities.ReadUntilChar(inStream, '!', 10, false);
//int bleh = StreamUtilities.PeekByte(inStream);
//string test2 = StreamUtilities.MemoryStreamToString(inStream);
int numBytesForIv;
try
{
numBytesForIv = Convert.ToInt32(numBytesForIvString);
}
catch
{
throw new Exceptions.CommunicationException("Error parsing the number of bytes for the Initialization Vector");
}
byte[] iv = new byte[numBytesForIv];
int numRead = inStream.Read(iv, 0, numBytesForIv);
if (numRead != numBytesForIv)
throw new Exceptions.CommunicationException("Error reading the initialization vector.");
aes.IV = iv;
try
{
plainStream = StreamUtilities.RunCryptoTransform(inStream, aes.CreateDecryptor(), false);
plainStream.Seek(0, SeekOrigin.Begin);
//string beforeGreaterThan2 = StreamUtilities.ReadUntilChar(inStream, 'x', 50, true);
//string test3 = StreamUtilities.MemoryStreamToString(plainStream);
//inStream.Seek(0, SeekOrigin.Begin);
}
catch (CryptographicException exception)
{
throw new Exceptions.CommunicationException("Error decrypting response.", exception);
}
}
else
{
plainStream = inStream;
}
Message result = new Message(plainStream);
if (result.controlValues.Count == 0 && result.Values.Count == 0 && result.Payload.Count == 0)
throw new Exceptions.CommunicationException("Blank response.");
if (!String.IsNullOrEmpty(result.CommunicationErrorMessage))
throw new Exceptions.CommunicationException(result.CommunicationErrorMessage);
return result;
}
示例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;
}