本文整理汇总了C#中System.Security.Cryptography.AesManaged.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# AesManaged.Dispose方法的具体用法?C# AesManaged.Dispose怎么用?C# AesManaged.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.AesManaged
的用法示例。
在下文中一共展示了AesManaged.Dispose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
public static String Decrypt(String privateKey, String salt, ApiObject request)
{
var aes = new AesManaged();
aes.IV = Encoding.Unicode.GetBytes(salt);
aes.Key = RSADecrypt(privateKey, request.Key);
String decrypted = null;
try
{
var decryptor = aes.CreateDecryptor();
using (MemoryStream msDecrypt = new MemoryStream(request.Data))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
decrypted = srDecrypt.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
}
finally
{
aes.Dispose();
}
return decrypted;
}
示例2: Encrypt
void Encrypt(OnCommandArgs args, string[] inMsg)
{
if (inMsg.Length < 3){
_ircInterface.Client.SendMessage(SendType.Message, args.Source, args.Nick + " the syntax for this command is .encrypt <key> <data>");
return;
}
try{
var key = AsciiToBase10(inMsg[1].ToCharArray());
var msg = AssembleMessage(inMsg.Skip(2).ToList());
msg = msg.Remove(msg.Length - 1);
var msgByte = AsciiToBase10(msg.ToCharArray());
if (msgByte.Length%16 != 0){
var paddedLen = (msgByte.Length/16 + 1)*16;
msgByte = PKCS7(msgByte, paddedLen);
}
var aes = new AesManaged();
aes.KeySize = 128;
aes.Mode = CipherMode.CBC;
aes.Key = PKCS7(key, 16);
aes.Padding = PaddingMode.None;
aes.IV = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
var memStrm = new MemoryStream(msgByte.Length);
var cryptStrm = new CryptoStream(memStrm, aes.CreateEncryptor(), CryptoStreamMode.Write);
cryptStrm.Write(msgByte, 0, msgByte.Length);
var ret = memStrm.ToArray();
aes.Dispose();
cryptStrm.Dispose();
memStrm.Dispose();
var retb10 = new string(Convert.ToBase64String(ret).ToCharArray());
_ircInterface.Client.SendMessage(SendType.Message, args.Source, args.Nick + ": " + retb10);
}
catch{
_ircInterface.Client.SendMessage(SendType.Message, args.Source, args.Nick + ": something went wrong during encryption.");
}
}
示例3: Decrypt
void Decrypt(OnCommandArgs args, string[] inMsg)
{
if (inMsg.Length < 3){
_ircInterface.Client.SendMessage(SendType.Message, args.Source, args.Nick + " the syntax for this command is .decrypt <key> <data>");
return;
}
try{
var key = AsciiToBase10(inMsg[1].ToCharArray());
var msgByte = Convert.FromBase64CharArray(inMsg[2].ToCharArray(), 0, inMsg[2].Length);
var aes = new AesManaged();
aes.Mode = CipherMode.CBC;
aes.IV = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
aes.KeySize = 128;
var keyPad = PKCS7(key, 16);
aes.Key = keyPad;
aes.Padding = PaddingMode.None;
var memStrm = new MemoryStream(msgByte.Length);
var cryptStrm = new CryptoStream(memStrm, aes.CreateDecryptor(), CryptoStreamMode.Write);
cryptStrm.Write(msgByte, 0, msgByte.Length);
var ret = memStrm.ToArray();
aes.Dispose();
cryptStrm.Dispose();
memStrm.Dispose();
ret = StripPKCS7(ret);
var retStr = new string(Base10ToAscii(ret));
_ircInterface.Client.SendMessage(SendType.Message, args.Source, args.Nick + ": " + retStr);
}
catch{
_ircInterface.Client.SendMessage
(SendType.Message, args.Source, args.Nick + ": something went wrong during decryption. did you change the encrypted string?");
}
}
示例4: Encrypt
public static ApiObject Encrypt(String publicKey, String salt, String dataToEncrypt)
{
var aes = new AesManaged();
aes.IV = Encoding.Unicode.GetBytes(salt);
Byte[] key = aes.Key;
Byte[] encrypted = null;
try
{
var encryptor = aes.CreateEncryptor();
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(dataToEncrypt);
}
encrypted = msEncrypt.ToArray();
}
}
} catch(Exception ex)
{
}
finally
{
aes.Dispose();
}
return new ApiObject { Data = encrypted, Key = RSAEncrypt(publicKey, key) };
}