本文整理汇总了C#中System.Security.Cryptography.AesCryptoServiceProvider.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# AesCryptoServiceProvider.Clear方法的具体用法?C# AesCryptoServiceProvider.Clear怎么用?C# AesCryptoServiceProvider.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.AesCryptoServiceProvider
的用法示例。
在下文中一共展示了AesCryptoServiceProvider.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decription
public string Decription(byte[] encryptedData, byte[] alicePubKeyBlob)
{
byte[] rawData = null;
var aes = new AesCryptoServiceProvider();
int nBytes = aes.BlockSize >> 3;
byte[] iv = new byte[nBytes];
for (int i = 0; i < iv.Length; i++)
iv[i] = encryptedData[i];
using (var bobAlgorithm = new ECDiffieHellmanCng(privKey))
using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob,
CngKeyBlobFormat.EccPublicBlob))
{
byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
aes.Key = symmKey;
aes.IV = iv;
using (ICryptoTransform decryptor = aes.CreateDecryptor())
using (MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);
cs.Close();
rawData = ms.ToArray();
}
aes.Clear();
}
return Encoding.UTF8.GetString(rawData);
}
示例2: AliceSendsData
private async static Task<byte[]> AliceSendsData(string message)
{
Console.WriteLine("Alice send message {0}", message);
byte[] rawData = Encoding.UTF8.GetBytes(message);
byte[] encryptedData = null;
using(var aliceAlgo = new ECDiffieHellmanCng(aliceKey))
{
using(CngKey bobPubKey = CngKey.Import(bobPubKeyBlob, CngKeyBlobFormat.GenericPublicBlob))
{
byte[] symmKey = aliceAlgo.DeriveKeyMaterial(bobPubKey);
Console.WriteLine("Alice create this symm key with Bobs public key information : {0}", Convert.ToBase64String(symmKey));
using(var aes = new AesCryptoServiceProvider())
{
aes.Key = symmKey;
aes.GenerateIV();
using(ICryptoTransform encryptor = aes.CreateEncryptor())
{
using(MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, encryptor,CryptoStreamMode.Write);
await ms.WriteAsync(aes.IV, 0, aes.IV.Length);
cs.Write(rawData, 0, rawData.Length);
cs.Close();
encryptedData = ms.ToArray();
}
}
aes.Clear();
}
}
}
Console.WriteLine("Alice message is encrypted : {0}", Convert.ToBase64String(encryptedData));
return encryptedData;
}
示例3: AliceSendData
private static byte[] AliceSendData(string msg)
{
Console.WriteLine(string.Format("Alice Send Msg: {0}", msg));
byte[] rawdata = Encoding.UTF8.GetBytes(msg);
byte[] encryptedData = null;
using (var aliceAlgorithm = new ECDiffieHellmanCng(aliceKey))
using (CngKey bobPubKey = CngKey.Import(bobPubKeyBlob, CngKeyBlobFormat.EccPublicBlob))
{
byte[] symmkey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
Console.WriteLine(string.Format("Alice Create this symmtric key with {0}", Convert.ToBase64String(symmkey)));
var aes = new AesCryptoServiceProvider();
aes.Key = symmkey;
aes.GenerateIV();
using (ICryptoTransform encryptor = aes.CreateEncryptor())
using (MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
ms.Write(aes.IV, 0, aes.IV.Length);
cs.Write(rawdata, 0, rawdata.Length);
cs.Close();
encryptedData = ms.ToArray();
}
aes.Clear();
}
Console.WriteLine(Convert.ToBase64String(encryptedData));
return encryptedData;
}
示例4: Request
public static string Request(string requestParams, string devKey, string devIV, string devID) {
// Create an unencrypted request as an array of bytes
byte[] request = UTF8Encoding.UTF8.GetBytes(requestParams);
byte[] key = UTF8Encoding.UTF8.GetBytes(devKey);
byte[] iv = UTF8Encoding.UTF8.GetBytes(devIV);
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = key;
aes.IV = iv;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
// Get the transformer from the AES Encryptor
ICryptoTransform cTransform = aes.CreateEncryptor();
// Use the transformer to encrypt our request
byte[] result = cTransform.TransformFinalBlock(request, 0, request.Length);
aes.Clear();
// Encode to base64
string encryptedRequest = Convert.ToBase64String(result, 0, result.Length);
// Send request to API
string requestUri = "http://api.blackoutrugby.com/?d=" + devID + "&er=" + encryptedRequest;
string xmlResponse = getWebResponse(requestUri);
return XmlToJson(xmlResponse);
}
示例5: Decrypt
/// <summary>
/// 解密字节数组
/// </summary>
/// <param name="inputData">要解密的字节数据</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static byte[] Decrypt(byte[] inputData, string password)
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider
{
Key = GetKeyArray(password),
Mode = cipherMode,
Padding = paddingMode
};
var transform = aes.CreateDecryptor();
byte[] data = null;
try
{
data = transform.TransformFinalBlock(inputData, 0, inputData.Length);
}
catch
{
return null;
}
aes.Clear();
return data;
}
示例6: Decrypt
/// <summary>
/// 解密字节数组
/// </summary>
/// <param name="inputData">要解密的字节数据</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static byte[] Decrypt(byte[] inputData, string password)
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = GetKeyArray(password);
aes.Mode = AesCipherMode;
aes.Padding = AesPaddingMode;
ICryptoTransform transform = aes.CreateDecryptor();
byte[] data = transform.TransformFinalBlock(inputData, 0, inputData.Length);
aes.Clear();
return data;
}
示例7: Encrypt
/// <summary>
/// 加密一个字符串
/// </summary>
/// <param name="plainText">要加密的字符串</param>
/// <param name="key">用于加密的密钥</param>
/// <returns>加密后的BASE64字符串</returns>
public override string Encrypt(string plainText, string key)
{
var inputData = System.Text.UTF8Encoding.UTF8.GetBytes(plainText);
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = System.Text.UTF8Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform transform = aes.CreateEncryptor();
byte[] data = transform.TransformFinalBlock(inputData, 0, inputData.Length);
aes.Clear();
return Convert.ToBase64String(data);
}
示例8: TestSymmetricKeyProvider
public TestSymmetricKeyProvider()
{
byte[] key;
byte[] iv;
using (var provider = new AesCryptoServiceProvider())
{
provider.GenerateIV();
provider.GenerateKey();
key = provider.Key;
iv = provider.IV;
provider.Clear();
}
_key = new TestSymmetricKey(key, iv);
}
示例9: Decrypt
/// <summary>
/// 解密一段BASE64字符串
/// </summary>
/// <param name="base64String">需要解密的字符串</param>
/// <param name="key">解密用的密钥</param>
/// <returns>解密后的明文字符串</returns>
public override String Decrypt(string base64String, string key)
{
var inputData = Convert.FromBase64String(base64String);
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = System.Text.UTF8Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform transform = aes.CreateDecryptor();
byte[] data = null;
try
{
data = transform.TransformFinalBlock(inputData, 0, inputData.Length);
}
catch
{
return null;
}
aes.Clear();
return System.Text.UTF8Encoding.UTF8.GetString(data);
}
示例10: DecryptAES
public static string DecryptAES(string hash, string key, bool hashKey = true)
{
if (hash == null || key == null)
return null;
var keyArray = HexStringToByteArray(hashKey ? HashMD5(key) : key);
var toEncryptArray = HexStringToByteArray(hash);
var aes = new AesCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var cTransform = aes.CreateDecryptor();
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
aes.Clear();
return Encoding.UTF8.GetString(resultArray);
}
示例11: EncryptAES
public static string EncryptAES(string phrase, string key, bool hashKey = true)
{
if (phrase == null || key == null)
return null;
var keyArray = HexStringToByteArray(hashKey ? HashMD5(key) : key);
var toEncryptArray = Encoding.UTF8.GetBytes(phrase);
byte[] result;
using (var aes = new AesCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
})
{
var cTransform = aes.CreateEncryptor();
result = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
aes.Clear();
}
return ByteArrayToHexString(result);
}
示例12: SymmetricDecrypt
public static string SymmetricDecrypt(this string cipherText, string key, SymmetricAlgorithm algorithm)
{
if (string.IsNullOrWhiteSpace(cipherText))
throw new ArgumentNullException("cipherText", "Cannot decrypt an empty cipher text.");
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException("key", "Cannot decrypt with an empty key.");
byte[] keyBuffer = Convert.FromBase64String(key.Hash(HashAlgorithm.MD5));
byte[] cipherTextBuffer = Convert.FromBase64String(cipherText);
System.Security.Cryptography.SymmetricAlgorithm symmetricAlgorithm;
switch (algorithm)
{
// case SymmetricAlgorithmEnum.DES:
// symmetricAlgorithm = new DESCryptoServiceProvider();
// break;
case SymmetricAlgorithm.RC2:
symmetricAlgorithm = new RC2CryptoServiceProvider();
break;
case SymmetricAlgorithm.TripleDES:
symmetricAlgorithm = new TripleDESCryptoServiceProvider();
break;
case SymmetricAlgorithm.Aes:
default:
symmetricAlgorithm = new AesCryptoServiceProvider();
break;
}
symmetricAlgorithm.Key = keyBuffer;
symmetricAlgorithm.Mode = CipherMode.ECB;
// The legacy code did not indicate padding - not sure if this will effect the ouput much but we probably shouldn't change it
// symmetricAlgorithm.Padding = PaddingMode.PKCS7;
var decryptor = symmetricAlgorithm.CreateDecryptor();
byte[] plainTextBuffer = decryptor.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length);
symmetricAlgorithm.Clear();
return Encoding.Default.GetString(plainTextBuffer);
}
示例13: AliceSendsDataAsync
private async Task<byte[]> AliceSendsDataAsync(string message)
{
WriteLine($"Alice sends message: {message}");
byte[] rawData = Encoding.UTF8.GetBytes(message);
byte[] encryptedData = null;
using (var aliceAlgorithm = new ECDiffieHellmanCng(_aliceKey))
using (CngKey bobPubKey = CngKey.Import(_bobPubKeyBlob,
CngKeyBlobFormat.EccPublicBlob))
{
byte[] symmKey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
WriteLine("Alice creates this symmetric key with " +
$"Bobs public key information: { Convert.ToBase64String(symmKey)}");
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = symmKey;
aes.GenerateIV();
using (ICryptoTransform encryptor = aes.CreateEncryptor())
using (var ms = new MemoryStream())
{
// create CryptoStream and encrypt data to send
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
// write initialization vector not encrypted
await ms.WriteAsync(aes.IV, 0, aes.IV.Length);
await cs.WriteAsync(rawData, 0, rawData.Length);
}
encryptedData = ms.ToArray();
}
aes.Clear();
}
}
WriteLine($"Alice: message is encrypted: {Convert.ToBase64String(encryptedData)}"); ;
WriteLine();
return encryptedData;
}
示例14: TestSymmetricKeyProvider
public TestSymmetricKeyProvider(params string[] ids)
{
_keys = new Dictionary<string, TestSymmetricKey>();
foreach (var id in ids.Concat(new[] {"default"}).Distinct())
{
byte[] key;
byte[] iv;
using (var provider = new AesCryptoServiceProvider())
{
provider.GenerateIV();
provider.GenerateKey();
key = provider.Key;
iv = provider.IV;
provider.Clear();
}
_keys.Add(id, new TestSymmetricKey(key, iv));
}
}
示例15: BobReceivesData
private static void BobReceivesData(byte[] encryptedData)
{
Console.WriteLine("Bob receives encrypted data");
byte[] rawData = null;
var aes = new AesCryptoServiceProvider();
int nBytes = aes.BlockSize >> 3;
byte[] iv = new byte[nBytes];
for(int i = 0; i < iv.Length; ++i)
{
iv[i] = encryptedData[i];
}
using(var bobAlgro = new ECDiffieHellmanCng(bobKey))
{
using(CngKey alicePubKey = CngKey.Import(alicePubKeyBlod, CngKeyBlobFormat.GenericPublicBlob))
{
byte[] symmKey = bobAlgro.DeriveKeyMaterial(alicePubKey);
Console.WriteLine("Bob creates this symmetric key with ALices public key information : {0}", Convert.ToBase64String(symmKey));
aes.Key = symmKey;
aes.IV = iv;
using(ICryptoTransform decryptor = aes.CreateDecryptor())
{
using(MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);
cs.Close();
rawData = ms.ToArray();
Console.WriteLine("Bob decrypts message to {0}", Encoding.UTF8.GetString(rawData));
}
}
}
}
aes.Clear();
}